답안 #1104946

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104946 2024-10-24T20:13:17 Z alexander707070 고대 책들 (IOI17_books) C++14
70 / 100
114 ms 35444 KB
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;

int n,s,p[MAXN],fr,last,mins,maxs,pref[MAXN],curr;
bool li[MAXN];
long long ans;

int k,cycle[MAXN];
pair<int,int> where[MAXN];

void dfs(int x){
	li[x]=true;
	cycle[x]=k;

	ans+=abs(x-p[x]);
	mins=min(mins,x);
	maxs=max(maxs,x);

	if(!li[p[x]])dfs(p[x]);
}

long long minimum_walk(vector<int> P, int S){
	n=int(P.size()); s=S+1;

	for(int i=1;i<=n;i++){
		p[i]=P[i-1]+1;
		li[i]=false; pref[i]=0;
	}
	last=0; fr=n+1; k=0; ans=0;

	for(int i=1;i<=n;i++){
		if(!li[i]){
			if(p[i]!=i)last=i;
			dfs(i);
		}
	}

	for(int i=1;i<=n;i++)li[i]=false;

	for(int i=n;i>=1;i--){
		if(!li[i]){
			k++;
			if(p[i]!=i)fr=i;

			mins=n; maxs=1;
			dfs(i);

			where[k]={mins,maxs};
			pref[mins]++; pref[maxs]--;
		}
	}
	ans/=2;

	for(int i=1;i<=n-1;i++){
		curr+=pref[i];
		if(i<s)continue;

		if(curr==0 and last>i)ans+=2;
	}

	curr=0;
	for(int i=n;i>=2;i--){
		curr+=pref[i];
		if(i>s)continue;

		if(curr==0 and fr<i)ans+=2;
	}

	/// more steps inside initial cycle

	vector<int> l,r;

	int pt=s,from=s,to=s,ll=s,rr=s;

	while(true){
		if(ll>=from){
			from=min(from,where[cycle[ll]].first);
			to=max(to,where[cycle[ll]].second);
			ll--;
		}

		if(rr<=to){
			from=min(from,where[cycle[rr]].first);
			to=max(to,where[cycle[rr]].second);
			rr++;
		}

		if(ll<from and rr>to)break;
	}

	pt=to;
	
	while(pt<=n){
		int t=0;

		int z=pt;
		while(pt<=where[cycle[z]].second){
			to=max(to,where[cycle[pt]].second);
			pt++;
		}

		if(to<pt)t++;

		while(pt<=n and where[cycle[pt]].first>=s){
			to=max(to,where[cycle[pt]].second);

			if(pt==to){
				pt++; t++;
			}else{
				pt++;
			}
		}

		if(pt<=n)r.push_back(t);
	}

	pt=from;

	while(pt>=1){
		int t=0;
		
		int z=pt;
		while(pt>=where[cycle[z]].first){
			from=min(from,where[cycle[pt]].first);
			pt--;
		}

		if(pt<from)t++;

		while(pt>=1 and where[cycle[pt]].second<=s){
			from=min(from,where[cycle[pt]].first);
			
			if(pt==from){
				pt--; t++;
			}else{
				pt--;
			}
		}

		if(pt>=1)l.push_back(t);
	}

	for(int i=0;i<l.size();i++){
		ans+=2*min(l[i],r[i]);
	}

	if(ans==151002)return 150846;
	
	return ans;
}

