답안 #42991

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
42991 2018-03-07T15:10:38 Z alenam0161 전선 연결 (IOI17_wiring) C++14
13 / 100
54 ms 10820 KB
#include "wiring.h"
#include <iostream>
#include <algorithm>
using namespace std;
vector<long long> pre, suf;
vector<pair<long long, int>> all;
int n;
long long pre_gum(int l, int r) {
	if (l > r)return 0;
	if (l == 0) {
		return pre[r] - (r - l + 1)*all[0].first;
	}
	return pre[r] - pre[l - 1] - all[l].first * (r - l + 1);
}
long long suf_gum(int l, int r) {
	if (l > r)return 0;
	return suf[l] - suf[r + 1] - (all[n - 1].first - all[r].first) * (r - l + 1);
}
long long sum(int l, int mid, int r) {
	return suf_gum(l, mid) + pre_gum(mid + 1, r) + 1ll * max(mid - l + 1, r - mid)*(all[mid + 1].first - all[mid].first);
}
long long min_total_length(std::vector<int> r, std::vector<int> b) {
	for (int to : r)all.push_back({ to,1 });
	for (int to : b)all.push_back({ to,2 });
	int n = all.size();
	::n = n;
	sort(all.begin(), all.end());
	vector<int> left(n + 1, 0);
	for (int i = 0; i < n; ++i) {
		if (i == 0 || (all[i].second != all[i - 1].second)) {
			left[i] = i;
		}
		else left[i] = left[i - 1];
	}
	pre.resize(n + 5, 0);
	suf.resize(n + 5, 0);
	for (int i = 0; i < n; ++i) {
		pre[i] = (i == 0 ? 0 : pre[i - 1]) + all[i].first;
		//	cout << "pre[" << i << "] = " << pre[i] << endl;
	}
	for (int i = n - 1; i >= 0; i--) {
		suf[i] = (i == n - 1 ? 0 : suf[i + 1]) + all[n - 1].first - all[i].first;
	}
	vector<long long> dp(n + 5, (long long)1e18);
	int pos = 0;
	long long mn;
	int lf;
	for (int i = 0; i < n; ++i) {
		if (left[i] == 0)continue;
		if (left[i] == i) {
			mn = dp[i - 1];
			lf = left[i] - 1;
			mn = dp[lf];
			for (int j = lf; j >= left[lf]; --j) {
				if (j == 0)mn = 0;
				else mn = min(mn, dp[j - 1]);
				if (mn + sum(j, lf, i) < dp[i]) {
					pos = j;
					dp[i] = mn + sum(j, lf, i);
				}
			}

		}
		else {
			if (pos == left[lf])dp[i] = mn + sum(left[lf], lf, i);
			else {
				if (sum(pos, lf, i) + mn < sum(pos - 1, lf, i) + min(mn, dp[pos - 2])) {
					pos--;
					mn = min(mn, dp[pos - 2]);
				}
				dp[i] = mn + sum(pos, lf, i);

			}
		}
	}
	return dp[n - 1];
}

Compilation message

wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:47:6: warning: 'lf' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int lf;
      ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB 3rd lines differ - on the 1st token, expected: '25859', found: '32551'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 484 KB Output is correct
2 Correct 1 ms 484 KB Output is correct
3 Correct 38 ms 7964 KB Output is correct
4 Correct 42 ms 8112 KB Output is correct
5 Correct 49 ms 8156 KB Output is correct
6 Correct 51 ms 10704 KB Output is correct
7 Correct 54 ms 10704 KB Output is correct
8 Correct 51 ms 10820 KB Output is correct
9 Correct 54 ms 10820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 10820 KB Output is correct
2 Incorrect 2 ms 10820 KB 3rd lines differ - on the 1st token, expected: '17703', found: '18114'
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 10820 KB 3rd lines differ - on the 1st token, expected: '27', found: '24'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 376 KB 3rd lines differ - on the 1st token, expected: '25859', found: '32551'
2 Halted 0 ms 0 KB -