Submission #406124

#TimeUsernameProblemLanguageResultExecution timeMemory
406124tqbfjotldThe short shank; Redemption (BOI21_prison)C++14
100 / 100
752 ms12868 KiB
#include <bits/stdc++.h>
using namespace std;

int range_len[2000005];
pair<int,int> memo[4005][4005];
int N,D,T;

pair<int,int> operator+ (pair<int,int> a, pair<int,int> b){
    return {a.first+b.first,a.second+b.second};
}

pair<int,int> solve(int K){
    //printf("solving %lld\n",K);
    int v1 = 0;
    deque<int> inc_loc;
    deque<int> inc_loc_2;
    int v2 = 0;
    int offset = 0;
    if (range_len[N-1]>0){
        v1 = 1;
    }
    else{
        inc_loc.push_back(1);
    }
    for (int x = N-2; x>=0; x--){
        offset++;v1++;
        if (range_len[x]==0){
            v1--;
            inc_loc.push_front(1-offset);
        }
        else{
            while ((!inc_loc.empty())&&(inc_loc.front())+offset<=range_len[x]){
                inc_loc.pop_front();
                v1++;
            }
            while ((!inc_loc_2.empty())&&(inc_loc_2.front())+offset<=range_len[x]){
                inc_loc_2.pop_front();
                v2++;
            }
        }
        int other_val = v1+K;
        int last_erased = 999999999;
        while (v1+inc_loc.size()>other_val){
            //printf("hi\n");
            last_erased = inc_loc.back();
            inc_loc.pop_back();
        }
        if (last_erased!=999999999) {
                while (!inc_loc_2.empty() && (inc_loc_2.back())>last_erased)inc_loc_2.pop_back();
                inc_loc_2.push_back(last_erased);
        }

       /* int t = v2;
        for (int x = 0; x<=N; x++){
            printf("%lld ",t);
            t+=inc_loc_2.count(x-offset);
        }
        printf("\n");*/
    }
    //printf("\n");
/*
    for (int x = N-1; x>=0; x--){
        for (int o = 0; o<=N; o++){
            memo[x][o] = memo[x+1][0]+make_pair(K+(range_len[x]>0),1);
            if (o>range_len[x]) memo[x][o] = min(memo[x][o],memo[x+1][o-1]+make_pair(1,0));
            else memo[x][o] = min(memo[x][o],memo[x+1][max(0LL,range_len[x]-1)]+make_pair((range_len[x]>0),0));
            printf("%lld ",memo[x][o].second);
        }
        printf("\n");
    }
    printf("\n");
    assert(v1==memo[0][0].first);
    assert(v2==memo[0][0].second);*/
    return {v1,v2};
}

inline void scan(int &a){
    a = 0;
    char ch = ' ';
    while (ch<'0'||ch>'9') ch = getchar();
    while (ch>='0'&&ch<='9'){
        a = (a<<3)+(a<<1)+ch-'0';
        ch = getchar();
    }
}

main(){
    scan(N); scan(D); scan(T);
    for (int x = 0; x<N; x++){
        int a;
        scan(a);
        range_len[x] = T-a+1;
        if (range_len[x]<0) range_len[x] = 0;
        if (x+range_len[x]>N) range_len[x] = N-x;
    }
    int a = -1;
    int b = N;
    while (a+1<b){
        //printf("try %lld\n",(a+b)/2);
        if (solve((a+b)/2).second>D){
            a = (a+b)/2;
        }
        else b = (a+b)/2;
    }
    //for (int x = 0; x<=N; x++) solve(x);
    pair<int,int> res = solve(b);
    //printf("b = %lld, res = %lld %lld\n",b,res);
    printf("%d",res.first-res.second*b-b*(D-res.second));
}

Compilation message (stderr)

prison.cpp: In function 'std::pair<int, int> solve(int)':
prison.cpp:43:33: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   43 |         while (v1+inc_loc.size()>other_val){
      |                ~~~~~~~~~~~~~~~~~^~~~~~~~~~
prison.cpp: At global scope:
prison.cpp:87:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   87 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...