90求助,第六个和第二十个过不了

P1314 [NOIP2011 提高组] 聪明的质监员

goodsnack @ 2024-10-24 20:24:24

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <queue>
#include <cstring>
#include <set>
#include <map>
using namespace std;
const int N = 1e6 + 2;
int n, m;
long long s;
int val_sum[N], cnt_sum[N];
int w[N], v[N];
int l[N], r[N];
int max_w = -1;
long long min(long long x, long long y) // 最小值
{
  if (x > y)
    return y;
  else
    return x;
}
void prefix(int x) // 二分答案+前缀和
{
  for (int i = 1; i <= n; i++)
  {
    if (w[i] >= x)
    {
      val_sum[i] = val_sum[i - 1] + 1;
      cnt_sum[i] = cnt_sum[i - 1] + v[i];
    }
    else
    {
      val_sum[i] = val_sum[i - 1];
      cnt_sum[i] = cnt_sum[i - 1];
    }
  }
}
long long y() // 返回答案
{
  long long ans = 0;
  for (int i = 1; i <= m; i++)
  {
    ans += (cnt_sum[r[i]] - cnt_sum[l[i] - 1]) * (val_sum[r[i]] - val_sum[l[i] - 1]);
  }
  return ans;
}
void ini()
{
  cin >> n >> m >> s;
  for (int i = 1; i <= n; i++) // 初始化
  {
    cin >> w[i] >> v[i];
    max_w = max(max_w, w[i]);
  }
  for (int i = 1; i <= m; i++)
  {
    cin >> l[i] >> r[i];
  }
}
int main()
{
  ini();
  int f = 1, r = max_w + 1, mid;
  long long anss = 9999999999999999;
  while (f <= r)
  {
    mid = (f + r) / 2;
    prefix(mid);
    long long ans = y();
    if (ans >= s)
    {
      f = mid + 1;
    }
    else
    {
      r = mid - 1;
    }
    anss = min(anss, fabs(s - ans));
  }
  cout << anss;
  return 0;
}

by puppy_knight @ 2024-12-05 16:09:00

我也是 蹲


by puppy_knight @ 2024-12-05 21:15:32

@goodsnack 找到问题了 你试试把val_sum 和cnt_sum数组用long定义 我用了之后就AC了


|