Submission #137480

# Submission time Handle Problem Language Result Execution time Memory
137480 2019-07-28T02:28:06 Z baluteshih Ancient Books (IOI17_books) C++14
50 / 100
277 ms 24292 KB
#include "books.h"
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int boss[1000005],mx[1000005],mi[1000005],bln[1000005];

int finds(int x)
{
	if(boss[x]==x) return x;
	return boss[x]=finds(boss[x]);
}

void Union(int a,int b)
{
	a=finds(a),b=finds(b);
	boss[a]=b,mx[b]=max(mx[b],mx[a]),mi[b]=min(mi[b],mi[a]);
}

long long minimum_walk(vector<int> p, int s) 
{
	ll ans=0,ls=-1,L=0,R=0;
	for(int i=0;i<p.size();++i) boss[i]=i,mx[i]=i,mi[i]=i;
	for(int i=0;i<p.size();++i)
		Union(i,p[i]);
	for(int i=0;i<p.size();++i)
		ans+=abs(p[i]-i);
	if(ans==0) return 0;
	for(int i=0;i<p.size();)
		if(finds(i),mx[boss[i]]==mi[boss[i]]) ++i;
		else
		{
			if(~ls) ans+=(mi[boss[i]]-ls)*2;
			for(ls=mx[finds(i)];i<=ls;bln[i]=ls,++i)
				if(mx[finds(i)]>ls)
					Union(ls,i),ls=mx[finds(i)];
		}
	finds(s);
	if(mi[boss[s]]==mx[boss[s]])
	{
		for(int i=s;p[i]==i;++i)
			++R;
		if(R==p.size()-s) R=-1;
		for(int i=s;p[i]==i;--i)
			++L;
		if(L==s+1) L=-1;
		if(!~L) ans+=R*2;
		else if(!~R) ans+=L*2;
	}
	else
	{
		for(int i=s;finds(i)!=finds(bln[i]);++i)
			++R;
		for(int i=s;finds(i)!=finds(bln[i]);--i)
			++L;
		ans+=min(L*2,R*2);
	}
	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:35:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<p.size();++i) boss[i]=i,mx[i]=i,mi[i]=i;
              ~^~~~~~~~~
books.cpp:36:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<p.size();++i)
              ~^~~~~~~~~
books.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<p.size();++i)
              ~^~~~~~~~~
books.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<p.size();)
              ~^~~~~~~~~
books.cpp:55:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(R==p.size()-s) R=-1;
      ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 296 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 296 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 380 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 296 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 380 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 264 ms 24292 KB Output is correct
31 Correct 277 ms 24184 KB Output is correct
32 Correct 150 ms 20344 KB Output is correct
33 Correct 164 ms 24228 KB Output is correct
34 Correct 163 ms 24184 KB Output is correct
35 Correct 167 ms 24184 KB Output is correct
36 Correct 178 ms 24160 KB Output is correct
37 Correct 195 ms 24160 KB Output is correct
38 Correct 186 ms 24132 KB Output is correct
39 Correct 189 ms 24164 KB Output is correct
40 Correct 211 ms 24160 KB Output is correct
41 Correct 249 ms 24128 KB Output is correct
42 Correct 245 ms 24184 KB Output is correct
43 Correct 165 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 376 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 296 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 3 ms 380 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 264 ms 24292 KB Output is correct
31 Correct 277 ms 24184 KB Output is correct
32 Correct 150 ms 20344 KB Output is correct
33 Correct 164 ms 24228 KB Output is correct
34 Correct 163 ms 24184 KB Output is correct
35 Correct 167 ms 24184 KB Output is correct
36 Correct 178 ms 24160 KB Output is correct
37 Correct 195 ms 24160 KB Output is correct
38 Correct 186 ms 24132 KB Output is correct
39 Correct 189 ms 24164 KB Output is correct
40 Correct 211 ms 24160 KB Output is correct
41 Correct 249 ms 24128 KB Output is correct
42 Correct 245 ms 24184 KB Output is correct
43 Correct 165 ms 24184 KB Output is correct
44 Incorrect 2 ms 376 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3590'
45 Halted 0 ms 0 KB -