#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;
vector<pii> oc;
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{
int pos = -1;
int s= 0;
if(A[i]>B[i-1]){
////cout<<"up"<<endl;
int lim = abs(B[i-1] -A[i]);
oc.clear();
oc.push_back(pii(i, 0));
for(int j = i-1; j>=0; j--){
s+=up_cost[j];
lim = min(lim, up_limit[j]);
//cout<<"lim "<<lim<<endl;
if(s==0){
oc.push_back(pii(j, lim));
}
}
reverse(oc.begin(), oc.end());
int move =0;
for(int inter= 0; inter<oc.size()-1; inter++){
int j = oc[inter].first;
if(oc[inter].first>0){
//cout<<"diff "<<abs(B[j-1]+h-B[j]-move)<<endl;
move = min(oc[inter].second, move +abs(B[j-1]+h-B[j]-move));
}
else{
move = oc[inter].second;
}
//cout<<oc[inter].first<<" "<<move<<endl;
for(int j = oc[inter].first; j<oc[inter+1].first; j++){
B[j] += move;
}
}
find_costs(0, i+1);
}
else{
////cout<<"down"<<endl;
int lim = abs(B[i-1] -A[i]);
oc.clear();
oc.push_back(pii(i, 0));
for(int j = i-1; j>=0; j--){
s+=down_cost[j];
lim = min(lim, down_limit[j]);
////cout<<"cost "<<up_cost[j]<<endl;
//cout<<"lim "<<lim<<endl;
if(s==0){
oc.push_back(pii(j,lim));
}
}
reverse(oc.begin(), oc.end());
int move =0;
for(int inter= 0; inter<oc.size()-1; inter++){
int j = oc[inter].first;
if(oc[inter].first>0){
move = min(oc[inter].second, move +abs(B[j-1]-h-B[j]+move));
}
else{
move = oc[inter].second;
}
//cout<<oc[inter].first<<" "<<move<<endl;
for(int j = oc[inter].first; j<oc[inter+1].first; j++){
B[j] -= move;
}
}
find_costs(0, i+1);
}
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(0, i+1);
for(int i = 0; i<n; i++){
//cout<<B[i]<<" ";
}
//cout<<endl;
////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;
}
Compilation message
safety.cpp: In function 'int main()':
safety.cpp:75:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int inter= 0; inter<oc.size()-1; inter++){
| ~~~~~^~~~~~~~~~~~
safety.cpp:108:40: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int inter= 0; inter<oc.size()-1; inter++){
| ~~~~~^~~~~~~~~~~~
safety.cpp:55:17: warning: unused variable 'pos' [-Wunused-variable]
55 | int pos = -1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2074 ms |
2364 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
74 ms |
588 KB |
Output is correct |
29 |
Correct |
53 ms |
504 KB |
Output is correct |
30 |
Correct |
48 ms |
576 KB |
Output is correct |
31 |
Correct |
19 ms |
652 KB |
Output is correct |
32 |
Correct |
15 ms |
560 KB |
Output is correct |
33 |
Correct |
75 ms |
632 KB |
Output is correct |
34 |
Correct |
20 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
74 ms |
588 KB |
Output is correct |
35 |
Correct |
53 ms |
504 KB |
Output is correct |
36 |
Correct |
48 ms |
576 KB |
Output is correct |
37 |
Correct |
19 ms |
652 KB |
Output is correct |
38 |
Correct |
15 ms |
560 KB |
Output is correct |
39 |
Correct |
75 ms |
632 KB |
Output is correct |
40 |
Correct |
20 ms |
592 KB |
Output is correct |
41 |
Correct |
88 ms |
656 KB |
Output is correct |
42 |
Correct |
46 ms |
584 KB |
Output is correct |
43 |
Correct |
24 ms |
548 KB |
Output is correct |
44 |
Correct |
48 ms |
624 KB |
Output is correct |
45 |
Correct |
23 ms |
728 KB |
Output is correct |
46 |
Correct |
15 ms |
580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
1 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 |
0 ms |
340 KB |
Output is correct |
19 |
Execution timed out |
2074 ms |
2364 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |