Submission #109642

# Submission time Handle Problem Language Result Execution time Memory
109642 2019-05-07T08:48:58 Z Kepperoni Ancient Books (IOI17_books) C++14
0 / 100
3 ms 384 KB
#include "books.h"
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

const ll MAXN = 1e6 + 10;

vector<pii> pi;
bool vis[MAXN];
ll n;
ll ans;
bool done[MAXN];
ll nr[MAXN], nl[MAXN];

long long minimum_walk(std::vector<int> p, int s) {
	n = p.size();
	for(int i=0; i<n; i++){
		if(!vis[i]){
			ll cl = i, cr = i;
			ll cu = i;
			while(!vis[i]){
				cl = min(cu, cl);
				cr = max(cu, cr);
				vis[cu] = 1;
				ans += abs(cu - p[cu]);
				cu = p[cu];
			}
			pi.pb({cl, cr});
		}
	}

	for(int i=0; i<n; i++) nr[i] = nl[i] = -1;

	ll lar=0, fil = n;
	ll cl = s, cr = s;
	for(int i=0; i<pi.size(); i++){
		if(pi[i].x <= s && s <= pi[i].y){
			cl = min(cl, pi[i].x);
			cr = max(cr, pi[i].y);
		}

		nr[pi[i].x] = pi[i].y;
		nl[pi[i].y] = pi[i].x;

		if(pi[i].x != pi[i].y){
			lar = max(lar, pi[i].y);
			fil = min(fil, pi[i].x);
		}
	}

	for(int i=cl; i<=lar; i++){
		if(i > cr){
			ans += 2;
			cr++;
		}

		if(nr[i] != -1)
			cr = max(cr, nr[i]);
	}

	for(int i=cr; i>=fil; i--){
		if(i < cl){
			ans += 2;
			cl--;
		}

		if(nl[i] != -1)
			cl = min(cl, nl[i]);
	}

	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<pi.size(); i++){
               ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 384 KB 3rd lines differ - on the 1st token, expected: '6', found: '4'
2 Halted 0 ms 0 KB -