Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

二分最好用 long long(紫书上是 double)

Posted by Coffish at 2020-10-20 00:03:11 on Problem 2018
int 会爆掉

#include <cstdio>
#include <algorithm>

#define int long long

int n, f;
int a[100050];

inline bool Check(int val) {
  int foo = 0;
  int ans = -1;
  static int buf[100050];
  for (int i = 1; i <= n; ++i) {
    buf[i] = buf[i - 1] + a[i] - val;
  }
  for (int i = f; i <= n; ++i) {
    if (buf[i - f] < foo) foo = buf[i - f];
    if (buf[i] - foo > ans) ans = buf[i] - foo;
  }
  return ans >= 0;
}

signed main()
{
  scanf("%lld%lld", &n, &f);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
    a[i] *= 1000;
  }
  int l = a[1], r = a[1];
  for (int i = 1; i <= n; ++i) {
    if (a[i] < l)
      l = a[i];
    else if (a[i] > r)
      r = a[i];
  }
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (Check(mid)) l = mid;
    else r = mid - 1;
  }
  printf("%lld\n", l);
  return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator