답안 #782462

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782462 2023-07-14T02:22:06 Z GusterGoose27 고대 책들 (IOI17_books) C++17
0 / 100
1 ms 468 KB
#include "books.h"

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e6+5;

typedef long long ll;
typedef pair<int, int> pii;
bool vis[MAXN];
int n;

int _abs(int v) {
	return v < 0 ? -v : v;
}

ll minimum_walk(vector<int> p, int s) {
	ll ans = 0;
	n = p.size();
	for (int i = 0; i < n; i++) ans += _abs(p[i]-i);
	if (ans == 0) return 0;
	vector<pii> intervals;
	for (int i = 0; i < n; i++) {
		if (vis[i]) continue;
		int cur = i;
		int l = i, r = i;
		do {
			l = min(l, cur);
			r = max(r, cur);
			vis[cur] = 1;
			cur = p[cur];
		} while (cur != i);
		if (r > l) intervals.push_back(pii(l, r));
	}
	sort(intervals.begin(), intervals.end()); // they are probably already sorted
	int curr = intervals[0].second;
	int mn = 0;
	if (intervals[0].first <= s && s <= intervals[0].second) mn = n;
	if (s < intervals[0].first) {
		mn = n;
		assert(false);
	}
	for (int i = 1; i < intervals.size(); i++) {
		if (intervals[i].first <= s && s <= intervals[i].second) mn = n;
		assert(curr != intervals[i].first);
		if (curr > intervals[i].first) {
			curr = max(curr, intervals[i].second);
			continue;
		}
		ans += 2*(intervals[i].first-curr);
		curr = intervals[i].second;
	}
	if (s > curr) {
		mn = n;
		assert(false);
	}
	// assert(mn == 0);
	for (int i = 0; i < n; i++)
		if (p[i] != i) mn = min(mn, _abs(i-s));
	return ans+2*mn;
}

Compilation message

books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:44: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]
   44 |  for (int i = 1; i < intervals.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -