Submission #105031

# Submission time Handle Problem Language Result Execution time Memory
105031 2019-04-10T08:33:15 Z bert30702 Ancient Books (IOI17_books) C++17
0 / 100
32 ms 31656 KB
#include <bits/stdc++.h>
#include "books.h"
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
const int MX = 1e6 + 100;
int boss[MX];
int Find(int u) {
	return boss[u] == u ? u : boss[u] = Find(boss[u]);
}
void merge(int a, int b) {
	boss[Find(a)] = Find(b);
}
priority_queue<int> pq[MX];
long long minimum_walk(vector<int> p, int s) {
	long long sum = 0;
	for(int i = 0; i < p.size(); i ++) while(pq[i].size()) pq[i].pop();
	for(int i = 0; i < p.size(); i ++) boss[i] = i;
	for(int i = 0; i < p.size(); i ++) sum += abs(i - p[i]), merge(i, p[i]);
	for(int i = 0; i < p.size(); i ++) pq[Find(i)].push(i); 
	multiset<pii> ms;
	for(int i = 0; i < p.size(); i ++) {
		if(i == Find(i)) ms.insert({pq[i].top(), i}); 
	}
	long long ans = 1ll << 60;
	for(int i = p.size() - 1; i >= 0; i --) {
		int a = (--ms.end()) -> F;
		int b = (ms.begin()) -> F;
		if((a > s and b > s) or (a < s and b < s)) {
			ans = min(ans, sum + max(abs(s - a), abs(s - b)) * 2);
		}
		else {
			ans = min(ans, sum + (a - b) * 2);
		}
		if((--ms.end()) -> F == i) {
			int ptr = (--ms.end()) -> S;
			ms.erase(--ms.end());
			pq[ptr].pop();
			if(pq[ptr].size()) ms.insert({pq[ptr].top(), ptr});
			else return ans;
		}
	}
}
//main () {
//	cout << minimun_walk({0, 2, 3, 1}, 0);
//}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < p.size(); i ++) while(pq[i].size()) pq[i].pop();
                 ~~^~~~~~~~~~
books.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < p.size(); i ++) boss[i] = i;
                 ~~^~~~~~~~~~
books.cpp:20:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < p.size(); i ++) sum += abs(i - p[i]), merge(i, p[i]);
                 ~~^~~~~~~~~~
books.cpp:21:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < p.size(); i ++) pq[Find(i)].push(i); 
                 ~~^~~~~~~~~~
books.cpp:23:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < p.size(); i ++) {
                 ~~^~~~~~~~~~
books.cpp:44:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31616 KB Output is correct
2 Incorrect 31 ms 31616 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 31 ms 31616 KB Output is correct
2 Incorrect 31 ms 31616 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 31 ms 31616 KB Output is correct
2 Incorrect 31 ms 31616 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 32 ms 31656 KB 3rd lines differ - on the 1st token, expected: '3304', found: '4734'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31616 KB Output is correct
2 Incorrect 31 ms 31616 KB 3rd lines differ - on the 1st token, expected: '6', found: '8'
3 Halted 0 ms 0 KB -