题解:CF2254E Chronostasis

· · 题解

禁止饭堂。

显然如果合法那么对于任意一个合法的前驱都一定存在合法的后继。

然后可以直接贪心,假设初始点是 x=0,之后每次选择一个最小的元素 d 保证 x+d>0,令 x\gets x+d

显然保证了字典序最小。具体实现可以直接用 set

注意 set 不可重,所以要存 pair 以区分。

时间复杂度 O(n\log n)

inline void solve(){
    int n;
    cin>>n;
    set<pair<int,int>>st;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        st.insert({x,i});
    }
    int sum=0;
    vector<int>ans;
    while(!st.empty()){
        auto flc=st.lower_bound({-sum+1,-1});
        if(flc==st.end()){
            cout<<"-1\n";
            return;
        }
        sum+=(*flc).first;
        ans.push_back(sum);
        st.erase(flc);
    }
    for(int i:ans)cout<<i<<' ';
    cout<<'\n';
}
// 脸色多来讨好大众
// 其实早已内心空洞
// 浮躁气氛闷住晚风
// 追根揭底万事皆空

// 如果世间一切相遇
// 最后大多无疾而终
// 此刻只想望你眼眸
// 就算明天世界将终