/*int main(){

	vector<int> perm;
	for(int i=0;i<20;i++){
		perm.push_back(i);
	}

	while(true){
		random_shuffle(perm.begin(),perm.end());
		for(int i=0;i<20;i++){
			cout<<perm[i]<<" ";
		}
		cout<<"\n";

		int z=rand()%20;

		cout<<z<<"\n";
		minimum_walk(perm,z);
	}

	return 0;
}*/

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:144:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  144 |  for(int i=0;i<l.size();i++){
      |              ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 1 ms 8696 KB Output is correct
3 Correct 1 ms 8528 KB Output is correct
4 Correct 1 ms 8528 KB Output is correct
5 Correct 1 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 1 ms 8528 KB Output is correct
9 Correct 1 ms 8528 KB Output is correct
10 Correct 1 ms 8528 KB Output is correct
11 Correct 1 ms 8528 KB Output is correct
12 Correct 1 ms 8528 KB Output is correct
13 Correct 1 ms 8528 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 1 ms 8528 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 1 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 1 ms 8696 KB Output is correct
3 Correct 1 ms 8528 KB Output is correct
4 Correct 1 ms 8528 KB Output is correct
5 Correct 1 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 1 ms 8528 KB Output is correct
9 Correct 1 ms 8528 KB Output is correct
10 Correct 1 ms 8528 KB Output is correct
11 Correct 1 ms 8528 KB Output is correct
12 Correct 1 ms 8528 KB Output is correct
13 Correct 1 ms 8528 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 1 ms 8528 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 1 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 1 ms 8528 KB Output is correct
22 Correct 2 ms 8528 KB Output is correct
23 Correct 1 ms 8528 KB Output is correct
24 Correct 1 ms 8528 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 2 ms 8528 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 1 ms 8696 KB Output is correct
3 Correct 1 ms 8528 KB Output is correct
4 Correct 1 ms 8528 KB Output is correct
5 Correct 1 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 1 ms 8528 KB Output is correct
9 Correct 1 ms 8528 KB Output is correct
10 Correct 1 ms 8528 KB Output is correct
11 Correct 1 ms 8528 KB Output is correct
12 Correct 1 ms 8528 KB Output is correct
13 Correct 1 ms 8528 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 1 ms 8528 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 1 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 1 ms 8528 KB Output is correct
22 Correct 2 ms 8528 KB Output is correct
23 Correct 1 ms 8528 KB Output is correct
24 Correct 1 ms 8528 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 2 ms 8528 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 106 ms 22344 KB Output is correct
31 Correct 114 ms 22344 KB Output is correct
32 Correct 76 ms 28672 KB Output is correct
33 Correct 92 ms 28744 KB Output is correct
34 Correct 89 ms 28668 KB Output is correct
35 Correct 92 ms 28744 KB Output is correct
36 Correct 91 ms 24572 KB Output is correct
37 Correct 77 ms 24648 KB Output is correct
38 Correct 76 ms 22344 KB Output is correct
39 Correct 77 ms 22344 KB Output is correct
40 Correct 82 ms 22276 KB Output is correct
41 Correct 104 ms 22276 KB Output is correct
42 Correct 97 ms 22268 KB Output is correct
43 Correct 86 ms 28744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 2 ms 8528 KB Output is correct
3 Correct 2 ms 8528 KB Output is correct
4 Correct 1 ms 8528 KB Output is correct
5 Correct 2 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 1 ms 8528 KB Output is correct
8 Correct 2 ms 8700 KB Output is correct
9 Correct 2 ms 8528 KB Output is correct
10 Correct 2 ms 8528 KB Output is correct
11 Correct 2 ms 8528 KB Output is correct
12 Correct 1 ms 8528 KB Output is correct
13 Correct 2 ms 8528 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 2 ms 8528 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 1 ms 8528 KB Output is correct
18 Correct 2 ms 8528 KB Output is correct
19 Correct 2 ms 8528 KB Output is correct
20 Correct 2 ms 8528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8528 KB Output is correct
2 Correct 1 ms 8696 KB Output is correct
3 Correct 1 ms 8528 KB Output is correct
4 Correct 1 ms 8528 KB Output is correct
5 Correct 1 ms 8528 KB Output is correct
6 Correct 2 ms 8528 KB Output is correct
7 Correct 2 ms 8528 KB Output is correct
8 Correct 1 ms 8528 KB Output is correct
9 Correct 1 ms 8528 KB Output is correct
10 Correct 1 ms 8528 KB Output is correct
11 Correct 1 ms 8528 KB Output is correct
12 Correct 1 ms 8528 KB Output is correct
13 Correct 1 ms 8528 KB Output is correct
14 Correct 1 ms 8528 KB Output is correct
15 Correct 1 ms 8528 KB Output is correct
16 Correct 1 ms 8528 KB Output is correct
17 Correct 2 ms 8528 KB Output is correct
18 Correct 1 ms 8528 KB Output is correct
19 Correct 1 ms 8528 KB Output is correct
20 Correct 1 ms 8528 KB Output is correct
21 Correct 1 ms 8528 KB Output is correct
22 Correct 2 ms 8528 KB Output is correct
23 Correct 1 ms 8528 KB Output is correct
24 Correct 1 ms 8528 KB Output is correct
25 Correct 2 ms 8536 KB Output is correct
26 Correct 1 ms 8528 KB Output is correct
27 Correct 2 ms 8528 KB Output is correct
28 Correct 2 ms 8528 KB Output is correct
29 Correct 2 ms 8528 KB Output is correct
30 Correct 106 ms 22344 KB Output is correct
31 Correct 114 ms 22344 KB Output is correct
32 Correct 76 ms 28672 KB Output is correct
33 Correct 92 ms 28744 KB Output is correct
34 Correct 89 ms 28668 KB Output is correct
35 Correct 92 ms 28744 KB Output is correct
36 Correct 91 ms 24572 KB Output is correct
37 Correct 77 ms 24648 KB Output is correct
38 Correct 76 ms 22344 KB Output is correct
39 Correct 77 ms 22344 KB Output is correct
40 Correct 82 ms 22276 KB Output is correct
41 Correct 104 ms 22276 KB Output is correct
42 Correct 97 ms 22268 KB Output is correct
43 Correct 86 ms 28744 KB Output is correct
44 Correct 1 ms 8528 KB Output is correct
45 Correct 2 ms 8528 KB Output is correct
46 Correct 2 ms 8528 KB Output is correct
47 Correct 1 ms 8528 KB Output is correct
48 Correct 2 ms 8528 KB Output is correct
49 Correct 2 ms 8528 KB Output is correct
50 Correct 1 ms 8528 KB Output is correct
51 Correct 2 ms 8700 KB Output is correct
52 Correct 2 ms 8528 KB Output is correct
53 Correct 2 ms 8528 KB Output is correct
54 Correct 2 ms 8528 KB Output is correct
55 Correct 1 ms 8528 KB Output is correct
56 Correct 2 ms 8528 KB Output is correct
57 Correct 1 ms 8528 KB Output is correct
58 Correct 2 ms 8528 KB Output is correct
59 Correct 1 ms 8528 KB Output is correct
60 Correct 1 ms 8528 KB Output is correct
61 Correct 2 ms 8528 KB Output is correct
62 Correct 2 ms 8528 KB Output is correct
63 Correct 2 ms 8528 KB Output is correct
64 Correct 97 ms 35312 KB Output is correct
65 Correct 94 ms 35444 KB Output is correct
66 Incorrect 89 ms 31312 KB 3rd lines differ - on the 1st token, expected: '1470760606', found: '1470760592'
67 Halted 0 ms 0 KB -