Submission #289572

# Submission time Handle Problem Language Result Execution time Memory
289572 2020-09-02T18:27:45 Z eohomegrownapps Ancient Books (IOI17_books) C++14
50 / 100
237 ms 36068 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);
	ll minv = 1e9;
	ll maxv = -1e9;
	for (ll i = 0; i<n; i++){
		if (cycle[i]!=-1){continue;}
		minc=1e9;maxc=-1e9;
		ll dist = findcycle(i,ptr);
		if (dist!=0){
			minv=min(minv,minc);
			maxv=max(maxv,maxc);
		}
		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 (s<=minv||s>=maxv){
		return cycledist+2*min(minv-s,maxv-s);
	} else if (!isbetween){
		UFDS u(cycleranges.size());
		for (int i = 0; i<cycleranges.size()-1; i++){
			for (int j = i+1; j<cycleranges.size(); 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(cycleranges.size(),{1e9,-1e9});
		for (int i = 0; i<cycleranges.size(); 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());
		/*for (auto p : intersectingranges){
			cout<<p.first<<' '<<p.second<<endl;
		}*/
		ll dist = 0;
		for (int i = 0; i<intersectingranges.size()-1; i++){
			dist+=min(abs(intersectingranges[i+1].first-intersectingranges[i].first),
				abs(intersectingranges[i].second-intersectingranges[i+1].second));
		}
		//cout<<dist<<endl;
		cycledist+=2*dist;
	}
	return cycledist;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:118: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]
  118 |   for (int i = 0; i<cycleranges.size()-1; i++){
      |                   ~^~~~~~~~~~~~~~~~~~~~~
books.cpp:119:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |    for (int j = i+1; j<cycleranges.size(); j++){
      |                      ~^~~~~~~~~~~~~~~~~~~
books.cpp:130: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]
  130 |   for (int i = 0; i<cycleranges.size(); i++){
      |                   ~^~~~~~~~~~~~~~~~~~~
books.cpp:147: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]
  147 |   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 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 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 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 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 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 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 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 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 0 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 0 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 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 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 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 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 0 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 0 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 237 ms 27768 KB Output is correct
31 Correct 237 ms 27896 KB Output is correct
32 Correct 175 ms 36068 KB Output is correct
33 Correct 183 ms 36068 KB Output is correct
34 Correct 181 ms 36016 KB Output is correct
35 Correct 182 ms 36068 KB Output is correct
36 Correct 172 ms 29932 KB Output is correct
37 Correct 174 ms 28352 KB Output is correct
38 Correct 172 ms 27896 KB Output is correct
39 Correct 173 ms 27768 KB Output is correct
40 Correct 179 ms 27768 KB Output is correct
41 Correct 198 ms 27772 KB Output is correct
42 Correct 190 ms 27768 KB Output is correct
43 Correct 183 ms 36068 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 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 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 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 0 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 0 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 0 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 237 ms 27768 KB Output is correct
31 Correct 237 ms 27896 KB Output is correct
32 Correct 175 ms 36068 KB Output is correct
33 Correct 183 ms 36068 KB Output is correct
34 Correct 181 ms 36016 KB Output is correct
35 Correct 182 ms 36068 KB Output is correct
36 Correct 172 ms 29932 KB Output is correct
37 Correct 174 ms 28352 KB Output is correct
38 Correct 172 ms 27896 KB Output is correct
39 Correct 173 ms 27768 KB Output is correct
40 Correct 179 ms 27768 KB Output is correct
41 Correct 198 ms 27772 KB Output is correct
42 Correct 190 ms 27768 KB Output is correct
43 Correct 183 ms 36068 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 -