답안 #283782

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283782 2020-08-26T07:16:41 Z hank55663 고대 책들 (IOI17_books) C++14
0 / 100
16 ms 24064 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.x,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){
		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.y);
			Maxi=it.y;
		}
	}
	
	//ok[Max]=1;
	if(!over){
		if(s>Max||s<v[0].x.x){
			
			Min=max(s-Max,v[0].x.x-s);
		}
		else{
			Min=0;
		}
	}
	else{
		for(int i = 0;i<p.size();i++){
			if(vis[Find(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:133:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 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 23808 KB Output is correct
6 Incorrect 15 ms 23808 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 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 23808 KB Output is correct
6 Incorrect 15 ms 23808 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 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 23808 KB Output is correct
6 Incorrect 15 ms 23808 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 24064 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 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 23808 KB Output is correct
6 Incorrect 15 ms 23808 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -