1 条题解

  • 1
    @ 2022-03-20 15:54:22

    70pts

    模拟

    #include<iostream>
    #include<vector>
    using namespace std;
    struct node {
        bool s;
        int b;
    } a[200005];
    int n;
    vector<int> v;
    int main() {
        cin>>n;
        for (int i=1;i<=n;i++) {
            cin>>a[i].s;
            a[i].b=i;
        }
        while(n>=1) {
            for(int i=1;i<=n;i++) {
                if(a[i].s==a[i-1].s&&i!=1) continue;
                cout<<a[i].b<<" ";
                b.push_back(i);
            }
            for (int i=0;i<(int)v.size();i++) {
                for (int j=v[i];j<n;j++)
                    a[j]=a[j+1];
                for (int j=i+1;j<(int)v.size();j++) v[j]--;
                n--;
            }
            cout<<endl;
        }
        return 0;
    }
    

    优化之后

    #include <bits/stdc++.h>
    #define lld long long
    using namespace std;
    #define frein(x) freopen(x, "r", stdin)
    #define freout(x) freopen(x, "w", stdout)
    struct seg {
        int l, r;
    };
    bool operator < (const seg & a, const seg & b) {
        return a.r < b.r;
    }
    set<seg> s;
    int n;
    int a[300010];
    bool del[300010];
    void remove(set<seg>::iterator & it) {
        seg x = *it;
        if (!x.l || !x.r) return;
        s.erase(it ++);
        del[x.l] = 1;
        ++ x.l;
        while (del[x.l] && x.l <= x.r) ++ x.l;
        if (x.r < x.l) return;
        if (it != s.begin()) {
            set<seg>::iterator jt = it;
            -- jt;
            if (a[jt->r] == a[x.l]) {
                seg t = *jt;
                s.erase(jt);
                t.r = x.r;
                s.insert(t);
                return;
            }
        }
        s.insert(x);
    }
    int main() {
        scanf("%d", &n);
        a[0] = -1;
        seg x;
        x.l = x.r = 0;
        for (int i = 1; i <= n; ++ i) {
            scanf("%d", a + i);
            if (a[i] == a[i - 1]) x.r  = i;
            else {
                if (x.l) s.insert(x);
                x.l = x.r = i;
            }
        }
         s.insert(x);
        set<seg>::iterator it;
        while (s.size()) {
            for (it = s.begin(); it != s.end(); ++ it) 
                printf("%d ", it->l);
            puts("");
            for (it = s.begin(); it != s.end();)
                remove(it);
        }
        return 0;
    }
    
  • 1

[CSP-J 2021] 小熊的果篮

信息

ID
1101
难度
6
分类
枚举 点击显示
标签
递交数
6
已通过
1
通过率
17%
上传者