Submission #70782

# Submission time Handle Problem Language Result Execution time Memory
70782 2018-08-23T10:56:42 Z gnoor Ancient Books (IOI17_books) C++17
50 / 100
314 ms 19052 KB
#include "books.h"

#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <cstdio>

using namespace std;

bool visit[1000100];

vector<pair<int,int>> uni(vector<pair<int,int>> &tbl) {
	sort(tbl.begin(),tbl.end(),[](const pair<int,int> &a,const pair<int,int> &b) {
			if (a.second!=b.second) return a.second<b.second;
			return a.first<b.first;
			});
	vector<pair<int,int>> re;
	for (int i=0;i<(int)tbl.size();i++) {
		int curl=tbl[i].first;
		while (!re.empty()&&tbl[i].first<=re.back().second) {
			curl=min(curl,re.back().first);
			re.pop_back();
		}
		re.push_back({curl,tbl[i].second});
	}
	return re;
}

vector<pair<int,int>> rem(vector<pair<int,int>> &tbl,vector<pair<int,int>> &out) {
	sort(tbl.begin(),tbl.end(),[](const pair<int,int> &a,const pair<int,int> &b) {
			if (a.second!=b.second) return a.second<b.second;
			return a.first>b.first;
			});
	
	vector<pair<int,int>> re;
	for (int i=0;i<(int)tbl.size();i++) {
		while (!re.empty()&&tbl[i].first<=re.back().first) {
			out.push_back(re.back());
			re.pop_back();
		}
		re.push_back(tbl[i]);
	}
	return re;
}

long long minimum_walk(vector<int> p, int s) {
	int n=(int)p.size();
	vector<pair<int,int>> coord;
	vector<pair<int,int>> nonover;
	vector<pair<int,int>> over;
	long long ans=0;
	for (int i=0;i<n;i++) {
		if (visit[i]) continue;
		visit[i]=true;
		int mn=i;
		int mx=i;
		int cur=p[i];
		ans+=abs(p[i]-i);
		while (cur!=i) {
			visit[cur]=true;
			mx=max(mx,cur);
			mn=min(mn,cur);
			ans+=abs(p[cur]-cur);
			cur=p[cur];
		}
		if (mn==mx) continue;
		coord.push_back({mn,mx});
		if (mn<=s&&s<=mx) over.push_back({mn,mx});
		else nonover.push_back({mn,mx});
		if (mn!=mx) coord.push_back({mn,mx});
		
	}
	coord.push_back({s,s});
	auto re=uni(coord);
	for (int i=1;i<(int)re.size();i++) {
		ans+=(re[i].first-re[i-1].second)*2;
	}

	auto over2=rem(over,nonover);

	int ll=(over2.empty()?s:over2.back().first);
	int rr=(over2.empty()?s:over2.front().second);

	nonover.push_back({ll,ll});
	nonover.push_back({rr,rr});
	nonover.push_back({s,s});

	auto nonover2=uni(nonover);

	long long addr=0;
	bool stflag=false;
	for (int i=0;i<(int)nonover2.size();i++) {
		if (nonover2[i].first<=s&&s<=nonover2[i].second) {
			stflag=true;
			if (nonover2[i].first<=rr&&rr<=nonover2[i].second) break;
			continue;
		}
		if (!stflag) continue;
		addr+=(nonover2[i].first-nonover2[i-1].second)*2;	
		if (nonover2[i].first<=rr&&rr<=nonover2[i].second) break;
	}
	stflag=false;
	long long addl=0;
	for (int i=(int)nonover2.size()-1;i>=0;i--) {
		if (nonover2[i].first<=s&&s<=nonover2[i].second) {
			stflag=true;
			if (nonover2[i].first<=ll&&ll<=nonover2[i].second) break;
			continue;
		}
		if (!stflag) continue;
		addl+=(nonover2[i+1].first-nonover2[i].second)*2;	
		if (nonover2[i].first<=ll&&ll<=nonover2[i].second) break;
	}
	ans+=min(addl,addr);
		
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 3 ms 528 KB Output is correct
6 Correct 4 ms 552 KB Output is correct
7 Correct 2 ms 552 KB Output is correct
8 Correct 2 ms 552 KB Output is correct
9 Correct 3 ms 572 KB Output is correct
10 Correct 3 ms 572 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 2 ms 572 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 2 ms 572 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 3 ms 528 KB Output is correct
6 Correct 4 ms 552 KB Output is correct
7 Correct 2 ms 552 KB Output is correct
8 Correct 2 ms 552 KB Output is correct
9 Correct 3 ms 572 KB Output is correct
10 Correct 3 ms 572 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 2 ms 572 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 2 ms 572 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 3 ms 632 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 3 ms 632 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 3 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 3 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 3 ms 528 KB Output is correct
6 Correct 4 ms 552 KB Output is correct
7 Correct 2 ms 552 KB Output is correct
8 Correct 2 ms 552 KB Output is correct
9 Correct 3 ms 572 KB Output is correct
10 Correct 3 ms 572 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 2 ms 572 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 2 ms 572 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 3 ms 632 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 3 ms 632 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 3 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 3 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
30 Correct 274 ms 9340 KB Output is correct
31 Correct 314 ms 9468 KB Output is correct
32 Correct 193 ms 9468 KB Output is correct
33 Correct 307 ms 18720 KB Output is correct
34 Correct 280 ms 18788 KB Output is correct
35 Correct 298 ms 19052 KB Output is correct
36 Correct 293 ms 19052 KB Output is correct
37 Correct 194 ms 19052 KB Output is correct
38 Correct 191 ms 19052 KB Output is correct
39 Correct 174 ms 19052 KB Output is correct
40 Correct 180 ms 19052 KB Output is correct
41 Correct 273 ms 19052 KB Output is correct
42 Correct 235 ms 19052 KB Output is correct
43 Correct 245 ms 19052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 19052 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3308'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 248 KB Output is correct
2 Correct 3 ms 484 KB Output is correct
3 Correct 3 ms 484 KB Output is correct
4 Correct 4 ms 488 KB Output is correct
5 Correct 3 ms 528 KB Output is correct
6 Correct 4 ms 552 KB Output is correct
7 Correct 2 ms 552 KB Output is correct
8 Correct 2 ms 552 KB Output is correct
9 Correct 3 ms 572 KB Output is correct
10 Correct 3 ms 572 KB Output is correct
11 Correct 2 ms 572 KB Output is correct
12 Correct 2 ms 572 KB Output is correct
13 Correct 3 ms 572 KB Output is correct
14 Correct 2 ms 572 KB Output is correct
15 Correct 3 ms 572 KB Output is correct
16 Correct 2 ms 572 KB Output is correct
17 Correct 3 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
19 Correct 3 ms 632 KB Output is correct
20 Correct 3 ms 632 KB Output is correct
21 Correct 3 ms 632 KB Output is correct
22 Correct 3 ms 632 KB Output is correct
23 Correct 3 ms 632 KB Output is correct
24 Correct 3 ms 648 KB Output is correct
25 Correct 2 ms 648 KB Output is correct
26 Correct 3 ms 648 KB Output is correct
27 Correct 2 ms 648 KB Output is correct
28 Correct 2 ms 648 KB Output is correct
29 Correct 3 ms 648 KB Output is correct
30 Correct 274 ms 9340 KB Output is correct
31 Correct 314 ms 9468 KB Output is correct
32 Correct 193 ms 9468 KB Output is correct
33 Correct 307 ms 18720 KB Output is correct
34 Correct 280 ms 18788 KB Output is correct
35 Correct 298 ms 19052 KB Output is correct
36 Correct 293 ms 19052 KB Output is correct
37 Correct 194 ms 19052 KB Output is correct
38 Correct 191 ms 19052 KB Output is correct
39 Correct 174 ms 19052 KB Output is correct
40 Correct 180 ms 19052 KB Output is correct
41 Correct 273 ms 19052 KB Output is correct
42 Correct 235 ms 19052 KB Output is correct
43 Correct 245 ms 19052 KB Output is correct
44 Incorrect 2 ms 19052 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3308'
45 Halted 0 ms 0 KB -