답안 #52190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52190 2018-06-24T14:51:59 Z 김세빈(#1331) 고대 책들 (IOI17_books) C++11
50 / 100
620 ms 56920 KB
#include "books.h"

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

vector <int> V[1010101], X;
vector <pii> P;
int T[3030303];
int L[1010101], R[1010101];
int chk[1010101], K[1010101];
int n,cnt,sz;
ll ans;

int abs(int x)
{
	return x > 0? x : -x;
}

int find_min(int l,int r)
{
	l += sz, r += sz;
	int ret = 0;
	
	for(;l<r;){
		if((l & 1) && L[ret] > L[T[l]]) ret = T[l];
		if((~r & 1) && L[ret] > L[T[r]]) ret = T[r];
		l = ++l >> 1;
		r = --r >> 1;
	}
	if(l == r && L[ret] > L[T[l]]) ret = T[l];
	
	return ret;
}

void insert(int p,int v)
{
	p += sz;
	T[p] = v;
	
	for(p>>=1;p;p>>=1){
		if(L[T[p<<1]] < L[T[p<<1|1]]) T[p] = T[p<<1];
		else T[p] = T[p<<1|1];
	}
}
 
bool comp(int a,int b)
{
	return L[a] < L[b];
}

ll minimum_walk(vector <int> p, int s)
{
	int i,t,m,k,f;
	
	n = p.size();
	
	for(sz=1;sz<n;sz<<=1);
	
	for(i=0;i<n;i++) if(!chk[i] && i != p[i]){
		cnt ++;
		chk[i] = cnt;
		V[cnt].push_back(i);
		L[cnt] = R[cnt] = i;
		K[cnt] = cnt;
		
		ans += abs(i - p[i]);
		
		for(int j=p[i];j!=i;j=p[j]){
			chk[j] = cnt;
			V[cnt].push_back(j);
			R[cnt] = max(R[cnt], j);
			
			ans += abs(j - p[j]);
		}
	}
	
	L[0] = n+1;
	
	sort(K+1, K+cnt+1, comp);
	
	for(i=1;i<=cnt;i++){
		t = K[i];
		f = find_min(L[t], R[t]);
		
		if(f){
			for(int j: V[t]){
				V[f].push_back(j);
				R[f] = max(R[f], j);
				chk[j] = f;
				insert(j, f);
			}
			V[t].clear();
		}
		else{
			for(int j: V[t]){
				insert(j, t);
			}
		}
	}
	
	for(i=1;i<=cnt;i++){
		t = K[i];
		if(V[t].empty()) continue;
		f = find_min(L[t], R[t]);
		
		if(L[f] < L[t]){
			for(int j: V[t]){
				V[f].push_back(j);
				R[f] = max(R[f], j);
				chk[j] = f;
				insert(j, f);
			}
			V[t].clear();
		}
	}
	
	for(i=1;i<=cnt;i++){
		t = K[i];
		if(!V[t].empty()){
			P.push_back(pii(L[t], R[t]));
			if(L[t] <= s && s <= R[t]){
				sort(V[t].begin(), V[t].end());
				X.push_back(t);
			}
		}
	}
	
	sort(P.begin(), P.end());
	
	if(P.size()){
		t = P[0].first;
		
		for(i=k=0;i<P.size();i++){
			if(P[i].second < t) continue;
			ans += 2 * (P[i].first - t);
			t = max(t, P[i].second);
		}
	}
	
	if(!X.empty()){
		for(i=0;i<X.size()-1;i++){
			k += min(abs(*(lower_bound(V[X[i]].begin(), V[X[i]].end(), L[X[i+1]])-1) - L[X[i+1]]), abs(*upper_bound(V[X[i]].begin(), V[X[i]].end(), R[X[i+1]]) - R[X[i+1]]));
		}
		k += min(abs(*(lower_bound(V[X[i]].begin(), V[X[i]].end(), s)-1) - s), abs(*upper_bound(V[X[i]].begin(), V[X[i]].end(), s) - s));
	}
	else{
		k = n+1;
		bool f1, f2;
		f1 = f2 = 0;
		for(i=0;i<P.size();i++){
			if(P[i].first >= s) f1 = 1;
			if(P[i].second <= s) f2 = 1;
		}
		if(f1 && f2) k = 0;
		else{
			k = n+1;
			for(i=0;i<P.size();i++) k = min(k, min(abs(P[i].second - s), abs(P[i].first - s)));
			if(k > n) k = 0;
		}
	}
	
	return ans + k * 2;
}

