1 条题解

  • 1
    @ 2026-8-28 19:59:42

    #include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n;

    // 存储数列和记录出现过的数
    vector<int> a(n);
     unordered_set<int> s;
    s.insert(1); // 用于快速查找
    
    a[0] = 1;
    
    for(int k = 2; k <= n; k++){
        int candidate = a[k-2] - k; // 注意数组下标
        
        if( candidate > 0&&s.find(candidate )==s.end()){ // 两个判断条件
            a[k-1] = candidate;
        } else {
            a[k-1] = a[k-2] + k;
        }
        
        s.insert(a[k-1]); // 记录已出现
    }
    
    sort(a.begin(), a.end()); // 排序
    
    for(int i = 0; i < n; i++){
        cout << a[i] << " ";
    }
    
    return 0;
    

    }

    • 1

    信息

    ID
    82
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    8
    已通过
    2
    上传者