Submission #953092

# Submission time Handle Problem Language Result Execution time Memory
953092 2024-03-25T13:19:39 Z waldi Ancient Books (IOI17_books) C++17
50 / 100
1365 ms 141708 KB
#include "books.h"
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int((x).size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;

ll minimum_walk(vector<int> perm, int start){
	int n = ssize(perm);
	ll wyn = 0ll;
	REP(i, n) wyn += abs(i-perm[i]);
	
	set<int> ciekawe;
	REP(i, n) if(perm[i] != i) ciekawe.emplace(i);
	
	vector<set<int>> cykle;
	vector<int> lewo, prawo;
	vector<int> odw(n, -1);
	REP(i, n) if(odw[i] < 0){
		int mini = i, maks = i;
		set<int> s;
		for(int w = i; odw[w] < 0; w = perm[w]){
			odw[w] = ssize(cykle);
			s.emplace(w);
			mini = min(mini, w);
			maks = max(maks, w);
		}
		cykle.emplace_back(s);
		lewo.emplace_back(mini);
		prawo.emplace_back(maks);
	}
	
	//~ REP(i, n) printf("%d ", odw[i]);
	//~ printf("\n");
	//~ for(vector<int> t : cykle){
		//~ for(int i : t) printf("%d ", i);
		//~ printf("\n");
	//~ }
	
	bool cokolwiek = 0;
	int l = start, p = start;
	while(ssize(ciekawe)){
		auto it = ciekawe.lower_bound(l);
		if(it != ciekawe.end() && *it <= p){
			l = min(l, lewo[odw[*it]]);
			p = max(p, prawo[odw[*it]]);
			ciekawe.erase(it);
			continue;
		}
		
		if(cokolwiek){
			if(it != ciekawe.end()) wyn += (*it-p)<<1, p = *it;
			else wyn += (l-*prev(it))<<1, l = *prev(it);
			continue;
		}
		
		if(l <= *ciekawe.begin() || *prev(ciekawe.end()) <= p){
			cokolwiek = 1;
			continue;
		}
		
		bool git = 0;
		int koszt_lewo = 0;
		int nowe_lewo = l;
		int iter = l;
		while(iter > 1){
			if(iter == nowe_lewo) --nowe_lewo, ++koszt_lewo;
			--iter;
			if(iter != perm[iter]){
				nowe_lewo = lewo[odw[iter]];
				if(prawo[odw[iter]] > p){git = 1; break;}
			}
		}
		if(!git){
			cokolwiek = 1;
			continue;
		}
		
		git = 0;
		int koszt_prawo = 0;
		int nowe_prawo = p;
		iter = p;
		while(iter < n){
			if(iter == nowe_prawo) ++nowe_prawo, ++koszt_prawo;
			++iter;
			if(iter != perm[iter]){
				nowe_prawo = prawo[odw[iter]];
				if(lewo[odw[iter]] < l){git = 1; break;}
			}
		}
		if(!git){
			cokolwiek = 1;
			continue;
		}
		
		if(koszt_lewo < koszt_prawo) wyn += koszt_lewo<<1, l = nowe_lewo;
		else wyn += koszt_prawo<<1, p = nowe_prawo;
	}
	return wyn;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 424 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 424 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1365 ms 132348 KB Output is correct
31 Correct 1078 ms 113392 KB Output is correct
32 Correct 201 ms 115076 KB Output is correct
33 Correct 319 ms 124644 KB Output is correct
34 Correct 322 ms 125416 KB Output is correct
35 Correct 391 ms 141708 KB Output is correct
36 Correct 438 ms 115436 KB Output is correct
37 Correct 482 ms 108900 KB Output is correct
38 Correct 487 ms 106380 KB Output is correct
39 Correct 500 ms 106288 KB Output is correct
40 Correct 556 ms 106020 KB Output is correct
41 Correct 790 ms 108788 KB Output is correct
42 Correct 664 ms 106372 KB Output is correct
43 Correct 311 ms 125416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3316'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 600 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 424 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1365 ms 132348 KB Output is correct
31 Correct 1078 ms 113392 KB Output is correct
32 Correct 201 ms 115076 KB Output is correct
33 Correct 319 ms 124644 KB Output is correct
34 Correct 322 ms 125416 KB Output is correct
35 Correct 391 ms 141708 KB Output is correct
36 Correct 438 ms 115436 KB Output is correct
37 Correct 482 ms 108900 KB Output is correct
38 Correct 487 ms 106380 KB Output is correct
39 Correct 500 ms 106288 KB Output is correct
40 Correct 556 ms 106020 KB Output is correct
41 Correct 790 ms 108788 KB Output is correct
42 Correct 664 ms 106372 KB Output is correct
43 Correct 311 ms 125416 KB Output is correct
44 Incorrect 1 ms 344 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3316'
45 Halted 0 ms 0 KB -