Submission #283829

# Submission time Handle Problem Language Result Execution time Memory
283829 2020-08-26T07:42:41 Z erd1 Ancient Books (IOI17_books) C++14
Compilation error
0 ms 0 KB
#include "books.h"

#include<bits/stdc++.h>

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef pair<int, int> pii;
typedef long long lld;
typedef long double ld;

template<typename Iter>
ostream& outIt(ostream& out, Iter b, Iter e){
	for(Iter i = b; i != e; ++i)
		out << (i == b?"":" ") << *i;
	return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> p){
	return out << '(' << p.ff << ", " << p.ss << ')';
}
template<typename T>
ostream& operator<<(ostream& out, vector<T> v){
	return outIt(out << '[', all(v)) << ']';
}
vector<pii> cs;
set<int> poss;
void add(vector<int>& p, int i){
	if(poss.count(i))return;
	poss.insert(i);
	add(p, p[i]);
}
long long minimum_walk(vector<int> p, int s) {
	lld ans = 0;
	int pmin = INT_MAX, pmax = INT_MIN;
	for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
	if(ans == 0)return 0;
	sort(all(cs));
	int r = cs.front().ff;
	add(p, r);
	bool fl = true;
	for(int i = 0; i < cs.size(); i++){
		if(cs[i].ff > s && s > r)fl = false;
		if(cs[i].ff > r) add(p, r), add(p, cs[i].ff);
		ans += 2*max(0, cs[i].ff - r);
		r = max(r, cs[i].ss);
	}
	add(p, r);
	for(int i = 0; i < p.size(); i++){
		if(s.count(i))continue;
		if(s.lower_bound(i) != s.lower_bound(p[i]))add(p, i);
	}
	if(fl){
		int l = s, r = s;
		while(l >= 0 && !poss.count(l))l--;
		while(r < p.size() && !poss.count(r))r++;
		if(l < 0)ans += 2*(r-s);
		else if(r == p.size()) ans += 2*(s-l);
		else ans += 2*min(r-s, s-l);
	}

	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  for(int i = 0; i < p.size(); i++)if(p[i] != i)ans += abs(p[i]-i), cs.pb({min(i, p[i]), max(i, p[i])}), pmin = min(pmin, i), pmax = max(pmax, i);
      |                 ~~^~~~~~~~~~
books.cpp:45:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |  for(int i = 0; i < cs.size(); i++){
      |                 ~~^~~~~~~~~~~
books.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |  for(int i = 0; i < p.size(); i++){
      |                 ~~^~~~~~~~~~
books.cpp:53:8: error: request for member 'count' in 's', which is of non-class type 'int'
   53 |   if(s.count(i))continue;
      |        ^~~~~
books.cpp:54:8: error: request for member 'lower_bound' in 's', which is of non-class type 'int'
   54 |   if(s.lower_bound(i) != s.lower_bound(p[i]))add(p, i);
      |        ^~~~~~~~~~~
books.cpp:54:28: error: request for member 'lower_bound' in 's', which is of non-class type 'int'
   54 |   if(s.lower_bound(i) != s.lower_bound(p[i]))add(p, i);
      |                            ^~~~~~~~~~~
books.cpp:59:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   while(r < p.size() && !poss.count(r))r++;
      |         ~~^~~~~~~~~~
books.cpp:61:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   else if(r == p.size()) ans += 2*(s-l);
      |           ~~^~~~~~~~~~~