Submission #355297

# Submission time Handle Problem Language Result Execution time Memory
355297 2021-01-22T11:16:48 Z Kerim Ancient Books (IOI17_books) C++17
70 / 100
248 ms 27500 KB
#include "books.h"
#include "bits/stdc++.h"
#define MAXN 1000009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N=1004;
int dp[N][N];
int L[MAXN],R[MAXN],bel[MAXN],S;
int destL,destR;
PII extend(int l,int r){
	int lp=l,rp=r;
	if(bel[l])umin(lp,L[bel[l]]),umax(rp,R[bel[l]]);
	if(bel[r])umin(lp,L[bel[r]]),umax(rp,R[bel[r]]);
	while(1){	
		if(l>lp){
			if(bel[--l])
				umin(lp,L[bel[l]]),umax(rp,R[bel[l]]);	
		}
		else if(r<rp){
			if(bel[++r])
				umin(lp,L[bel[r]]),umax(rp,R[bel[r]]);	
		}
		else
			break;
	}
	return mp(l,r);
}
int rec(int l,int r){
	if(l<destL or r>destR)return INF;
	PII tmp=extend(l,r);
	l=tmp.ff;r=tmp.ss;
	if(l==destL and r==destR)return 0;
	int &ret=dp[l][r];
	if(~ret)return ret;
	return ret=min(rec(l-1,r),rec(l,r+1))+2;
}
long long minimum_walk(vector<int> p, int s) {
	memset(dp,-1,sizeof dp);
	int n=int(p.size());
	vector<int>vis(n);
	destL=destR=s;
	ll res=0,e=0;int last=0;
	for(int i=0;i<n;i++){
		res+=abs(p[i]-i);
		if(p[i]!=i and !vis[i]){
			int to=i;L[++S]=i;
			do{
				umax(R[S],to);bel[to]=S;
				vis[to]=1;to=p[to];
			}while(to!=i);
			e+=max(0,i-destR)*2;
			umin(destL,L[S]);umax(destR,R[S]);
		} 
	}
	if(n<N)return rec(s,s)+res;
	return res+e;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:57:19: warning: unused variable 'last' [-Wunused-variable]
   57 |  ll res=0,e=0;int last=0;
      |                   ^~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4332 KB Output is correct
2 Correct 3 ms 4332 KB Output is correct
3 Correct 3 ms 4332 KB Output is correct
4 Correct 3 ms 4332 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 3 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4204 KB Output is correct
10 Correct 3 ms 4332 KB Output is correct
11 Correct 3 ms 4204 KB Output is correct
12 Correct 3 ms 4332 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4204 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4332 KB Output is correct
2 Correct 3 ms 4332 KB Output is correct
3 Correct 3 ms 4332 KB Output is correct
4 Correct 3 ms 4332 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 3 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4204 KB Output is correct
10 Correct 3 ms 4332 KB Output is correct
11 Correct 3 ms 4204 KB Output is correct
12 Correct 3 ms 4332 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4204 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4332 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4332 KB Output is correct
2 Correct 3 ms 4332 KB Output is correct
3 Correct 3 ms 4332 KB Output is correct
4 Correct 3 ms 4332 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 3 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4204 KB Output is correct
10 Correct 3 ms 4332 KB Output is correct
11 Correct 3 ms 4204 KB Output is correct
12 Correct 3 ms 4332 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4204 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4332 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
30 Correct 247 ms 19948 KB Output is correct
31 Correct 248 ms 21356 KB Output is correct
32 Correct 143 ms 17516 KB Output is correct
33 Correct 142 ms 22764 KB Output is correct
34 Correct 147 ms 22912 KB Output is correct
35 Correct 149 ms 23004 KB Output is correct
36 Correct 152 ms 22656 KB Output is correct
37 Correct 147 ms 21484 KB Output is correct
38 Correct 144 ms 21356 KB Output is correct
39 Correct 147 ms 21300 KB Output is correct
40 Correct 147 ms 21356 KB Output is correct
41 Correct 179 ms 21228 KB Output is correct
42 Correct 166 ms 21228 KB Output is correct
43 Correct 145 ms 23276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4332 KB Output is correct
2 Correct 3 ms 4332 KB Output is correct
3 Correct 5 ms 4332 KB Output is correct
4 Correct 6 ms 4332 KB Output is correct
5 Correct 6 ms 4332 KB Output is correct
6 Correct 3 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4332 KB Output is correct
10 Correct 3 ms 4332 KB Output is correct
11 Correct 3 ms 4332 KB Output is correct
12 Correct 3 ms 4332 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4332 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4332 KB Output is correct
2 Correct 3 ms 4332 KB Output is correct
3 Correct 3 ms 4332 KB Output is correct
4 Correct 3 ms 4332 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 3 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4204 KB Output is correct
10 Correct 3 ms 4332 KB Output is correct
11 Correct 3 ms 4204 KB Output is correct
12 Correct 3 ms 4332 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4204 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4332 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
30 Correct 247 ms 19948 KB Output is correct
31 Correct 248 ms 21356 KB Output is correct
32 Correct 143 ms 17516 KB Output is correct
33 Correct 142 ms 22764 KB Output is correct
34 Correct 147 ms 22912 KB Output is correct
35 Correct 149 ms 23004 KB Output is correct
36 Correct 152 ms 22656 KB Output is correct
37 Correct 147 ms 21484 KB Output is correct
38 Correct 144 ms 21356 KB Output is correct
39 Correct 147 ms 21300 KB Output is correct
40 Correct 147 ms 21356 KB Output is correct
41 Correct 179 ms 21228 KB Output is correct
42 Correct 166 ms 21228 KB Output is correct
43 Correct 145 ms 23276 KB Output is correct
44 Correct 6 ms 4332 KB Output is correct
45 Correct 3 ms 4332 KB Output is correct
46 Correct 5 ms 4332 KB Output is correct
47 Correct 6 ms 4332 KB Output is correct
48 Correct 6 ms 4332 KB Output is correct
49 Correct 3 ms 4332 KB Output is correct
50 Correct 3 ms 4332 KB Output is correct
51 Correct 3 ms 4332 KB Output is correct
52 Correct 3 ms 4332 KB Output is correct
53 Correct 3 ms 4332 KB Output is correct
54 Correct 3 ms 4332 KB Output is correct
55 Correct 3 ms 4332 KB Output is correct
56 Correct 3 ms 4332 KB Output is correct
57 Correct 3 ms 4332 KB Output is correct
58 Correct 3 ms 4332 KB Output is correct
59 Correct 3 ms 4332 KB Output is correct
60 Correct 3 ms 4332 KB Output is correct
61 Correct 3 ms 4332 KB Output is correct
62 Correct 3 ms 4332 KB Output is correct
63 Correct 3 ms 4332 KB Output is correct
64 Incorrect 148 ms 27500 KB 3rd lines differ - on the 1st token, expected: '9339550', found: '9338938'
65 Halted 0 ms 0 KB -