답안 #261586

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261586 2020-08-11T21:48:49 Z sandoval 쌀 창고 (IOI11_ricehub) C++11
100 / 100
632 ms 1536 KB
#include "ricehub.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct Info{int l, r; ll cost;};

int besthub(int R, int L, int X[], long long B) {
  const int n = R;
  if (n <= 5000) {
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int curr = 1;
      ll rem = B;
      priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
      if (i-1>=0) pq.push({X[i]-X[i-1], i-1});
      if (1+i<n) pq.push({X[1+i]-X[i], 1+i});
      while (!pq.empty()) {
        auto x = pq.top(); pq.pop();
        if (x.first > rem) break;
        curr++; rem -= x.first;
        int add = 1;
        if (x.second < i) {
          add = -1;
        }

        x.second += add;
        if (x.second >= 0 && x.second < n) {
          pq.push({abs(X[x.second]-X[i]), x.second});
        }
      }

      ans = max(ans, curr);
    }
    return ans;
  }
  vector<ll> pre(X, X+n);

  for (int i = 1; i < n; ++i) pre[i] += pre[i-1];

  auto sum = [](const vector<ll>& ps, int l, int r)->long long{
    if (l > r) return 0LL;
    ll ans = ps[r];
    if (l > 0) ans -= ps[l-1];
    return ans;
  };

  auto get = [&sum, &n](int A[], int idx, int l, int r, const vector<ll>& ps)->Info{
    int lb = lower_bound(A, A+n, l) - A;
    int ub = upper_bound(A, A+n, r) - A - 1;
    int le = 1+idx-lb;
    int g = ub-idx;
    return Info{lb, ub, 1LL*A[idx]*le-sum(ps, lb, idx) + sum(ps, 1+idx, ub)-1LL*g*A[idx]};
  };

  auto getleft = [&sum, &n](int p, int l, int r, const vector<ll>& ps)->ll{
    const int len = 1+r-l;
    return 1LL*p*len-sum(ps, l, r);
  };

  auto getright = [&sum, &n](int p, int l, int r, const vector<ll>& ps)->ll{
    const int len = 1+r-l;
    return sum(ps, l, r)-1LL*p*len;
  };

  int ans = 1;
  for (int i = 0; i < n; ++i) {
    Info best{-1,-1,-1};
    {
      int low = 0, high = 1e9;
      while (low <= high) {
        int mid = low + ((high-low)/2);
        Info x = get(X, i, X[i]-mid, X[i]+mid, pre);
        if (x.cost <= B) {
          low = 1+mid;
          best = x;
        } else {
          high = mid-1;
        }
      }
    }
    assert(best.l != -1);

    int len = 1+best.r-best.l, op1 = 0, op2 = 0;
    ll rem = B - best.cost;
    assert(rem >= 0);

    {
      int low = 0, high = best.l-1;
      while (low <= high) {
        int mid = (low+high) >> 1;
        if (getleft(X[i], mid, best.l-1, pre) <= rem) {
          high = mid-1;
          op1 = best.l-mid;
        } else {
          low = 1+mid;
        }
      }
    }

    {
      int low = best.r+1, high = n-1;
      while (low <= high) {
        int mid = (low+high) >> 1;
        if (getright(X[i], best.r+1, mid, pre) <= rem) {
          low = 1+mid;
          op2 = mid-best.r;
        } else {
          high = mid-1;
        }
      }
    }

    ans = max(ans, len+max(op1, op2));
  }
  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 1 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 1 ms 256 KB Output is correct
13 Correct 1 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 6 ms 384 KB Output is correct
24 Correct 7 ms 384 KB Output is correct
25 Correct 6 ms 384 KB Output is correct
26 Correct 7 ms 384 KB Output is correct
27 Correct 3 ms 256 KB Output is correct
28 Correct 3 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 20 ms 364 KB Output is correct
4 Correct 18 ms 256 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 3 ms 256 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 25 ms 376 KB Output is correct
14 Correct 23 ms 384 KB Output is correct
15 Correct 3 ms 256 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 17 ms 384 KB Output is correct
18 Correct 15 ms 384 KB Output is correct
19 Correct 26 ms 376 KB Output is correct
20 Correct 24 ms 376 KB Output is correct
21 Correct 33 ms 384 KB Output is correct
22 Correct 33 ms 384 KB Output is correct
23 Correct 632 ms 404 KB Output is correct
24 Correct 582 ms 504 KB Output is correct
25 Correct 605 ms 504 KB Output is correct
26 Correct 577 ms 504 KB Output is correct
27 Correct 316 ms 384 KB Output is correct
28 Correct 317 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 512 KB Output is correct
2 Correct 59 ms 544 KB Output is correct
3 Correct 169 ms 1536 KB Output is correct
4 Correct 241 ms 1536 KB Output is correct
5 Correct 102 ms 896 KB Output is correct
6 Correct 101 ms 1024 KB Output is correct
7 Correct 153 ms 1536 KB Output is correct
8 Correct 219 ms 1536 KB Output is correct
9 Correct 127 ms 896 KB Output is correct
10 Correct 147 ms 896 KB Output is correct
11 Correct 171 ms 1528 KB Output is correct
12 Correct 243 ms 1536 KB Output is correct
13 Correct 152 ms 896 KB Output is correct
14 Correct 156 ms 896 KB Output is correct
15 Correct 144 ms 1152 KB Output is correct
16 Correct 181 ms 1152 KB Output is correct
17 Correct 154 ms 1408 KB Output is correct
18 Correct 215 ms 1408 KB Output is correct
19 Correct 162 ms 1408 KB Output is correct
20 Correct 251 ms 1408 KB Output is correct