题解:P17297 [ICPC 2026 Xi'an I] Qenerals
题目大意
初始给定
- 将
x 增加y ; - 可以任意次选择
j \in S 且a_{j}\le x ,执行将x 减去a_{j} ,将y 增加1 ,并把j 从S 中移除。
求
贪心策略
优先选择成本小的堡垒,并且尽可能早地占领它。
::::info[证明(一定要看呀 QwQ)]{open}
若第
| 可以看出,堡垒的收益只与它的占领时刻和自身成本有关,且越早占领收益越高,成本越小收益也越高。 |
|---|
代码实现
将数组
::::warning[你真的要看代码吗]
#include <bits/stdc++.h>
#define int long long
using namespace std;
int query, source, current;
int target[1 << 20];
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
// cin.exceptions(ios::badbit | ios::failbit);
cin >> query;
while (query--) {
cin >> source >> current;
for (int pos = 1; pos <= source; pos++) cin >> target[pos];
sort(target + 1, target + source + 1);
int result = 0, values = 1;
for (int pos = 1; pos <= source; pos++)
if (result >= target[pos]) {
if (current <= target[pos]) break; // 亏钱
result -= target[pos];
values++;
} else {
int value = ((target[pos] - result) + values - 1) / values;
if (value > current || current - value <= target[pos]) break; // 等不到
current -= value, result += value * values, result -= target[pos], values++;
}
cout << result + current * values << '\n';
}
return 0;
}
| 时间复杂度来源于排序的 |
|---|
如果 WA 了的话可以对拍:
::::info[生成器]
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
// freopen(".in", "r", stdin);
freopen("P17297.in", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cin.exceptions(ios::badbit | ios::failbit);
mt19937 value(chrono::steady_clock::now().time_since_epoch().count());
int query = 1;
cout << query << '\n';
int source = value() % 6 + 1, current = value() % 5 + 1;
cout << source << ' ' << current << '\n';
for (int pos = 1; pos <= source; pos++) {
int target = value() % 10 + 1;
cout << target << " \n"[pos == source];
}
return 0;
}
| 暴力就自己写吧 awa。 |
|---|