Submission #757241

# Submission time Handle Problem Language Result Execution time Memory
757241 2023-06-12T20:48:50 Z alexander707070 Ancient Books (IOI17_books) C++14
0 / 100
1 ms 340 KB
#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
 
int n,perm[MAXN],len[MAXN],br,cnt,last;
bool li[MAXN];
long long sum,add[MAXN],ans;

void dfs(int x){
	li[x]=true;
	sum+=abs(x-perm[x]); br++;
	if(!li[perm[x]])dfs(perm[x]);
}

long long minimum_walk(vector<int> p, int s){
	n=int(p.size());

	for(int i=0;i<n;i++)perm[i]=p[i];

	for(int i=0;i<n;i++){
		if(!li[i]){
			sum=br=0; 
			dfs(i); add[i]=sum; len[i]=br;
			if(len[i]>1)last=i;
		}
	}


	cnt=n;
	for(int i=0;i<=last;i++){
		ans+=add[i]; cnt-=len[i];
		
		if(cnt==0){
			ans+=i; break;
		}
		ans++;
	}

	return ans;
}

/*
int main(){
	cout<<minimum_walk({0, 2, 3, 1}, 0)<<"\n";
}
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '4', found: '5'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '4', found: '5'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '4', found: '5'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3737'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 340 KB 3rd lines differ - on the 1st token, expected: '4', found: '5'
6 Halted 0 ms 0 KB -