WA on test 18 求助
查看原帖
WA on test 18 求助
418601
Typed_SIGTERM楼主2023/10/1 15:31

https://codeforces.com/contest/1873/submission/225450104

/bx


还是贴一下代码吧

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2e5 + 10;
int a[N];

int main() {
  cin.sync_with_stdio(0); cin.tie(0);
  int t;
  cin >> t;
  int n, x, ans, sum;
  while (t--) {
    cin >> n >> x;
    ans = sum = 0;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + 1 + n); // 从低的柱子开始分配
    int i;
    for (i = 1; i <= n; ++i) { // 每次尝试再加一根柱子直到超过可用水量
      if (i != 1 && a[i - 1] == a[i]) continue;
      sum += (a[i] - a[i - 1]) * (i - 1);
      // cerr << "need " << sum << " water when h = " << a[i] << '\n';
      if (sum > x) { // 超过可用水量
        sum -= (a[i] - a[i - 1]) * (i - 1);
        break;
      }
      ans = a[i];
    }
    // cerr << "sum = " << sum << '\n';
    ans += max(0, (x - sum)) / (i - 1); // 剩下的水平均分给已有的柱子
    cout << ans << '\n';
  }
  return 0;
}
2023/10/1 15:31
加载中...