Submission #384839

# Submission time Handle Problem Language Result Execution time Memory
384839 2021-04-02T12:13:47 Z kshitij_sodani Ancient Books (IOI17_books) C++14
0 / 100
19 ms 24044 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'


#include "books.h"
llo ans=0;
llo n;
vector<llo> adj[1000001];
llo vis[1000001];
pair<llo,llo> cot[1000001];
llo par[1000001];
llo find(llo no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
vector<llo> ss;
void dfs(int no){
	vis[no]=1;
	ss.pb(no);
	for(auto j:adj[no]){
		if(vis[j]==0){
			dfs(j);
		}
	}
}
long long minimum_walk(std::vector<int> p, int s) {
	while(p.size()){
		if(p.back()+1==p.size()){
			p.pop_back();
		}
		else{
			break;
		}
	}


	n=p.size();
//cout<<p.size()<<":"<<endl;
	if(n==0){
		return 0;
	}
	for(int i=0;i<n;i++){
		adj[i].pb(p[i]);
	}
	//llo co=0;
	llo ans=0;
	vector<pair<llo,llo>> tt;
	for(int i=0;i<n;i++){
		if(vis[i]==0){
			ss.clear();
			dfs(i);
			/*if(ss.size()==0){
				continue;
			}*/
			llo mi=ss[0];
			llo ma=ss[0];
			for(auto j:ss){
				mi=min(mi,j);
				ma=max(ma,j);
			}
			for(auto j:ss){
				cot[j]={mi,ma};
			}
			tt.pb({mi,ma});
			//co++;
			
			for(int j=0;j+1<ss.size();j++){
				ans+=abs(ss[j]-ss[j+1]);
			}
		/*	for(auto j:ss){
				cout<<j<<",,";
			}
			cout<<endl;*/
			ans+=(abs(ss[0]-ss.back()));
		}
	}
	sort(tt.begin(),tt.end());
	vector<pair<llo,llo>> tt2;

	for(auto j:tt){
		if(tt2.size()==0){
			tt2.pb(j);
		}
		else{
			if(tt2.back().b<j.a){
				tt2.pb(j);
			}
			else{
				tt2[tt2.size()-1].b=max(tt2[tt2.size()-1].b,j.b);
			}
		}
	}
	pair<llo,llo> cur;
	for(auto j:tt2){
		if(s>=j.a and s<=j.b){
			cur=j;
		}
	}
	set<pair<llo,llo>> ss2;
	llo xx=tt2.size();
	ans+=2*(xx-1);
	for(int i=cur.a;i<=cur.b;i++){
		ss2.insert(cot[i]);
	}
	vector<pair<llo,llo>> yy;
	for(auto j:ss2){
		yy.pb(j);
	}
/*	for(int i=0;i<n;i++){
		cout<<cot[i].a<<"<"<<cot[i].b<<endl;
	}*/
	
	for(int j=0;j<yy.size();j++){
		par[j]=j;
	}
	for(int j=0;j<yy.size();j++){
		for(int k=j+1;k<yy.size();k++){
			if((yy[j].a<yy[k].a and yy[j].b<yy[k].b) or (yy[j].a>yy[k].a and yy[j].b>yy[k].b)){
				llo aa=find(j);
				llo bb=find(k);
				if(aa!=bb){
					par[aa]=bb;
				}
			}
		}
	}
	for(int j=0;j<yy.size();j++){
		if(par[j]==j){
			ans+=2;
		}
	}

	/*for(auto j:ss){
		llo st=1;

		for(auto k:ss){
			if(k.a!=j.a or k.b!=j.b){

				if(k.a<j.a and k.b>j.a and k.b<j.b){
					st=0;
				}

			}
		}
		ans+=st;
	}
*/

	

	
	




	//ans+=2*(co-1);

	return ans-2;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:38:16: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   if(p.back()+1==p.size()){
      |      ~~~~~~~~~~^~~~~~~~~~
books.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |    for(int j=0;j+1<ss.size();j++){
      |                ~~~^~~~~~~~~~
books.cpp:123:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |  for(int j=0;j<yy.size();j++){
      |              ~^~~~~~~~~~
books.cpp:126:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  126 |  for(int j=0;j<yy.size();j++){
      |              ~^~~~~~~~~~
books.cpp:127:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |   for(int k=j+1;k<yy.size();k++){
      |                 ~^~~~~~~~~~
books.cpp:137:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |  for(int j=0;j<yy.size();j++){
      |              ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 17 ms 23788 KB Output is correct
3 Correct 17 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 17 ms 23788 KB Output is correct
6 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 17 ms 23788 KB Output is correct
3 Correct 17 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 17 ms 23788 KB Output is correct
6 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 17 ms 23788 KB Output is correct
3 Correct 17 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 17 ms 23788 KB Output is correct
6 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 24044 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2746'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Correct 17 ms 23788 KB Output is correct
3 Correct 17 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 17 ms 23788 KB Output is correct
6 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '8', found: '10'
7 Halted 0 ms 0 KB -