Submission #283834

# Submission time Handle Problem Language Result Execution time Memory
283834 2020-08-26T07:46:57 Z hank55663 Ancient Books (IOI17_books) C++14
70 / 100
2000 ms 491768 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define x first
#define y second
int f[1000005];
int l[1000005];
int r[1000005];
int Find(int x){
	if(f[x]==x)return x;
	return f[x]=Find(f[x]);
}
int pre[1000005];
vector<pii> edge[4000005];
int dis[4000005];
int ok[1000005];
int vis[4000005];
int mindex;
struct node{
	node *l,*r;
	//list<int> v;
	int num;
	int a,b;
	node(int _a,int _b):a(_a),b(_b),l(NULL),r(NULL){
		num=mindex++;
	}
}*root;
void build(node *n){
	if(n->a==n->b){
		edge[n->num].pb(mp(n->a,0));
		return;
	}
	int mid=(n->a+n->b)/2;
	n->l=new node(n->a,mid);
	n->r=new node(mid+1,n->b);
	edge[n->num].push_back(mp(n->l->num,0));
	edge[n->num].push_back(mp(n->r->num,0));
	build(n->l);
	build(n->r);
}
void go(node *n,int l,int r,int x){
	if(n->a>=l&&n->b<=r){
		edge[x].push_back(mp(n->num,0));
		return ;
	}
	if(n->b<l||n->a>r)return;
	go(n->l,l,r,x);
	go(n->r,l,r,x);
}
long long minimum_walk(std::vector<int> p, int s) {
//	vector<pii> v;
	mindex=p.size();
	long long ans=0;
	int Min=1e9;
	int over=0;
	for(int i = 0;i<p.size();i++){
		if(p[i]!=i){
//			v.pb(mp(p[i],i));
			ans+=abs(p[i]-i);
		//	Min=min(Min,abs(i-s));
			if((i<s&&p[i]>s)||(i>s&&p[i]<s))over=1;
		}
		l[i]=1e9;
		r[i]=0;
		f[i]=i;
	}
	for(int i = 0;i<p.size();i++){
		f[Find(p[i])]=Find(i);
	}
	for(int i = 0;i<p.size();i++){
		l[Find(i)]=min(l[Find(i)],i);
		r[Find(i)]=max(r[Find(i)],i);
	}
	vector<pii> v;
	root = new node(0,p.size()-1);
	build(root);
	for(int i = 0;i<p.size();i++){
		if(f[i]==i&&l[i]!=r[i]){
			v.pb(mp(l[i],r[i]));
		}
	}
	if(v.empty())return 0;
	sort(v.begin(),v.end());
	int Max=v[0].y;
	ok[v[0].x]=1;
	for(auto it:v){
		if(Max<it.x){
			ok[Max]=1;
			ok[it.x]=1;
		}
		ans+=max(0,(it.x-Max)*2);
		Max=max(Max,it.y);
	}
	ok[Max]=1;
	if(!over){
	//	printf("?\n");
		if(s>Max||s<v[0].x){
			
			Min=max(s-Max,v[0].x-s);
		}
		else{
			//printf("%d ?\n",ans);
			Min=0;
		}
	}
	else{
		for(int i = 0;i<p.size();i++){
			if(p[i]<i){
				go(root,p[i],i,i);
			}
			if(p[i]>i){
				go(root,i,p[i],i);
			}
			if(i!=0){
				edge[i].pb(mp(i-1,1));
			}
			if(i!=p.size()){
				edge[i].pb(mp(i+1,1));
			}
		}
		deque<int> q;
		q.pb(s);
		fill(dis,dis+mindex,1e9);
		dis[s]=0;
		fill(vis,vis+mindex,0);
		while(!q.empty()){
			int x=q.front();
			q.pop_front();
			if(vis[x])continue;
			vis[x]=1;
			for(auto it:edge[x]){
				if(it.y==0){
					if(dis[it.x]>dis[x]){
						dis[it.x]=dis[x];
						q.push_front(it.x);
					}
				}
				else{
					if(dis[it.x]>dis[x]+1){
						dis[it.x]=dis[x]+1;
						q.push_back(it.x);
					}
				}
			}
		}
		for(int i = 0;i<p.size();i++){
			if(ok[i]){
				Min=min(Min,dis[i]);
			}
		}
	}
//	printf("%d\n",Min);
	return ans+Min*2;
}
/*
8
7 3 4 1 2 5 6 0
*/

Compilation message

books.cpp: In constructor 'node::node(int, int)':
books.cpp:26:8: warning: 'node::b' will be initialized after [-Wreorder]
   26 |  int a,b;
      |        ^
books.cpp:23:8: warning:   'node* node::l' [-Wreorder]
   23 |  node *l,*r;
      |        ^
