97pts WA on #2 求助
查看原帖
97pts WA on #2 求助
734630
justin_jiajia蒟蒻楼主2023/12/2 19:17

RT,贪心双指针 record

code:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pp = pair<int, int>;
#define pb push_back
#define mp make_pair
const int mx = 1e5 + 2;
int n;
double a[mx], b[mx];
bool cmp(double x, double y) { return x > y; }
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
  sort(a, a + n, cmp);
  sort(b, b + n, cmp);
  int ac = 0;
  double as = 0.0;
  int bc = 0;
  double bs = 0.0;
  if (a[0] > b[0])
    as = a[ac++];
  else
    bs = b[bc++];
  double ans = max(0.0, min(as, bs) - (ac + bc));
  while (ac < n && bc < n) {
    if (as < bs) {
      // chose a in this condition
      as += a[ac];
      ++ac;
      ans = max(min(as, bs) - (ac + bc), ans);
    } else {
      // chose b else
      bs += b[bc];
      ++bc;
      ans = max(min(as, bs) - (ac + bc), ans);
    }
  }
  cout << setiosflags(ios::fixed) << setprecision(4) << ans << '\n';
}
2023/12/2 19:17
加载中...