답안 #70927

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
70927 2018-08-23T17:39:37 Z gnoor 고대 책들 (IOI17_books) C++17
50 / 100
306 ms 18076 KB
#include "books.h"

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

using namespace std;

bool visit[1000100];
long long disl[1000100];
long long disr[1000100];
int tol[1000100];
int tor[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;
}

struct seg {
	int cl,cr,pl,pr;
};

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

long long minimum_walk(vector<int> p, int s) {
	int n=(int)p.size();
	vector<pair<int,int>> coord;

	vector<seg> over;
	vector<seg> nonover;

	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 cl=-1e9;
		int cr=1e9;
		if (i<=s) cl=max(cl,i);
		if (i>=s) cr=min(cr,i);

		int cur=p[i];
		ans+=abs(p[i]-i);
		while (cur!=i) {
			visit[cur]=true;
			mx=max(mx,cur);
			mn=min(mn,cur);

			if (cur<=s) cl=max(cl,cur);
			if (cur>=s) cr=min(cr,cur);

			ans+=abs(p[cur]-cur);
			cur=p[cur];
		}

		if (mn==mx) continue;

		coord.push_back({mn,mx});

		if (mn>=s||mx<=s) nonover.push_back(seg{cl,cr,mn,mx});
		else over.push_back(seg{cl,cr,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;
	}

	if (over.empty()) {
		return ans;
	}

	//-------------------------------------------

	over=rem(over,nonover);

	int tgtl=-1e9;
	int tgtr=1e9;
	for (auto &i:over) {
		tgtl=max(tgtl,i.cl);
		tgtr=min(tgtr,i.cr);
	}

	memset(disl,63,sizeof(disl));
	memset(disr,63,sizeof(disr));
	int curr,curl;

	//----------------------------

	long long addr=0;
	sort(nonover.begin(),nonover.end(),[](const seg &a, const seg &b) {
			return a.cr<b.cr;
			});

	curr=s;
	curl=s;
	for (int i=0;i<(int)nonover.size();i++) {
		if (nonover[i].cr==1e9) break;
		if (nonover[i].cr>curr) {
			addr+=(nonover[i].cr-curr)*2ll;
		}
		curr=max(curr,nonover[i].pr);
		curl=min(curl,nonover[i].pl);
		disl[curl]=min(disl[curl],addr);
	}
	tol[0]=0;
	for (int i=1;i<=s;i++) {
		if (disl[i-1]<=disl[i]) {
			disl[i]=disl[i-1];
			tol[i]=tol[i-1];
		} else tol[i]=i;
	}

	//----------------------------

	long long addl=0;
	sort(nonover.begin(),nonover.end(),[](const seg &a, const seg &b) {
			return a.cl>b.cl;
			});

	curr=s;
	curl=s;
	for (int i=0;i<(int)nonover.size();i++) {
		if (nonover[i].cl==-1e9) break;
		if (nonover[i].cl<curl) {
			addl+=(curl-nonover[i].cl)*2ll;
		}
		curl=min(curl,nonover[i].pl);
		curr=max(curr,nonover[i].pr);
		disr[curr]=min(disr[curr],addl);
	}
	tor[n-1]=n-1;
	for (int i=n-2;i>=s;i--) {
		if (disr[i+1]<=disr[i]) {
			disr[i]=disr[i+1];
			tor[i]=tor[i+1];
		} else tor[i]=i;
	}

	//-------------------------
	
	addr=0;
	sort(nonover.begin(),nonover.end(),[](const seg &a, const seg &b) {
			return a.cr<b.cr;
			});

	curr=s;
	for (int i=0;i<(int)nonover.size()&&curr<tgtr;i++) {
		if (nonover[i].cr==1e9) break;
		if (nonover[i].cr>curr) {
			addr+=(nonover[i].cr-curr)*2ll;
		}
		curr=max(curr,nonover[i].pr);
		if (disr[curr]<=addr) {
			addr=disr[curr];
			curr=tor[curr];
		}
	}
	if (curr<tgtr) addr+=(tgtr-curr)*2ll;

	//--------------------

	addl=0;
	sort(nonover.begin(),nonover.end(),[](const seg &a, const seg &b) {
			return a.cl>b.cl;
			});

	curl=s;
	for (int i=0;i<(int)nonover.size()&&curl>tgtl;i++) {
		if (nonover[i].cl==-1e9) break;
		if (nonover[i].cl<curl) {
			addl+=(curl-nonover[i].cl)*2ll;
		}
		curl=min(curl,nonover[i].pl);
		if (disl[curl]<=addl) {
			addl=disl[curl];
			curl=tol[curl];
		}
	}
	if (curl>tgtl) addl+=(curl-tgtl)*2ll;

	//-------------------

	return ans+min(addl,addr);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 564 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 3 ms 564 KB Output is correct
7 Correct 2 ms 564 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 2 ms 564 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 564 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 3 ms 564 KB Output is correct
7 Correct 2 ms 564 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 2 ms 564 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 2 ms 636 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 636 KB Output is correct
24 Correct 3 ms 636 KB Output is correct
25 Correct 3 ms 636 KB Output is correct
26 Correct 4 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 636 KB Output is correct
29 Correct 3 ms 636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 564 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 3 ms 564 KB Output is correct
7 Correct 2 ms 564 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 2 ms 564 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 2 ms 636 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 636 KB Output is correct
24 Correct 3 ms 636 KB Output is correct
25 Correct 3 ms 636 KB Output is correct
26 Correct 4 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 636 KB Output is correct
29 Correct 3 ms 636 KB Output is correct
30 Correct 306 ms 9408 KB Output is correct
31 Correct 301 ms 9468 KB Output is correct
32 Correct 165 ms 9468 KB Output is correct
33 Correct 240 ms 17428 KB Output is correct
34 Correct 231 ms 17428 KB Output is correct
35 Correct 224 ms 18076 KB Output is correct
36 Correct 216 ms 18076 KB Output is correct
37 Correct 196 ms 18076 KB Output is correct
38 Correct 165 ms 18076 KB Output is correct
39 Correct 218 ms 18076 KB Output is correct
40 Correct 218 ms 18076 KB Output is correct
41 Correct 217 ms 18076 KB Output is correct
42 Correct 253 ms 18076 KB Output is correct
43 Correct 219 ms 18076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 18076 KB Output is correct
2 Correct 17 ms 18076 KB Output is correct
3 Correct 17 ms 18076 KB Output is correct
4 Correct 4 ms 18076 KB Output is correct
5 Correct 2 ms 18076 KB Output is correct
6 Incorrect 15 ms 18076 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22144'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 564 KB Output is correct
4 Correct 2 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 3 ms 564 KB Output is correct
7 Correct 2 ms 564 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 2 ms 564 KB Output is correct
10 Correct 3 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 616 KB Output is correct
13 Correct 2 ms 620 KB Output is correct
14 Correct 2 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 3 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 3 ms 620 KB Output is correct
19 Correct 2 ms 620 KB Output is correct
20 Correct 3 ms 620 KB Output is correct
21 Correct 2 ms 636 KB Output is correct
22 Correct 3 ms 636 KB Output is correct
23 Correct 3 ms 636 KB Output is correct
24 Correct 3 ms 636 KB Output is correct
25 Correct 3 ms 636 KB Output is correct
26 Correct 4 ms 636 KB Output is correct
27 Correct 3 ms 636 KB Output is correct
28 Correct 3 ms 636 KB Output is correct
29 Correct 3 ms 636 KB Output is correct
30 Correct 306 ms 9408 KB Output is correct
31 Correct 301 ms 9468 KB Output is correct
32 Correct 165 ms 9468 KB Output is correct
33 Correct 240 ms 17428 KB Output is correct
34 Correct 231 ms 17428 KB Output is correct
35 Correct 224 ms 18076 KB Output is correct
36 Correct 216 ms 18076 KB Output is correct
37 Correct 196 ms 18076 KB Output is correct
38 Correct 165 ms 18076 KB Output is correct
39 Correct 218 ms 18076 KB Output is correct
40 Correct 218 ms 18076 KB Output is correct
41 Correct 217 ms 18076 KB Output is correct
42 Correct 253 ms 18076 KB Output is correct
43 Correct 219 ms 18076 KB Output is correct
44 Correct 16 ms 18076 KB Output is correct
45 Correct 17 ms 18076 KB Output is correct
46 Correct 17 ms 18076 KB Output is correct
47 Correct 4 ms 18076 KB Output is correct
48 Correct 2 ms 18076 KB Output is correct
49 Incorrect 15 ms 18076 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22144'
50 Halted 0 ms 0 KB -