Submission #799459

# Submission time Handle Problem Language Result Execution time Memory
799459 2023-07-31T14:48:13 Z anton Safety (NOI18_safety) C++17
33 / 100
2000 ms 1696 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>


const int MAX_N = 2*1e5;
const int INF = 1e18;
int n, h, A[MAX_N], B[MAX_N], C[MAX_N];
int up_cost[MAX_N], down_cost[MAX_N];
int up_limit[MAX_N], down_limit[MAX_N];

void find_costs(int l, int r){
    for(int i = l; i<r; i++){
        C[i] = B[i]-A[i];
        if(C[i] == 0){
            up_cost[i] = 1;
            down_cost[i] = 1;
            up_limit[i]= INF;
            down_limit[i] = INF;
        }
        else if(C[i]<0){
            up_limit[i] = abs(C[i]);
            down_limit[i] =INF;
            up_cost[i] =-1;
            down_cost[i] =1;
        }
        else if(C[i]>0){
            up_limit[i] = INF;
            down_limit[i] = abs(C[i]);
            up_cost[i] =1;
            down_cost[i] =-1;
        }
    }
}
signed main(){
    cin>>n>>h;

    for(int i = 0; i<n; i++){
        cin>>A[i];
    }
    int res= 0;
    B[0] = A[0];
    C[0] =0;
    up_cost[0] = down_cost[0] = 1;
    for(int i = 1; i<n; i++){
        if(abs(A[i]-B[i-1])<=h){
            B[i] = A[i];
            C[i] = 0;
            up_cost[0] = down_cost[0] = 1;
        }
        else{
            bool has_zero = true;
            int prev_pos= -1;
            while(has_zero){
                has_zero = false;
                int pos = -1;
                int s= 0;
                if(A[i]>B[i-1]){
                    //cout<<"up"<<endl;
                    for(int j = i-1; j>prev_pos; j--){
                        s+=up_cost[j];
                        //cout<<"cost "<<up_cost[j]<<endl;
                        if(s==0){
                            pos = j;
                        }
                    }
                    //cout<<pos<<endl;
                    if(pos!=-1){
                        has_zero = true;
                        int lim = abs(B[i-1] -A[i]);
                        for(int j = pos; j<i; j++){
                            lim = min(lim, up_limit[j]);
                        }
                        if(pos>0){
                            lim = min(lim, B[pos-1]+h-B[pos]);
                        }
                        
                        //cout<<"lim "<<lim<<endl;

                        for(int j = pos; j<i; j++){
                            B[j]+=lim;
                        }
                        find_costs(pos, i);
                    }   

                }
                else{
                    //cout<<"down"<<endl;
                    for(int j = i-1; j>prev_pos; j--){
                        s+=down_cost[j];
                        
                        //cout<<"cost "<<down_cost[j]<<endl;
                        if(s==0){
                            pos = j;
                        }
                    }
                    if(pos!=-1){
                        //cout<<"pos "<<pos<<endl;
                        has_zero = true;

                        int lim = abs(B[i-1] -A[i]);
                        for(int j = pos; j<i; j++){
                            lim = min(lim, down_limit[j]);
                        }

                        if(pos>0){
                            lim = min(lim, abs(B[pos-1]-h-B[pos]));
                        }
                        //cout<<"lim "<<lim<<endl;

                        for(int j = pos; j<i; j++){
                            B[j]-=lim;
                        }
                        find_costs(pos, i);
                    }
                }

                prev_pos =pos;
            }

            if(abs(A[i]-B[i-1])<=h){
                B[i] = A[i];
                C[i] = 0;
                up_cost[0] = down_cost[0] = 1;
            }
            else{
                if(A[i]>B[i-1]){
                    B[i] = B[i-1] +h;
                }
                else{
                    B[i] = B[i-1] -h;
                }
                find_costs(i, i+1);
            }

            
            
        }
        find_costs(0, i+1);
        //cout<<endl;
    }

    find_costs(0, n);

    for(int i = 0; i<n; i++){
        res+=abs(C[i]);
    }

    for(int i = 0; i<n; i++){
        //cout<<B[i]<<" ";
    }
    //cout<<endl;

    cout<<res<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2057 ms 1696 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 19 ms 380 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 19 ms 380 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 6 ms 376 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 19 ms 372 KB Output is correct
27 Correct 8 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 19 ms 380 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 6 ms 376 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 4 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 19 ms 372 KB Output is correct
27 Correct 8 ms 340 KB Output is correct
28 Correct 1890 ms 636 KB Output is correct
29 Correct 1614 ms 540 KB Output is correct
30 Correct 36 ms 612 KB Output is correct
31 Correct 15 ms 548 KB Output is correct
32 Correct 22 ms 552 KB Output is correct
33 Execution timed out 2072 ms 452 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 5 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 19 ms 380 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 6 ms 376 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 4 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 19 ms 372 KB Output is correct
33 Correct 8 ms 340 KB Output is correct
34 Correct 1890 ms 636 KB Output is correct
35 Correct 1614 ms 540 KB Output is correct
36 Correct 36 ms 612 KB Output is correct
37 Correct 15 ms 548 KB Output is correct
38 Correct 22 ms 552 KB Output is correct
39 Execution timed out 2072 ms 452 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Execution timed out 2057 ms 1696 KB Time limit exceeded
20 Halted 0 ms 0 KB -