Compilation message

books.cpp: In function 'int find_min(int, int)':
books.cpp:31:5: warning: operation on 'l' may be undefined [-Wsequence-point]
   l = ++l >> 1;
   ~~^~~~~~~~~~
books.cpp:32:5: warning: operation on 'r' may be undefined [-Wsequence-point]
   r = --r >> 1;
   ~~^~~~~~~~~~
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:137:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=k=0;i<P.size();i++){
             ~^~~~~~~~~
books.cpp:145:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0;i<X.size()-1;i++){
           ~^~~~~~~~~~~
books.cpp:154:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(i=0;i<P.size();i++){
           ~^~~~~~~~~
books.cpp:161:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(i=0;i<P.size();i++) k = min(k, min(abs(P[i].second - s), abs(P[i].first - s)));
            ~^~~~~~~~~
books.cpp:57:10: warning: unused variable 'm' [-Wunused-variable]
  int i,t,m,k,f;
          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24056 KB Output is correct
2 Correct 24 ms 24164 KB Output is correct
3 Correct 22 ms 24236 KB Output is correct
4 Correct 22 ms 24236 KB Output is correct
5 Correct 22 ms 24236 KB Output is correct
6 Correct 22 ms 24236 KB Output is correct
7 Correct 24 ms 24236 KB Output is correct
8 Correct 26 ms 24236 KB Output is correct
9 Correct 26 ms 24236 KB Output is correct
10 Correct 25 ms 24236 KB Output is correct
11 Correct 22 ms 24352 KB Output is correct
12 Correct 21 ms 24352 KB Output is correct
13 Correct 22 ms 24352 KB Output is correct
14 Correct 23 ms 24352 KB Output is correct
15 Correct 27 ms 24368 KB Output is correct
16 Correct 21 ms 24368 KB Output is correct
17 Correct 24 ms 24368 KB Output is correct
18 Correct 25 ms 24396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24056 KB Output is correct
2 Correct 24 ms 24164 KB Output is correct
3 Correct 22 ms 24236 KB Output is correct
4 Correct 22 ms 24236 KB Output is correct
5 Correct 22 ms 24236 KB Output is correct
6 Correct 22 ms 24236 KB Output is correct
7 Correct 24 ms 24236 KB Output is correct
8 Correct 26 ms 24236 KB Output is correct
9 Correct 26 ms 24236 KB Output is correct
10 Correct 25 ms 24236 KB Output is correct
11 Correct 22 ms 24352 KB Output is correct
12 Correct 21 ms 24352 KB Output is correct
13 Correct 22 ms 24352 KB Output is correct
14 Correct 23 ms 24352 KB Output is correct
15 Correct 27 ms 24368 KB Output is correct
16 Correct 21 ms 24368 KB Output is correct
17 Correct 24 ms 24368 KB Output is correct
18 Correct 25 ms 24396 KB Output is correct
19 Correct 25 ms 24396 KB Output is correct
20 Correct 24 ms 24396 KB Output is correct
21 Correct 23 ms 24396 KB Output is correct
22 Correct 22 ms 24396 KB Output is correct
23 Correct 22 ms 24396 KB Output is correct
24 Correct 21 ms 24396 KB Output is correct
25 Correct 26 ms 24396 KB Output is correct
26 Correct 27 ms 24396 KB Output is correct
27 Correct 24 ms 24396 KB Output is correct
28 Correct 24 ms 24396 KB Output is correct
29 Correct 21 ms 24396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24056 KB Output is correct
2 Correct 24 ms 24164 KB Output is correct
3 Correct 22 ms 24236 KB Output is correct
4 Correct 22 ms 24236 KB Output is correct
5 Correct 22 ms 24236 KB Output is correct
6 Correct 22 ms 24236 KB Output is correct
7 Correct 24 ms 24236 KB Output is correct
8 Correct 26 ms 24236 KB Output is correct
9 Correct 26 ms 24236 KB Output is correct
10 Correct 25 ms 24236 KB Output is correct
11 Correct 22 ms 24352 KB Output is correct
12 Correct 21 ms 24352 KB Output is correct
13 Correct 22 ms 24352 KB Output is correct
14 Correct 23 ms 24352 KB Output is correct
15 Correct 27 ms 24368 KB Output is correct
16 Correct 21 ms 24368 KB Output is correct
17 Correct 24 ms 24368 KB Output is correct
18 Correct 25 ms 24396 KB Output is correct
19 Correct 25 ms 24396 KB Output is correct
20 Correct 24 ms 24396 KB Output is correct
21 Correct 23 ms 24396 KB Output is correct
22 Correct 22 ms 24396 KB Output is correct
23 Correct 22 ms 24396 KB Output is correct
24 Correct 21 ms 24396 KB Output is correct
25 Correct 26 ms 24396 KB Output is correct
26 Correct 27 ms 24396 KB Output is correct
27 Correct 24 ms 24396 KB Output is correct
28 Correct 24 ms 24396 KB Output is correct
29 Correct 21 ms 24396 KB Output is correct
30 Correct 620 ms 49752 KB Output is correct
31 Correct 566 ms 51548 KB Output is correct
32 Correct 168 ms 51548 KB Output is correct
33 Correct 268 ms 54896 KB Output is correct
34 Correct 264 ms 55024 KB Output is correct
35 Correct 294 ms 56736 KB Output is correct
36 Correct 331 ms 56748 KB Output is correct
37 Correct 265 ms 56748 KB Output is correct
38 Correct 312 ms 56748 KB Output is correct
39 Correct 270 ms 56748 KB Output is correct
40 Correct 334 ms 56748 KB Output is correct
41 Correct 451 ms 56748 KB Output is correct
42 Correct 451 ms 56748 KB Output is correct
43 Correct 359 ms 56920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 56920 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3588'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24056 KB Output is correct
2 Correct 24 ms 24164 KB Output is correct
3 Correct 22 ms 24236 KB Output is correct
4 Correct 22 ms 24236 KB Output is correct
5 Correct 22 ms 24236 KB Output is correct
6 Correct 22 ms 24236 KB Output is correct
7 Correct 24 ms 24236 KB Output is correct
8 Correct 26 ms 24236 KB Output is correct
9 Correct 26 ms 24236 KB Output is correct
10 Correct 25 ms 24236 KB Output is correct
11 Correct 22 ms 24352 KB Output is correct
12 Correct 21 ms 24352 KB Output is correct
13 Correct 22 ms 24352 KB Output is correct
14 Correct 23 ms 24352 KB Output is correct
15 Correct 27 ms 24368 KB Output is correct
16 Correct 21 ms 24368 KB Output is correct
17 Correct 24 ms 24368 KB Output is correct
18 Correct 25 ms 24396 KB Output is correct
19 Correct 25 ms 24396 KB Output is correct
20 Correct 24 ms 24396 KB Output is correct
21 Correct 23 ms 24396 KB Output is correct
22 Correct 22 ms 24396 KB Output is correct
23 Correct 22 ms 24396 KB Output is correct
24 Correct 21 ms 24396 KB Output is correct
25 Correct 26 ms 24396 KB Output is correct
26 Correct 27 ms 24396 KB Output is correct
27 Correct 24 ms 24396 KB Output is correct
28 Correct 24 ms 24396 KB Output is correct
29 Correct 21 ms 24396 KB Output is correct
30 Correct 620 ms 49752 KB Output is correct
31 Correct 566 ms 51548 KB Output is correct
32 Correct 168 ms 51548 KB Output is correct
33 Correct 268 ms 54896 KB Output is correct
34 Correct 264 ms 55024 KB Output is correct
35 Correct 294 ms 56736 KB Output is correct
36 Correct 331 ms 56748 KB Output is correct
37 Correct 265 ms 56748 KB Output is correct
38 Correct 312 ms 56748 KB Output is correct
39 Correct 270 ms 56748 KB Output is correct
40 Correct 334 ms 56748 KB Output is correct
41 Correct 451 ms 56748 KB Output is correct
42 Correct 451 ms 56748 KB Output is correct
43 Correct 359 ms 56920 KB Output is correct
44 Incorrect 22 ms 56920 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3588'
45 Halted 0 ms 0 KB -