Submission #1337315

#TimeUsernameProblemLanguageResultExecution timeMemory
1337315MateiKing80Collecting Stamps 4 (JOI25_stamps4)C++20
Running #5-59
548 ms10928 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define int ll
const ll inf = 8e18;


signed main() {
	int n, x;
	cin >> n >> x;
	vector<int> a(2 * n);
	vector<ll> c(2 * n);
	for (int i = 0; i < 2 * n; i ++) {
		cin >> a[i];
		a[i] --;
	}
	for (int i = 0; i < 2 * n; i ++) {
		cin >> c[i];
	}
	
	vector<ll> b(2 * n);
	vector<array<int, 2>> pos(n, {-1, -1});
	for (int i = 0; i < 2 * n; i ++) {
		if (pos[a[i]][0] == -1) {
			pos[a[i]][0] = i;
		} else {
			pos[a[i]][1] = i;
		}
	}
	for (int i = 0; i < n; i ++) {
		b[0] += pos[i][0];
	}
	for (int i = 0; i + 1 < 2 * n; i ++) {
		b[i + 1] = b[i];
		b[i + 1] -= n - 1;
		if (i == pos[a[i]][0]) {
			b[i + 1] += pos[a[i]][1] - i - 1;
		} else {
			b[i + 1] += 2 * n - i - 1 + pos[a[i]][0];
		}
	}
	
	for (int i = 0; i < 2 * n; i ++) {
		b[i] = 1LL * n * n - b[i] + 1LL * n * (n - 1) / 2;
	}
	
	vector<int> ord(2 * n);
	for (int i = 0; i < 2 * n; i ++) {
		ord[i] = i;
	}
	sort(ord.begin(), ord.end(), [&](int i, int j) {
		return b[i] < b[j];
	});
	
	vector<ll> pref(2 * n), suf(2 * n);
	for (int i = 0; i < 2 * n; i ++) {
		suf[i] = c[ord[i]];
		pref[i] = c[ord[i]] - b[ord[i]] * x;
	}
	for (int i = 1; i < 2 * n; i ++) {
		pref[i] = min(pref[i - 1], pref[i]);
	}
	for (int i = 2 * n - 1; i; i --) {
		suf[i - 1] = min(suf[i - 1], suf[i]);
	}
	
	int q;
	cin >> q;
	
	while (q --) {
		ll k;
		cin >> k;
		int l = 0, r = 2 * n;
		while (l < r) {
			int mid = (l + r) / 2;
			if (b[ord[mid]] >= k) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		
		ll ans = inf;
		if (l < 2 * n) {
			ans = min(ans, suf[l]);
		}
		if (l) {
			ans = min(ans, pref[l - 1] + x * k);
		}

		cout << ans << '\n';
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...