题解 | 小红的排列构造②

小红的排列构造②

https://www.nowcoder.com/practice/a4ec29e74aaa450aa8a4200fe3b06308

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;cin>>n;
    string s;cin>>s;
    if(s.back()=='0'){
        cout<<-1<<'\n';
        return 0;
    }
    set <int> se;
    for(int i=1;i<=n;i++) se.insert(i);
    /*
    1 1 1 0 0 1 0 1
    1 2 3 5 6 4 8 7
    0 0 0 0 0 0 1 1
    2 3 4 5 6 7 1 8
    */
    for(int i=1;i<=s.size();i++){
        if(s[i-1]=='1'){
            cout<<*se.begin()<<' ';
            se.erase(se.begin());
            continue;
        }
        else{
            auto it=se.upper_bound(i);
            cout<<*it<<' ';
            se.erase(it);
            continue;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务