books.cpp:27:2: warning:   when initialized here [-Wreorder]
   27 |  node(int _a,int _b):a(_a),b(_b),l(NULL),r(NULL){
      |  ^~~~
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:59:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:70:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:80:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:110:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
books.cpp:120:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |    if(i!=p.size()){
      |       ~^~~~~~~~~~
books.cpp:149:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 64 ms 94348 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 63 ms 94328 KB Output is correct
5 Correct 65 ms 94332 KB Output is correct
6 Correct 64 ms 94328 KB Output is correct
7 Correct 65 ms 94328 KB Output is correct
8 Correct 66 ms 94456 KB Output is correct
9 Correct 64 ms 94328 KB Output is correct
10 Correct 63 ms 94328 KB Output is correct
11 Correct 63 ms 94296 KB Output is correct
12 Correct 66 ms 94328 KB Output is correct
13 Correct 64 ms 94328 KB Output is correct
14 Correct 62 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 64 ms 94328 KB Output is correct
18 Correct 62 ms 94328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 64 ms 94348 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 63 ms 94328 KB Output is correct
5 Correct 65 ms 94332 KB Output is correct
6 Correct 64 ms 94328 KB Output is correct
7 Correct 65 ms 94328 KB Output is correct
8 Correct 66 ms 94456 KB Output is correct
9 Correct 64 ms 94328 KB Output is correct
10 Correct 63 ms 94328 KB Output is correct
11 Correct 63 ms 94296 KB Output is correct
12 Correct 66 ms 94328 KB Output is correct
13 Correct 64 ms 94328 KB Output is correct
14 Correct 62 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 64 ms 94328 KB Output is correct
18 Correct 62 ms 94328 KB Output is correct
19 Correct 67 ms 94456 KB Output is correct
20 Correct 67 ms 94456 KB Output is correct
21 Correct 71 ms 94440 KB Output is correct
22 Correct 66 ms 94456 KB Output is correct
23 Correct 64 ms 94456 KB Output is correct
24 Correct 64 ms 94456 KB Output is correct
25 Correct 63 ms 94432 KB Output is correct
26 Correct 69 ms 94456 KB Output is correct
27 Correct 64 ms 94584 KB Output is correct
28 Correct 66 ms 94456 KB Output is correct
29 Correct 66 ms 94456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 64 ms 94348 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 63 ms 94328 KB Output is correct
5 Correct 65 ms 94332 KB Output is correct
6 Correct 64 ms 94328 KB Output is correct
7 Correct 65 ms 94328 KB Output is correct
8 Correct 66 ms 94456 KB Output is correct
9 Correct 64 ms 94328 KB Output is correct
10 Correct 63 ms 94328 KB Output is correct
11 Correct 63 ms 94296 KB Output is correct
12 Correct 66 ms 94328 KB Output is correct
13 Correct 64 ms 94328 KB Output is correct
14 Correct 62 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 64 ms 94328 KB Output is correct
18 Correct 62 ms 94328 KB Output is correct
19 Correct 67 ms 94456 KB Output is correct
20 Correct 67 ms 94456 KB Output is correct
21 Correct 71 ms 94440 KB Output is correct
22 Correct 66 ms 94456 KB Output is correct
23 Correct 64 ms 94456 KB Output is correct
24 Correct 64 ms 94456 KB Output is correct
25 Correct 63 ms 94432 KB Output is correct
26 Correct 69 ms 94456 KB Output is correct
27 Correct 64 ms 94584 KB Output is correct
28 Correct 66 ms 94456 KB Output is correct
29 Correct 66 ms 94456 KB Output is correct
30 Correct 612 ms 272572 KB Output is correct
31 Correct 634 ms 272504 KB Output is correct
32 Correct 506 ms 272664 KB Output is correct
33 Correct 586 ms 278224 KB Output is correct
34 Correct 538 ms 278164 KB Output is correct
35 Correct 542 ms 278336 KB Output is correct
36 Correct 543 ms 277840 KB Output is correct
37 Correct 547 ms 276996 KB Output is correct
38 Correct 551 ms 275032 KB Output is correct
39 Correct 554 ms 274896 KB Output is correct
40 Correct 566 ms 273072 KB Output is correct
41 Correct 576 ms 272632 KB Output is correct
42 Correct 573 ms 272632 KB Output is correct
43 Correct 542 ms 274848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94584 KB Output is correct
2 Correct 65 ms 94584 KB Output is correct
3 Correct 65 ms 94588 KB Output is correct
4 Correct 63 ms 94456 KB Output is correct
5 Correct 64 ms 94460 KB Output is correct
6 Correct 65 ms 94584 KB Output is correct
7 Correct 64 ms 94584 KB Output is correct
8 Correct 65 ms 94584 KB Output is correct
9 Correct 64 ms 94584 KB Output is correct
10 Correct 64 ms 94456 KB Output is correct
11 Correct 64 ms 94584 KB Output is correct
12 Correct 64 ms 94712 KB Output is correct
13 Correct 65 ms 94584 KB Output is correct
14 Correct 64 ms 94584 KB Output is correct
15 Correct 64 ms 94584 KB Output is correct
16 Correct 65 ms 94584 KB Output is correct
17 Correct 65 ms 94584 KB Output is correct
18 Correct 66 ms 94532 KB Output is correct
19 Correct 64 ms 94584 KB Output is correct
20 Correct 65 ms 94584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 94328 KB Output is correct
2 Correct 64 ms 94348 KB Output is correct
3 Correct 63 ms 94328 KB Output is correct
4 Correct 63 ms 94328 KB Output is correct
5 Correct 65 ms 94332 KB Output is correct
6 Correct 64 ms 94328 KB Output is correct
7 Correct 65 ms 94328 KB Output is correct
8 Correct 66 ms 94456 KB Output is correct
9 Correct 64 ms 94328 KB Output is correct
10 Correct 63 ms 94328 KB Output is correct
11 Correct 63 ms 94296 KB Output is correct
12 Correct 66 ms 94328 KB Output is correct
13 Correct 64 ms 94328 KB Output is correct
14 Correct 62 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 64 ms 94328 KB Output is correct
18 Correct 62 ms 94328 KB Output is correct
19 Correct 67 ms 94456 KB Output is correct
20 Correct 67 ms 94456 KB Output is correct
21 Correct 71 ms 94440 KB Output is correct
22 Correct 66 ms 94456 KB Output is correct
23 Correct 64 ms 94456 KB Output is correct
24 Correct 64 ms 94456 KB Output is correct
25 Correct 63 ms 94432 KB Output is correct
26 Correct 69 ms 94456 KB Output is correct
27 Correct 64 ms 94584 KB Output is correct
28 Correct 66 ms 94456 KB Output is correct
29 Correct 66 ms 94456 KB Output is correct
30 Correct 612 ms 272572 KB Output is correct
31 Correct 634 ms 272504 KB Output is correct
32 Correct 506 ms 272664 KB Output is correct
33 Correct 586 ms 278224 KB Output is correct
34 Correct 538 ms 278164 KB Output is correct
35 Correct 542 ms 278336 KB Output is correct
36 Correct 543 ms 277840 KB Output is correct
37 Correct 547 ms 276996 KB Output is correct
38 Correct 551 ms 275032 KB Output is correct
39 Correct 554 ms 274896 KB Output is correct
40 Correct 566 ms 273072 KB Output is correct
41 Correct 576 ms 272632 KB Output is correct
42 Correct 573 ms 272632 KB Output is correct
43 Correct 542 ms 274848 KB Output is correct
44 Correct 64 ms 94584 KB Output is correct
45 Correct 65 ms 94584 KB Output is correct
46 Correct 65 ms 94588 KB Output is correct
47 Correct 63 ms 94456 KB Output is correct
48 Correct 64 ms 94460 KB Output is correct
49 Correct 65 ms 94584 KB Output is correct
50 Correct 64 ms 94584 KB Output is correct
51 Correct 65 ms 94584 KB Output is correct
52 Correct 64 ms 94584 KB Output is correct
53 Correct 64 ms 94456 KB Output is correct
54 Correct 64 ms 94584 KB Output is correct
55 Correct 64 ms 94712 KB Output is correct
56 Correct 65 ms 94584 KB Output is correct
57 Correct 64 ms 94584 KB Output is correct
58 Correct 64 ms 94584 KB Output is correct
59 Correct 65 ms 94584 KB Output is correct
60 Correct 65 ms 94584 KB Output is correct
61 Correct 66 ms 94532 KB Output is correct
62 Correct 64 ms 94584 KB Output is correct
63 Correct 65 ms 94584 KB Output is correct
64 Correct 977 ms 346828 KB Output is correct
65 Correct 971 ms 343836 KB Output is correct
66 Correct 1586 ms 409732 KB Output is correct
67 Correct 1690 ms 423224 KB Output is correct
68 Correct 195 ms 122060 KB Output is correct
69 Correct 229 ms 123256 KB Output is correct
70 Correct 180 ms 121848 KB Output is correct
71 Correct 162 ms 119928 KB Output is correct
72 Correct 149 ms 118624 KB Output is correct
73 Correct 258 ms 131396 KB Output is correct
74 Correct 545 ms 282732 KB Output is correct
75 Correct 642 ms 282604 KB Output is correct
76 Correct 1196 ms 361756 KB Output is correct
77 Correct 1177 ms 363948 KB Output is correct
78 Correct 1221 ms 359468 KB Output is correct
79 Correct 1219 ms 359452 KB Output is correct
80 Correct 549 ms 277112 KB Output is correct
81 Correct 1848 ms 491768 KB Output is correct
82 Correct 1682 ms 491636 KB Output is correct
83 Correct 1298 ms 366832 KB Output is correct
84 Correct 1293 ms 367596 KB Output is correct
85 Correct 1363 ms 372892 KB Output is correct
86 Correct 1437 ms 385656 KB Output is correct
87 Correct 1220 ms 403564 KB Output is correct
88 Correct 1238 ms 381712 KB Output is correct
89 Correct 1265 ms 367716 KB Output is correct
90 Correct 1575 ms 407388 KB Output is correct
91 Correct 1684 ms 424184 KB Output is correct
92 Execution timed out 2130 ms 450296 KB Time limit exceeded