题目本身就很好,同时又能带来对树状数组的一些思考。 题解 # 我们要倒着处理,对于当前的iii,会存在一个kkk,使得kkk个还没有用过的最小的数的和为sis_isi。那么当前iii的答案就是k+1k+1k+1。可以用树状数组配二分找,也可以用树状数组配倍增黑科技求。 Code # 二分 # #include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); ++i) #define for1(i, n) for (int i = 1; i <= int(n); ++i) #define fore(i, l, r) for (int i = int(l); i <= int(r); ++i) #define ford(i, n) for (int i = int(n)-1; i >= 0; --i) #define pb push_back #define eb emplace_back #define ms(a, x) memset(a, x, sizeof(a)) #define F first #define S second #define endl '\n' #define all(x) (x).begin(),(x).end() using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = 0x3f3f3f3f; mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); template<typename... Args> void write(Args... args) { ((cout << args << " "), ...); cout<<endl;} struct fenwick{ vector<ll> t; int n; fenwick(int n_):n(n_){ t=vector<ll>(n+1); } void update(int i,int x){ for(;i<=n;i+=i&-i){ t[i]+=x; } } ll get(int i){ ll res=0; for(;i>0;i-=i&-i) res+=t[i]; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin>>n; vector<ll> a(n); for(auto& it:a) cin>>it; fenwick tree(n); for(int i=1;i<=n;i++) tree.update(i,i); vector<int> ans(n); for(int i=n-1;i>=0;i--){ int l=1,r=n; while(l<=r){ int mid=(l+r)>>1; if(tree.get(mid)<=a[i]) l=mid+1; else r=mid-1; } ans[i]=l; tree.update(l,-l); } for(auto it:ans) cout<<it<<' '; return 0; } 倍增 # #include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); ++i) #define for1(i, n) for (int i = 1; i <= int(n); ++i) #define fore(i, l, r) for (int i = int(l); i <= int(r); ++i) #define ford(i, n) for (int i = int(n)-1; i >= 0; --i) #define pb push_back #define eb emplace_back #define ms(a, x) memset(a, x, sizeof(a)) #define F first #define S second #define endl '\n' #define all(x) (x).begin(),(x).end() using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = 0x3f3f3f3f; mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); template<typename... Args> void write(Args... args) { ((cout << args << " "), ...); cout<<endl;} struct fenwick{ vector<ll> t; int n; fenwick(int n_):n(n_){ t=vector<ll>(n+1); } void update(int i,int x){ for(;i<=n;i+=i&-i){ t[i]+=x; } } int search(ll prefix){ int pos=0; ll sum=0; for(int i=20;i>=0;i--){ if(pos+(1<<i)<=n&&(sum+t[pos+(1<<i)]<=prefix)){ pos+=(1<<i); sum+=t[pos]; } } return pos+1; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin>>n; vector<ll> a(n); for(auto& it:a) cin>>it; fenwick tree(n); for(int i=1;i<=n;i++) tree.update(i,i); vector<int> ans(n); for(int i=n-1;i>=0;i--){ int x=tree.search(a[i]); ans[i]=x; tree.update(x,-x); } for(auto it:ans) cout<<it<<' '; return 0; }