Submission #283788

# Submission time Handle Problem Language Result Execution time Memory
283788 2020-08-26T07:20:01 Z hank55663 Ancient Books (IOI17_books) C++14
50 / 100
619 ms 199928 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];
struct node{
	node *l,*r;
	list<int> v;
	int a,b;
	node(int _a,int _b):a(_a),b(_b),l(NULL),r(NULL){
		v.clear();
	}
}*root;
void build(node *n){
	if(n->a==n->b)return;
	int mid=(n->a+n->b)/2;
	n->l=new node(n->a,mid);
	n->r=new node(mid+1,n->b);
	build(n->l);
	build(n->r);
}
void add(node *n,int l,int r,int k){
	if(n->a>=l&&n->b<=r){
		n->v.push_back(k);
		return;
	}
	if(n->a<l||n->a>r)return;
	add(n->l,l,r,k);
	add(n->r,l,r,k);
}
int vis[1000005];
vector<int> vv[1000005];
queue<int> q;
void go(node *n,int x){
	//if(n->a>=x&&n->b<=x){
		for(auto it:n->v){
			if(!vis[it]){
				q.push(it);
				vis[it]=1;
			}
		}
		n->v.clear();
	if(n->a==n->b)return;
	//}
	int mid=(n->a+n->b)/2;
	if(x<mid)go(n->l,x);
	else go(n->r,x);
}
long long minimum_walk(std::vector<int> p, int s) {
//	vector<pii> v;
	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);
		vv[Find(i)].push_back(i);
	}
	vector<pair<pii,int> > 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(mp(l[i],r[i]),i));
			add(root,l[i],r[i],i);
		}
	}
	if(v.empty())return 0;
	sort(v.begin(),v.end());
	int Max=v[0].x.y,Maxi=v[0].y;
	q.push(v[0].y);
	vis[v[0].y]=1;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(auto it:vv[x]){
			go(root,it);
		}
	}
	//ok[v[0].x.x]=1;
	for(auto it:v){
	//	printf("%d %d %d\n",it.x.x,it.x.y,ans);
		if(it.x.x>Max){
			if(!vis[Maxi]){
				vis[Maxi]=1;
				q.push(Maxi);
			}
			vis[it.y]=1;
			q.push(it.y);
		}
		ans+=max(0,(it.x.x-Max)*2);
		if(it.x.y>Max){
			Max=max(Max,it.x.y);
			Maxi=it.y;
		}
	}
	
	//ok[Max]=1;
	if(!over){
	//	printf("?\n");
		if(s>Max||s<v[0].x.x){
			
			Min=max(s-Max,v[0].x.x-s);
		}
		else{
			//printf("%d ?\n",ans);
			Min=0;
		}
	}
	else{
		for(int i = 0;i<p.size();i++){
			if(vis[Find(i)]){
				printf("%d\n",i);
				Min=min(Min,abs(i-s));
			}
		}
	}
	return ans+Min*2;
}

Compilation message

books.cpp: In constructor 'node::node(int, int)':
books.cpp:20:8: warning: 'node::b' will be initialized after [-Wreorder]
   20 |  int a,b;
      |        ^
books.cpp:18:8: warning:   'node* node::l' [-Wreorder]
   18 |  node *l,*r;
      |        ^
books.cpp:21:2: warning:   when initialized here [-Wreorder]
   21 |  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:65:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:76:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:79:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:87:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:136:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 15 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23936 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 18 ms 23784 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 15 ms 23808 KB Output is correct
