P2352 队爷的新书

P2352 队爷的新书

贪心.

显然, 我们的答案一定是在某个区间的右端点, 所以我们把左右端点分别排序, 然后枚举右端点, 把所有小于当前右端点的左端点统计进来, 每次更新答案, 当前的报酬就是左端点下标 - 右端点下标 * 当前右端点的值.

\(code:\)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 1e5 + 5;
int n;
ll a[N], b[N], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) a[i] = read(), b[i] = read();
  sort(a + 1, a + n + 1); sort(b + 1, b + n + 1);
  for (int i = 1, j = 1; j <= n; j++) {
    while (a[i] <= b[j] && i <= n) i++;
    ans = max(ans, (i - j) * b[j]);
  }
  printf("%lld", ans);
  return 0;
}
posted @ 2021-09-18 06:43  sshadows  阅读(26)  评论(0编辑  收藏  举报