답안 #516072

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
516072 2022-01-20T11:00:15 Z blue Teams (CEOI11_tea) C++17
100 / 100
2327 ms 113156 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
using vi = vector<int>;
using pii = pair<int, int>;
using vvi = vector<vi>;
#define sz(x) int(x.size())
 
const int INF = 100'000'000;
 
const int Z = (1<<20);
vector<int> segtree;
 
void set(int i, int v)
{
    i += Z;
    segtree[i] = v;
    for(i /= 2; i >= 1; i /= 2) segtree[i] = max(segtree[i*2], segtree[(i*2) | 1]);
}
 
 
vector<pii> segtree2;
void set2(int i, int v)
{
    // cerr << "\n\n" << "set2 " << i << ' ' << v << '\n';
    i += Z;
    segtree2[i].first = v;
    for(i /= 2; i >= 1; i /= 2) segtree2[i] = max(segtree2[i*2], segtree2[(i*2) | 1]);
    // cerr << "root = " << segtree2[1]
}
 
 
 
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    int n;
    cin >> n;
 
    pii a[1+n];
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i].first;
        a[i].second = i;
    }
    sort(a+1, a+n+1);
 
    int t;
 
    vi dp(1+n);
    vi dpmx(1+n);
 
    dp[0] = dpmx[0] = 0;
 
    for(int i = 1; i <= n; i++)
    {
        t = -INF;
        if(a[i].first > i)
        {
            ;
        }
        else
        {
            if(i - a[i].first >= 0)
                t = dpmx[i - a[i].first];
        }
 
        dp[i] = max(-INF, t + 1);
        dpmx[i] = max(dp[i], dpmx[i-1]);
 
        // cerr << i << " : " << t << '\n';
    }
 
    int team_count = dp[n];
 
    // int Z = (1<<20);
 
    int z = 0;
    for(int i = 1; i <= n; i++) z = max(z, a[i].first);
 
 
    int lo = 1, hi = n;
    while(lo != hi)
    {
        int mxs = (lo+hi)/2;
 
        segtree = vector<int>(Z<<1, -INF);
 
        int l = 0;
        int r = -1;
 
        vi new_dp(1+n, -INF);
        new_dp[0] = 0;
 
        // cerr << "n = " << n << '\n';
 
        for(int i = 1; i <= n; i++)
        {
            int nl = max(0, i-mxs);
            int nr = max(-1, i - a[i].first);
 
 
 
            if(nl > nr) continue;
 
 
            if(nr - nl < 30)
            {
                for(int g = nl; g <= nr; g++)
                    new_dp[i] = max(new_dp[i], new_dp[g]+1);
                continue;
            }
 
            while(r < nr)
            {
                r++;
                set(r, new_dp[r]);
            }
 
            while(l > nl)
            {
                l--;
                set(l, new_dp[l]);
            }
 
            while(r > nr)
            {
                set(r, -INF);
                r--;
            }
 
            while(l < nl)
            {
                set(l, -INF);
                l++;
            }
 
            new_dp[i] = max(-INF, segtree[1]+1);
        }
 
        if(new_dp[n] == team_count)
            hi = mxs;
        else
            lo = mxs+1;
    }
 
    // cerr << team_count << ' ' << lo << '\n';
 
 
 
 
 
 
 
        int mxs = lo;
 
        // cerr << "\n\n\n\n\n";
        // cerr << lo << ' ' << hi << " : " << mxs << '\n';
 
        segtree2 = vector<pii>(Z<<1, {-INF, -1});
        for(int i = 0; i <= n; i++) segtree2[Z+i].second = i;
 
        int l = 0;
        int r = -1;
 
        vi new_dp(1+n, -INF);
        new_dp[0] = 0;
 
        vi prv(1+n, -1);
 
        // cerr << "n = " << n << '\n';
 
        for(int i = 1; i <= n; i++)
        {
            int nl = max(0, i-mxs);
            int nr = max(-1, i - a[i].first);
 
            if(nr - nl < 20)
            {
                for(int i2 = nl; i2 <= nr; i2++)
                {
                    if(new_dp[i2]+1 >= new_dp[i])
                    {
                        new_dp[i] = new_dp[i2] + 1;
                        prv[i] = i2;
                    }
                }
                continue;
            }
            // if(i-mxs > i - a[i]) con
            // cerr << "i = " << i << " : " << "nl nr = " << nl << ' ' << nr << '\n';
            if(nl > nr) continue;
 
            // cerr << "journey " << l << ' ' << r << " -> " << nl << ' ' << nr << '\n';
 
            while(r < nr)
            {
                r++;
                set2(r, new_dp[r]);
            }
 
            while(l > nl)
            {
                l--;
                set2(l, new_dp[l]);
            }
 
            while(r > nr)
            {
                set2(r, -INF);
                r--;
            }
 
            while(l < nl)
            {
                set2(l, -INF);
                l++;
            }
            // cerr << l << ' ' << r << '\n';
            // cerr << "st = " << segtree2[1].first << ' ' << segtree2[1].second << '\n';
 
            new_dp[i] = max(-INF, segtree2[1].first+1);
            prv[i] = segtree2[1].second;
 
            // cerr << i << " : " << new_dp[i] << ' ' << prv[i] << '\n';
        }
 
        vvi teams;
           int I = n;
           while(I != 0)
           {
               teams.push_back(vi(0));
               // cerr << I << ' ' << prv[I] << '\n';
               for(int q = I; q > prv[I]; q--)
                   teams.back().push_back(a[q].second);
               I = prv[I];
           }
 
           cout << team_count << '\n';
           for(int f = 0; f < team_count; f++)
           {
               cout << sz(teams[f]) << ' ';
               for(int w: teams[f]) cout << w << ' ';
               cout << '\n';
           }
           return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16716 KB Output is correct
2 Correct 30 ms 33124 KB Output is correct
3 Correct 22 ms 33128 KB Output is correct
4 Correct 21 ms 33120 KB Output is correct
5 Correct 23 ms 33088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 33136 KB Output is correct
2 Correct 28 ms 33116 KB Output is correct
3 Correct 25 ms 33336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 33160 KB Output is correct
2 Correct 26 ms 33092 KB Output is correct
3 Correct 26 ms 33104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 33168 KB Output is correct
2 Correct 36 ms 33196 KB Output is correct
3 Correct 41 ms 33224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 33164 KB Output is correct
2 Correct 38 ms 33168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 34784 KB Output is correct
2 Correct 162 ms 34880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 35032 KB Output is correct
2 Correct 128 ms 34692 KB Output is correct
3 Correct 199 ms 35056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1732 ms 51292 KB Output is correct
2 Correct 1441 ms 52912 KB Output is correct
3 Correct 1654 ms 52232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2073 ms 59952 KB Output is correct
2 Correct 2015 ms 113156 KB Output is correct
3 Correct 1834 ms 62120 KB Output is correct
4 Correct 2050 ms 55700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1118 ms 62328 KB Output is correct
2 Correct 278 ms 63288 KB Output is correct
3 Correct 2327 ms 60060 KB Output is correct
4 Correct 2104 ms 59600 KB Output is correct