13 Correct 15 ms 23936 KB Output is correct
14 Correct 15 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 15 ms 23808 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 15 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 15 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23936 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 18 ms 23784 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 15 ms 23808 KB Output is correct
13 Correct 15 ms 23936 KB Output is correct
14 Correct 15 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 15 ms 23808 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 15 ms 23808 KB Output is correct
19 Correct 18 ms 23936 KB Output is correct
20 Correct 16 ms 23936 KB Output is correct
21 Correct 16 ms 24020 KB Output is correct
22 Correct 16 ms 24064 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 18 ms 24064 KB Output is correct
25 Correct 15 ms 24064 KB Output is correct
26 Correct 15 ms 23936 KB Output is correct
27 Correct 16 ms 23936 KB Output is correct
28 Correct 16 ms 23936 KB Output is correct
29 Correct 15 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 15 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23936 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 18 ms 23784 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 15 ms 23808 KB Output is correct
13 Correct 15 ms 23936 KB Output is correct
14 Correct 15 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 15 ms 23808 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 15 ms 23808 KB Output is correct
19 Correct 18 ms 23936 KB Output is correct
20 Correct 16 ms 23936 KB Output is correct
21 Correct 16 ms 24020 KB Output is correct
22 Correct 16 ms 24064 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 18 ms 24064 KB Output is correct
25 Correct 15 ms 24064 KB Output is correct
26 Correct 15 ms 23936 KB Output is correct
27 Correct 16 ms 23936 KB Output is correct
28 Correct 16 ms 23936 KB Output is correct
29 Correct 15 ms 24064 KB Output is correct
30 Correct 619 ms 172656 KB Output is correct
31 Correct 564 ms 172800 KB Output is correct
32 Correct 368 ms 199928 KB Output is correct
33 Correct 400 ms 197400 KB Output is correct
34 Correct 398 ms 197476 KB Output is correct
35 Correct 399 ms 193772 KB Output is correct
36 Correct 395 ms 185448 KB Output is correct
37 Correct 375 ms 179188 KB Output is correct
38 Correct 370 ms 177400 KB Output is correct
39 Correct 375 ms 177144 KB Output is correct
40 Correct 381 ms 173820 KB Output is correct
41 Correct 446 ms 172784 KB Output is correct
42 Correct 386 ms 173192 KB Output is correct
43 Correct 392 ms 195056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 15 ms 24064 KB secret mismatch
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 15 ms 23936 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23808 KB Output is correct
6 Correct 15 ms 23936 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23808 KB Output is correct
10 Correct 18 ms 23784 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 15 ms 23808 KB Output is correct
13 Correct 15 ms 23936 KB Output is correct
14 Correct 15 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 15 ms 23808 KB Output is correct
17 Correct 15 ms 23808 KB Output is correct
18 Correct 15 ms 23808 KB Output is correct
19 Correct 18 ms 23936 KB Output is correct
20 Correct 16 ms 23936 KB Output is correct
21 Correct 16 ms 24020 KB Output is correct
22 Correct 16 ms 24064 KB Output is correct
23 Correct 17 ms 24064 KB Output is correct
24 Correct 18 ms 24064 KB Output is correct
25 Correct 15 ms 24064 KB Output is correct
26 Correct 15 ms 23936 KB Output is correct
27 Correct 16 ms 23936 KB Output is correct
28 Correct 16 ms 23936 KB Output is correct
29 Correct 15 ms 24064 KB Output is correct
30 Correct 619 ms 172656 KB Output is correct
31 Correct 564 ms 172800 KB Output is correct
32 Correct 368 ms 199928 KB Output is correct
33 Correct 400 ms 197400 KB Output is correct
34 Correct 398 ms 197476 KB Output is correct
35 Correct 399 ms 193772 KB Output is correct
36 Correct 395 ms 185448 KB Output is correct
37 Correct 375 ms 179188 KB Output is correct
38 Correct 370 ms 177400 KB Output is correct
39 Correct 375 ms 177144 KB Output is correct
40 Correct 381 ms 173820 KB Output is correct
41 Correct 446 ms 172784 KB Output is correct
42 Correct 386 ms 173192 KB Output is correct
43 Correct 392 ms 195056 KB Output is correct
44 Incorrect 15 ms 24064 KB secret mismatch
45 Halted 0 ms 0 KB -