Submission #289558

# Submission time Handle Problem Language Result Execution time Memory
289558 2020-09-02T18:11:08 Z eohomegrownapps Ancient Books (IOI17_books) C++14
50 / 100
259 ms 36452 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<ll> cycle;
vector<int> p;
ll n;

ll minc = 1e9;
ll maxc = -1e9;

struct UFDS{
	int n;
	vector<int> parent;
	UFDS(int _n){
		n = _n;
		parent.resize(n);
		for (int i = 0; i<n; i++){
			parent[i]=i;
		}
	}

	int root(int i){
		if (parent[i]==i){return i;}
		return parent[i]=root(parent[i]);
	}

	void connect(int a, int b){
		int ra = root(a);
		int rb = root(b);
		if (ra==rb){return;}
		parent[ra]=rb;
	}

	bool connected(int a, int b){
		return root(a) == root(b);
	}
};

ll findcycle(ll i, ll ptr){
	cycle[i] = ptr;
	minc=min(minc,i);
	maxc=max(maxc,i);
	if (cycle[p[i]]!=-1){
		return abs(i-p[i]);
	} else {
		return abs(i-p[i]) + findcycle(p[i], ptr);
	}
}

ll minimum_walk(std::vector<int> px, int s) {
	p = px;
	n = p.size();
	cycle.resize(n,-1);

	ll cycledist = 0;
	ll ptr = 0;

	vector<pair<int,int>> cycleranges;
	vector<ll> cyclewidth(n+1);
	for (ll i = 0; i<n; i++){
		if (cycle[i]!=-1){continue;}
		minc=1e9;maxc=-1e9;
		ll dist = findcycle(i,ptr);
		cycleranges.push_back({minc,maxc});
		cyclewidth[minc]++;
		cyclewidth[maxc]--;
		cycledist+=dist;
		ptr++;
	}

	ll prev = 0;
	bool iszero = true;
	ll ctr = 0;
	ll minabove=1e9;
	ll maxbelow=-1e9;
	bool isbetween = false;
	for (ll i = 0; i<=n; i++){
		ctr+=cyclewidth[i];
		/*if (ctr==0&&i==s){
			isbetween=true;
		}*/
		if (iszero&&ctr>0){
			if (i<=s){
				maxbelow=max(maxbelow,i);
			}
			if (i>=s){
				minabove=min(minabove,i);
			}
			if (prev!=0){cycledist+=2*(i-prev);}
			iszero = false;
		} else if ((!iszero)&&ctr==0){
			if (i-1<=s){
				maxbelow=max(maxbelow,i-1);
			}
			if (i-1>=s){
				minabove=min(minabove,i-1);
			}
			prev = i;
			iszero = true;
		}
	}
	//cout<<maxbelow<<' '<<minabove<<endl;
	if (maxbelow==-1e9&&minabove==1e9){
		return 0;
	} else if (!isbetween){
		cycledist+=2*(min(abs(minabove-s),abs(s-maxbelow)));
	} else {
		UFDS u(n);
		for (int i = 0; i<n-1; i++){
			for (int j = i+1; j<n; j++){
				int as = cycleranges[i].first;
				int ae = cycleranges[i].second;
				int bs = cycleranges[j].first;
				int be = cycleranges[j].second;
				if ((as<=bs&&bs<=ae&&bs<=ae&&ae<=be)||(bs<=as&&as<=be&&as<=be&&be<=ae)){
					u.connect(i,j);
				}
			}
		}
		vector<pair<int,int>> ranges(n,{1e9,-1e9});
		for (int i = 0; i<n; i++){
			int r = u.root(i);
			ranges[r].first=min(ranges[r].first,cycleranges[r].first);
			ranges[r].second=max(ranges[r].second,cycleranges[r].second);
		}
		vector<pair<int,int>> intersectingranges;
		for (auto p : ranges){
			if (p.first==1e9){continue;}
			if (p.first<=s&&s<=p.second){
				intersectingranges.push_back(p);
			}
		}
		sort(intersectingranges.begin(),intersectingranges.end());
		ll dist = 0;
		for (int i = 0; i<intersectingranges.size()-1; i++){
			dist+=min(intersectingranges[i+1].first-intersectingranges[i].first,
				intersectingranges[i].second-intersectingranges[i+1].second);
		}
		cycledist+=2*dist;
	}
	return cycledist;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:138:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |   for (int i = 0; i<intersectingranges.size()-1; i++){
      |                   ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 372 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 372 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 372 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 259 ms 28152 KB Output is correct
31 Correct 252 ms 28152 KB Output is correct
32 Correct 187 ms 36452 KB Output is correct
33 Correct 192 ms 36452 KB Output is correct
34 Correct 189 ms 36452 KB Output is correct
35 Correct 192 ms 36452 KB Output is correct
36 Correct 180 ms 30316 KB Output is correct
37 Correct 181 ms 28764 KB Output is correct
38 Correct 190 ms 28280 KB Output is correct
39 Correct 187 ms 28268 KB Output is correct
40 Correct 203 ms 28152 KB Output is correct
41 Correct 211 ms 28152 KB Output is correct
42 Correct 204 ms 28152 KB Output is correct
43 Correct 189 ms 36452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3594'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 372 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 256 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 259 ms 28152 KB Output is correct
31 Correct 252 ms 28152 KB Output is correct
32 Correct 187 ms 36452 KB Output is correct
33 Correct 192 ms 36452 KB Output is correct
34 Correct 189 ms 36452 KB Output is correct
35 Correct 192 ms 36452 KB Output is correct
36 Correct 180 ms 30316 KB Output is correct
37 Correct 181 ms 28764 KB Output is correct
38 Correct 190 ms 28280 KB Output is correct
39 Correct 187 ms 28268 KB Output is correct
40 Correct 203 ms 28152 KB Output is correct
41 Correct 211 ms 28152 KB Output is correct
42 Correct 204 ms 28152 KB Output is correct
43 Correct 189 ms 36452 KB Output is correct
44 Incorrect 1 ms 384 KB 3rd lines differ - on the 1st token, expected: '3304', found: '3594'
45 Halted 0 ms 0 KB -