Submission #542847

# Submission time Handle Problem Language Result Execution time Memory
542847 2022-03-28T09:13:57 Z zggf Ancient Books (IOI17_books) C++14
50 / 100
167 ms 34376 KB
#include "books.h"
#define f first
#define s second
#include <bits/stdc++.h>

using namespace std;

vector<pair<int, int>> tab;

void solve(int a, int b, int &na, int &nb){
	//cout<<a<<" "<<b<<" "<<na<<" "<<nb<<'\n';
	int ta = na;
	int tb = nb;

	for(int i = na; i <= a; i++){
		if(tab[i].f!=-1){
			ta = min(ta, tab[i].f);
			tb = max(tb, tab[i].s);
			//cout<<i<<" "<<tab[i].f<<" "<<tab[i].s<<'\n';
		}
	}
	for(int i = b; i <= nb; i++){
		if(tab[i].f!=-1){
			ta = min(ta, tab[i].f);
			tb = max(tb, tab[i].s);
			//cout<<i<<" "<<tab[i].f<<" "<<tab[i].s<<'\n';
		}
	}
	if(ta!=na||nb!=tb) solve(na-1, nb+1, ta, tb);
	na = ta;
	nb = tb;
}

long long minimum_walk(std::vector<int> p, int s) {
	int n = p.size();
	vector<int> odw(n);
	long long wyn = 0;
	vector<pair<int, int>> inp;
	vector<pair<int, int>> inp2;
	tab.resize(n, {-1, -1});
	int globMax=-1e9, globMin=1e9;
	for(int i = 0; i < n; i++){
		int pt = i;
		if(!odw[pt]){
			if(p[pt]!=pt){
				int mini = 1e9;
				int maxi = -1e9;
				while(!odw[pt]){
					mini = min(mini, pt);
					maxi = max(maxi, pt);
					odw[pt] = true;
					wyn+=abs(p[pt]-pt);
					pt = p[pt];	
				}
				globMax = max(globMax, maxi);
				globMin = min(globMin, mini);
				tab[i] = {mini, maxi};
				inp2.push_back({mini, -i});
				inp2.push_back({maxi, i});
				inp.push_back({mini, maxi});
			}else{
				tab[i] = {-1, -1};
			       	odw[pt] = true;
			}
		}
	}
	inp.push_back({s, s});
	sort(inp.begin(), inp.end());
	int dal = -1;
	int cA=-1, cB=-1;
	bool bylo = false;
	for(int i = 0; i < inp.size(); i++){
		while(dal>inp[i].f){
			dal = max(dal, inp[i].s);
			i++;	
			if(i>=inp.size()) break;
		}
		if(dal>=s){
			cB = dal;
			bylo = true;
		}
		if(i<inp.size()){
			if(i!=0) wyn+=2*(inp[i].f-dal);
			dal = inp[i].s;
		}
		if(!bylo) cA=inp[i].f;
	}
	int pocz=s, kon=s;
	//cout<<cA<<" "<<cB<<'\n';
	return wyn;
	while(true){
		//cout<<pocz<<" "<<kon<<'\n';
		solve(pocz, kon, pocz, kon);
		//cout<<pocz<<" "<<kon<<'\n';
		if(pocz>cA&&kon<cB){
			wyn+=2;
			pocz--;
			kon++;
		}else break;
	}
	return wyn;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:72:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for(int i = 0; i < inp.size(); i++){
      |                 ~~^~~~~~~~~~~~
books.cpp:76:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |    if(i>=inp.size()) break;
      |       ~^~~~~~~~~~~~
books.cpp:82:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |   if(i<inp.size()){
      |      ~^~~~~~~~~~~
# 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 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 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 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
# 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 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 144 ms 26576 KB Output is correct
31 Correct 166 ms 26600 KB Output is correct
32 Correct 114 ms 26588 KB Output is correct
33 Correct 122 ms 33080 KB Output is correct
34 Correct 122 ms 33108 KB Output is correct
35 Correct 127 ms 33604 KB Output is correct
36 Correct 129 ms 32720 KB Output is correct
37 Correct 115 ms 27460 KB Output is correct
38 Correct 97 ms 26716 KB Output is correct
39 Correct 111 ms 26720 KB Output is correct
40 Correct 109 ms 26620 KB Output is correct
41 Correct 167 ms 26592 KB Output is correct
42 Correct 107 ms 26700 KB Output is correct
43 Correct 130 ms 34376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 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 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 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 300 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 144 ms 26576 KB Output is correct
31 Correct 166 ms 26600 KB Output is correct
32 Correct 114 ms 26588 KB Output is correct
33 Correct 122 ms 33080 KB Output is correct
34 Correct 122 ms 33108 KB Output is correct
35 Correct 127 ms 33604 KB Output is correct
36 Correct 129 ms 32720 KB Output is correct
37 Correct 115 ms 27460 KB Output is correct
38 Correct 97 ms 26716 KB Output is correct
39 Correct 111 ms 26720 KB Output is correct
40 Correct 109 ms 26620 KB Output is correct
41 Correct 167 ms 26592 KB Output is correct
42 Correct 107 ms 26700 KB Output is correct
43 Correct 130 ms 34376 KB Output is correct
44 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -