Submission #384786

# Submission time Handle Problem Language Result Execution time Memory
384786 2021-04-02T09:34:36 Z kshitij_sodani Ancient Books (IOI17_books) C++14
0 / 100
17 ms 23916 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];
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()==p.size()+1){
			p.pop_back();
		}
		else{
			break;
		}
	}

	n=p.size();
	if(n==0){
		return 0;
	}
	for(int i=0;i<n;i++){
		adj[i].pb(p[i]);
	}
	llo co=0;
	llo ans=0;
	for(int i=0;i<n;i++){
		if(vis[i]==0){
			ss.clear();
			dfs(i);
			if(ss.size()==0){
				continue;
			}
			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()));
		}
	}
	ans+=2*(co-1);

	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:29:14: 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]
   29 |   if(p.back()==p.size()+1){
      |      ~~~~~~~~^~~~~~~~~~~~
books.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |    for(int j=0;j+1<ss.size();j++){
      |                ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 23916 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4186'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23788 KB Output is correct
2 Incorrect 17 ms 23788 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -