Submission #392849

# Submission time Handle Problem Language Result Execution time Memory
392849 2021-04-22T00:27:43 Z faresbasbs Ancient Books (IOI17_books) C++14
0 / 100
1 ms 332 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;
long long n,ans,mini,maxi,num,cyc[1000001],L[1000001],R[1000001];
map<pair<int,int>,long long> mp;
bool seen[1000001];
vector<int> p;

void dfs(long long curr){
	seen[curr] = 1 , L[num] = min(L[curr],curr) , R[num] = max(R[curr],curr) , cyc[curr] = num;
	mini = min(mini,curr) , maxi = max(maxi,curr);
	if(!seen[p[curr]]){
		dfs(p[curr]);
	}
}

void extend(long long &l , long long &r){
	long long ll = min({l,L[cyc[l]],L[cyc[r]]}) , rr = max({r,R[cyc[l]],R[cyc[r]]});
	while(ll < l || r < rr){
		if(ll < l){
			l -= 1;
			ll = min(ll,L[cyc[l]]);
			rr = max(rr,R[cyc[l]]);
		}else{
			r += 1;
			ll = min(ll,L[cyc[r]]);
			rr = max(rr,R[cyc[r]]);
		}
	}
}

long long getans(long long l , long long r){
	extend(l,r);
	if(mp.find({l,r}) != mp.end()){
		return mp[{l,r}];
	}
	long long nl , nr , ret = 1000000000000000000;
	if(l){
		nl = l-1 , nr = r , extend(nl,nr);
		ret = min(ret,2+getans(nl,nr));
	}
	if(r+1 < n){
		nl = l , nr = r+1 , extend(nl,nr);
		ret = min(ret,2+getans(nl,nr));
	}
	return mp[{l,r}] = ret;
}

long long minimum_walk(vector<int> P , int s){
	p = P , n = p.size() , mini = maxi = s;
	for(long long i = 0 ; i < n ; i += 1){
		ans += abs(p[i]-i);
		if(seen[i]){
			continue;
		}
		if(p[i] == i){
			L[num] = R[num] = i;
			cyc[i] = num;
			num += 1;
			continue;
		}
		L[num] = R[num] = i;
		dfs(i);
		num += 1;
	}
	mp[{mini,maxi}] = 0;
	return ans+getans(s,s);
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2746'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Incorrect 1 ms 332 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
4 Halted 0 ms 0 KB -