답안 #283794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283794 2020-08-26T07:22:08 Z hank55663 고대 책들 (IOI17_books) C++14
50 / 100
601 ms 200056 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++){
      |                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23784 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 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 17 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 17 ms 23784 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 14 ms 23808 KB Output is correct
17 Correct 14 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23784 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 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 17 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 17 ms 23784 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 14 ms 23808 KB Output is correct
17 Correct 14 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23936 KB Output is correct
20 Correct 15 ms 23936 KB Output is correct
21 Correct 14 ms 24064 KB Output is correct
22 Correct 18 ms 24064 KB Output is correct
23 Correct 14 ms 24064 KB Output is correct
24 Correct 15 ms 24064 KB Output is correct
25 Correct 15 ms 23936 KB Output is correct
26 Correct 16 ms 24064 KB Output is correct
27 Correct 18 ms 23936 KB Output is correct
28 Correct 18 ms 23936 KB Output is correct
29 Correct 14 ms 23936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23784 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 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 17 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 17 ms 23784 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 14 ms 23808 KB Output is correct
17 Correct 14 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23936 KB Output is correct
20 Correct 15 ms 23936 KB Output is correct
21 Correct 14 ms 24064 KB Output is correct
22 Correct 18 ms 24064 KB Output is correct
23 Correct 14 ms 24064 KB Output is correct
24 Correct 15 ms 24064 KB Output is correct
25 Correct 15 ms 23936 KB Output is correct
26 Correct 16 ms 24064 KB Output is correct
27 Correct 18 ms 23936 KB Output is correct
28 Correct 18 ms 23936 KB Output is correct
29 Correct 14 ms 23936 KB Output is correct
30 Correct 601 ms 172696 KB Output is correct
31 Correct 521 ms 172628 KB Output is correct
32 Correct 342 ms 200056 KB Output is correct
33 Correct 400 ms 197352 KB Output is correct
34 Correct 370 ms 197480 KB Output is correct
35 Correct 375 ms 193896 KB Output is correct
36 Correct 377 ms 185448 KB Output is correct
37 Correct 349 ms 179188 KB Output is correct
38 Correct 359 ms 177272 KB Output is correct
39 Correct 347 ms 177012 KB Output is correct
40 Correct 349 ms 173940 KB Output is correct
41 Correct 430 ms 172904 KB Output is correct
42 Correct 361 ms 173048 KB Output is correct
43 Correct 403 ms 195156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 24064 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23808 KB Output is correct
2 Correct 15 ms 23808 KB Output is correct
3 Correct 15 ms 23808 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 15 ms 23784 KB Output is correct
6 Correct 15 ms 23808 KB Output is correct
7 Correct 15 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 17 ms 23808 KB Output is correct
11 Correct 15 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 17 ms 23784 KB Output is correct
14 Correct 14 ms 23808 KB Output is correct
15 Correct 15 ms 23808 KB Output is correct
16 Correct 14 ms 23808 KB Output is correct
17 Correct 14 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23936 KB Output is correct
20 Correct 15 ms 23936 KB Output is correct
21 Correct 14 ms 24064 KB Output is correct
22 Correct 18 ms 24064 KB Output is correct
23 Correct 14 ms 24064 KB Output is correct
24 Correct 15 ms 24064 KB Output is correct
25 Correct 15 ms 23936 KB Output is correct
26 Correct 16 ms 24064 KB Output is correct
27 Correct 18 ms 23936 KB Output is correct
28 Correct 18 ms 23936 KB Output is correct
29 Correct 14 ms 23936 KB Output is correct
30 Correct 601 ms 172696 KB Output is correct
31 Correct 521 ms 172628 KB Output is correct
32 Correct 342 ms 200056 KB Output is correct
33 Correct 400 ms 197352 KB Output is correct
34 Correct 370 ms 197480 KB Output is correct
35 Correct 375 ms 193896 KB Output is correct
36 Correct 377 ms 185448 KB Output is correct
37 Correct 349 ms 179188 KB Output is correct
38 Correct 359 ms 177272 KB Output is correct
39 Correct 347 ms 177012 KB Output is correct
40 Correct 349 ms 173940 KB Output is correct
41 Correct 430 ms 172904 KB Output is correct
42 Correct 361 ms 173048 KB Output is correct
43 Correct 403 ms 195156 KB Output is correct
44 Incorrect 17 ms 24064 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
45 Halted 0 ms 0 KB -