C. Vanya and Exams

https://codeforces.com/problemset/problem/492/C

题意:给定长度为n的数组a和b,以及一个上限值r和平均值ave。现在可以对a中的数字a[i]进行+1操作,且a[i]不能超过r,消耗b[i]的代价。问使得数组a的平均值不小于ave的最小代价是多少。

思路:计算出数组a需要增加的数值need,然后考虑每个位置距离r有多少次+1可以用,最后按数组b的代价大小非逆序排序,贪心的扫描一边即可。

总结,好久没有很快的A过题了。。原来秒切这么爽啊

inline void solve() {
int n;
cin >> n;

long long r, ave;
cin >> r >> ave;

vector a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
}

long long need = ave * n - std::accumulate(a.begin(), a.end(), 0ll);
for (auto& x : a) {
x = std::max(0ll, r - x);
}
std::vector pos(n);
std::iota(pos.begin(), pos.end(), 0);
std::sort(pos.begin(), pos.end(), [&](const int i, const int j) {
return b[i] < b[j];
});

long long ans = 0;
for (auto i : pos) {
if (need <= 0) {
break;
}
int now = std::min(need, a[i]);
ans += 1ll * now * b[i];
need -= now;
}

cout << ans << '\n';

}

posted @ 2025-12-24 10:01  _Yxc  阅读(13)  评论(0)    收藏  举报