题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie has challenged Farmer John to a game involving milk buckets! There are $N$ $(2 \leq N \leq 2 \cdot 10^5)$ milk buckets lined up in a row. The $i$-th bucket from the left initially contains $a_i$ $(0 \leq a_i \leq 10^9)$ gallons of milk.
The game consists of two phases:
Phase 1: Farmer John may swap any two adjacent buckets. He may perform as many swaps as he likes, but each swap costs 1 coin.
Phase 2: After swapping, Farmer John performs the following operation until only one bucket is left: Choose two adjacent buckets with milk amounts $a_i$ and $a_{i+1}$, and replace both buckets with one bucket containing $\frac{a_i+a_{i+1}}2$ gallons of milk in their place.
Your goal is to determine the minimum number of coins Farmer John must spend in the swapping phase to maximize the amount of milk in the final bucket after all merges are complete.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains one integer $T$ $(1 \leq T \leq 100)$: the number of independent test cases.Then, for each test case, the first line contains an integer $N$: the number of milk buckets. The second line contains $N$ integers $a_1, a_2, \dots, a_N$, separated by spaces: the number of gallons of milk in each bucket.
It is guaranteed that the sum of $N$ over all test cases does not exceed $5 \cdot 10^5.$
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output the minimum number of coins Farmer John must spend to maximize the amount of milk in the final bucket.SAMPLE INPUT:
2 3 0 0 1 3 0 1 0
SAMPLE OUTPUT:
0 1
For the first test, we do not need to swap any milk buckets in the first phase. In the second phase, Farmer John can merge the first two buckets and then merge the only two buckets left to achieve a final amount of 0.5. It can be shown that this final amount is maximal.
For the second test, we must perform a singular swap of the first two milk buckets in the first stage to achieve a final amount of 0.5 in the second stage. It can be shown that we cannot achieve a final amount of 0.5 without swaps in the first stage.
SAMPLE INPUT:
4 4 9 4 9 2 6 0 0 2 0 0 0 3 2 0 1 9 3 3 3 10 3 2 13 14 13
SAMPLE OUTPUT:
1 2 0 3
For the first test, Farmer John can swap the second and the third buckets in the first phase. Then, in the second phase, Farmer John can perform the following:
- $[9,9,4,2]$ -> merge the third and fourth buckets ->
- $[9,9,3]$ -> merge the second and third buckets ->
- $[9,6]$ -> merge the first and second buckets ->
- $[7.5]$
The final amount of milk is 7.5, which is the maximum possible. It can be shown that even with additional swaps, the final amount cannot exceed 7.5, and that with fewer swaps, the final amount cannot reach 7.5.
SCORING:
- Inputs 3-4: $a_i\le 1$ and $N\le 2000$ (sum of $N\le 5000$)
- Inputs 5-6: $a_i\le 1$
- Inputs 7-9: $N\le 2000$ (sum of $N\le 5000$)
- Inputs 10-14: No additional constraints.
Problem credits: Charlie Yang
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 13.2k平均输出 token(含思考) ↓
- 22.9输出速度 tokens/s ↑
- 517 秒延迟 ↓
- 1.09×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 27.9k平均输出 token(含思考) ↓
- 193.8输出速度 tokens/s ↑
- 145 秒延迟 ↓
- 1.75×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 65.2k平均输出 token(含思考) ↓
- 167.2输出速度 tokens/s ↑
- 394 秒延迟 ↓
- 0.92×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; Fenwick(int n_) : n(n_), bit(n_ + 1, 0) {} void add(int idx, int val) { for (; idx <= n; idx += idx & -idx) bit[idx] += val; } int sum(int idx) const { int res = 0; for (; idx > 0; idx -= idx & -idx) res += bit[idx]; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); vector<int> vals; vals.reserve(N); for (int &x : a) { cin >> x; vals.push_back(x); } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); int M = vals.size(); vector<int> rank(N), freq(M, 0); for (int i = 0; i < N; i++) { rank[i] = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin(); freq[rank[i]]++; } vector<int> totalSmaller(M); int prefix = 0; for (int i = 0; i < M; i++) { totalSmaller[i] = prefix; prefix += freq[i]; } Fenwick fw(M); long long answer = 0; for (int i = 0; i < N; i++) { int r = rank[i]; int leftSmaller = fw.sum(r); // ranks 0 .. r-1 are strictly smaller int rightSmaller = totalSmaller[r] - leftSmaller; answer += min(leftSmaller, rightSmaller); fw.add(r + 1, 1); } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int i, int v) { for (; i <= n; i += i & -i) bit[i] += v; } int sum(int i) const { int s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<pair<long long, int>> items(N); for (int i = 0; i < N; ++i) { cin >> items[i].first; items[i].second = i + 1; } sort(items.begin(), items.end()); Fenwick fw(N); vector<vector<int>> groups; int lowerCount = 0; for (int l = 0; l < N; ) { int r = l; while (r < N && items[r].first == items[l].first) ++r; vector<int> costs; costs.reserve(r - l); for (int i = l; i < r; ++i) { int p = items[i].second; int before = fw.sum(p - 1); int after = lowerCount - before; costs.push_back(min(before, after)); } for (int i = l; i < r; ++i) fw.add(items[i].second, 1); groups.push_back(move(costs)); lowerCount += r - l; l = r; } long long answer = 0; int remainingRanks = max(0, N - 2); for (int g = (int)groups.size() - 1; g >= 0 && remainingRanks > 0; --g) { auto &costs = groups[g]; int take = min(remainingRanks, (int)costs.size()); if (take < (int)costs.size()) { nth_element(costs.begin(), costs.begin() + take, costs.end()); } for (int i = 0; i < take; ++i) answer += costs[i]; remainingRanks -= take; } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> tree; Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int i, int value) { for (; i <= n; i += i & -i) tree[i] += value; } int sum(int i) const { int result = 0; for (; i > 0; i -= i & -i) result += tree[i]; return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<pair<int, int>> buckets(N); for (int i = 0; i < N; ++i) { cin >> buckets[i].first; buckets[i].second = i + 1; } sort(buckets.begin(), buckets.end()); Fenwick bit(N); long long answer = 0; for (int left = 0; left < N; ) { int right = left; while (right < N && buckets[right].first == buckets[left].first) { ++right; } const int smallerCount = left; for (int i = left; i < right; ++i) { int smallerOnLeft = bit.sum(buckets[i].second); int smallerOnRight = smallerCount - smallerOnLeft; answer += min(smallerOnLeft, smallerOnRight); } for (int i = left; i < right; ++i) { bit.add(buckets[i].second, 1); } left = right; } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; Fenwick(int n = 0) : n(n), bit(n + 1, 0) {} void add(int idx, int val) { for (; idx <= n; idx += idx & -idx) bit[idx] += val; } int sumPrefix(int idx) const { int res = 0; for (; idx > 0; idx -= idx & -idx) res += bit[idx]; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); vector<int> comp; comp.reserve(N); for (int i = 0; i < N; i++) { cin >> a[i]; comp.push_back(a[i]); } sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); int K = (int)comp.size(); vector<vector<int>> positions(K); for (int i = 0; i < N; i++) { int id = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin(); positions[id].push_back(i + 1); } Fenwick fw(N); long long answer = 0; long long inserted = 0; for (int id = 0; id < K; id++) { const vector<int>& pos = positions[id]; int c = (int)pos.size(); long long smallerCount = inserted; vector<int> before(c); long long totalBefore = 0; for (int i = 0; i < c; i++) { before[i] = fw.sumPrefix(pos[i] - 1); totalBefore += before[i]; } long long prefix = 0; long long best = LLONG_MAX; for (int left = 0; left <= c; left++) { long long cost = (long long)(c - left) * smallerCount + 2LL * prefix - totalBefore; best = min(best, cost); if (left < c) prefix += before[left]; } answer += best; for (int p : pos) fw.add(p, 1); inserted += c; } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; Fenwick(int n = 0) { init(n); } void init(int n_) { n = n_; bit.assign(n + 1, 0); } void add(int idx, int val) { for (; idx <= n; idx += idx & -idx) bit[idx] += val; } int sum(int idx) { int res = 0; for (; idx > 0; idx -= idx & -idx) res += bit[idx]; return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<long long> a(N); for (int i = 0; i < N; i++) cin >> a[i]; vector<long long> vals = a; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); int M = (int)vals.size(); vector<int> rank(N); vector<int> freq(M + 1, 0); for (int i = 0; i < N; i++) { rank[i] = int(lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()) + 1; freq[rank[i]]++; } vector<int> prefLess(M + 2, 0); int cum = 0; for (int r = 1; r <= M; r++) { prefLess[r] = cum; cum += freq[r]; } Fenwick fw(M); long long answer = 0; for (int i = 0; i < N; i++) { int r = rank[i]; int leftSmaller = fw.sum(r - 1); int totalSmaller = prefLess[r]; int rightSmaller = totalSmaller - leftSmaller; answer += min(leftSmaller, rightSmaller); fw.add(r, 1); } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; Fenwick(int n = 0) { init(n); } void init(int n_) { n = n_; bit.assign(n + 1, 0); } void add(int idx, int val) { while (idx <= n) { bit[idx] += val; idx += idx & -idx; } } int sum(int idx) { int res = 0; while (idx > 0) { res += bit[idx]; idx -= idx & -idx; } return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); for (int &x : a) cin >> x; vector<int> vals = a; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); int M = (int)vals.size(); vector<int> leftSmaller(N); Fenwick fw(M); for (int i = 0; i < N; i++) { int r = int(lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()) + 1; leftSmaller[i] = fw.sum(r - 1); fw.add(r, 1); } fw.init(M); long long answer = 0; for (int i = N - 1; i >= 0; i--) { int r = int(lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin()) + 1; int rightSmaller = fw.sum(r - 1); answer += min(leftSmaller[i], rightSmaller); fw.add(r, 1); } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; class Fenwick { int n; vector<int> bit; public: explicit Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int idx, int value) { for (; idx <= n; idx += idx & -idx) { bit[idx] += value; } } int sum(int idx) const { int result = 0; for (; idx > 0; idx -= idx & -idx) { result += bit[idx]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> a(N); for (int &x : a) cin >> x; vector<int> order(N); iota(order.begin(), order.end(), 0); sort(order.begin(), order.end(), [&](int x, int y) { if (a[x] != a[y]) return a[x] < a[y]; return x < y; }); Fenwick fw(N); long long inversions = 0; long long adjustment = 0; for (int left = 0; left < N; ) { int right = left + 1; while (right < N && a[order[right]] == a[order[left]]) { ++right; } int groupSize = right - left; long long totalSmaller = left; vector<long long> b(groupSize); for (int t = 0; t < groupSize; ++t) { int pos = order[left + t] + 1; // The Fenwick tree currently contains only strictly smaller values. long long smallerBefore = fw.sum(pos - 1); long long equalBefore = t; long long smallerAfter = totalSmaller - smallerBefore; b[t] = smallerBefore + equalBefore - smallerAfter; // Earlier elements greater than a[pos]. inversions += (pos - 1LL) - smallerBefore - equalBefore; } sort(b.begin(), b.end()); long long prefixSum = 0; long long best = 0; // Choosing zero elements for the decreasing side. for (int k = 1; k <= groupSize; ++k) { prefixSum += b[k - 1]; long long cost = prefixSum - 1LL * k * (k - 1) / 2; best = min(best, cost); } adjustment += best; for (int t = 0; t < groupSize; ++t) { fw.add(order[left + t] + 1, 1); } left = right; } cout << inversions + adjustment << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Fenwick { int n; vector<int> bit; // Initially every position contains 1. explicit Fenwick(int n) : n(n), bit(n + 1) { for (int i = 1; i <= n; ++i) { bit[i] = i & -i; } } void add(int index, int delta) { for (; index <= n; index += index & -index) { bit[index] += delta; } } int prefixSum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += bit[index]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<pair<int64, int>> order(N); for (int i = 0; i < N; ++i) { cin >> order[i].first; order[i].second = i + 1; } // Larger values first; occurrences of the same value are in // increasing original-position order. sort(order.begin(), order.end(), [](const auto& x, const auto& y) { if (x.first != y.first) return x.first > y.first; return x.second < y.second; }); Fenwick fenwick(N); vector<int64> smallerBefore; int64 answer = 0; int active = N; for (int start = 0; start < N; ) { int finish = start + 1; while (finish < N && order[finish].first == order[start].first) { ++finish; } int count = finish - start; int smaller = active - count; // After removing this group, the Fenwick tree contains // exactly the positions with strictly smaller values. for (int i = start; i < finish; ++i) { fenwick.add(order[i].second, -1); } smallerBefore.resize(count); int64 currentCost = 0; for (int i = 0; i < count; ++i) { int position = order[start + i].second; smallerBefore[i] = fenwick.prefixSum(position - 1); // Initially, place every occurrence on the right side. currentCost += static_cast<int64>(smaller) - smallerBefore[i]; } int64 bestCost = currentCost; // Move occurrences from the right side to the left side // in their original order. for (int i = 0; i < count; ++i) { currentCost += 2 * smallerBefore[i] - smaller; bestCost = min(bestCost, currentCost); } answer += bestCost; active = smaller; start = finish; } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; class Fenwick { int n; vector<int> tree; public: explicit Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int index) { for (; index <= n; index += index & -index) { ++tree[index]; } } int prefixSum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += tree[index]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<pair<int, int>> buckets(N); for (int i = 0; i < N; ++i) { cin >> buckets[i].first; buckets[i].second = i; } sort(buckets.begin(), buckets.end()); Fenwick fenwick(N); long long answer = 0; int smallerCount = 0; for (int left = 0; left < N; ) { int right = left + 1; while (right < N && buckets[right].first == buckets[left].first) { ++right; } // Query before inserting this group so that only strictly // smaller buckets are counted. for (int k = left; k < right; ++k) { int position = buckets[k].second; int smallerLeft = fenwick.prefixSum(position); int smallerRight = smallerCount - smallerLeft; answer += min(smallerLeft, smallerRight); } for (int k = left; k < right; ++k) { fenwick.add(buckets[k].second + 1); } smallerCount += right - left; left = right; } cout << answer << '\n'; } return 0; }