답안 #781490

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781490 2023-07-13T06:55:40 Z asdfgrace 쌀 창고 (IOI11_ricehub) C++17
100 / 100
225 ms 5700 KB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << '\n')
#define PV2(x) DEBUG(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
#define PAR2D(x) DEBUG(PRINT(#x << ":\n"); for (auto arr : x) {PAR(arr);} PRINT('\n'));
using i64 = long long;
const int INF = 1000000007; //998244353;

int besthub(int R, int L, int X[], i64 B) {
  vector<pair<i64, i64>> at;
  for (int i = 0; i < R; ++i) {
    if (i == 0 || X[i] != X[i - 1]) at.push_back({X[i], 1LL});
    else ++at.back().second;
  }
  int n = (int)at.size();
  // for each city, find maximum number WITHIN BUDGET
  // this can be done greedily
  // if we add in the order of distance from current city
  // we can binary search (yes!!!) for the size of the outer bound
  // nlog^2n probably passes as well
  vector<pair<i64, i64>> pref(n);
  vector<i64> cd(n);
  pref[0] = at[0];
  cd[0] = at[0].first * at[0].second;
  for (int i = 1; i < n; ++i) {
    pref[i].first = pref[i - 1].first + at[i].first;
    pref[i].second = pref[i - 1].second + at[i].second;
    cd[i] = cd[i - 1] + at[i].first * at[i].second;
  }
  PAR2(at); PAR2(pref);
  auto cost = [&] (i64 m, int i) -> i64 {
    int lb = lower_bound(at.begin(), at.end(),
      make_pair(at[i].first - m, 0LL)) - at.begin();
    int rb = upper_bound(at.begin(), at.end(),
      make_pair(at[i].first + m, 0LL)) - at.begin() - 1;
    i64 ldist = cd[i] - cd[lb] + at[lb].first * at[lb].second;
    i64 lcnt = pref[i].second - pref[lb].second + at[lb].second;
    i64 rdist = cd[rb] - cd[i] + at[i].first * at[i].second;
    i64 rcnt = pref[rb].second - pref[i].second + at[i].second;
    //PV(lcnt); PV(ldist); PV(rcnt); PV(rdist);
    return lcnt * at[i].first - ldist + rdist - rcnt * at[i].first;
  };
  auto abval = [&] (i64 x) -> i64 {
    return max(x, -x);
  };
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    i64 l = 0, r = L;
    // make sure it COMPLETELY works
    // then we can add on the edges if needed
    while (r > l + 1) {
      i64 m = (l + r) / 2;
      //PV(m);
      //PV(cost(m, i));
      if (cost(m, i) <= B) {
        l = m;
      } else {
        r = m - 1;
      }
    }
    i64 res = (cost(r, i) <= B ? r : l), c = cost(res, i);
    PV(res); PV(c);
    int lb = lower_bound(at.begin(), at.end(),
      make_pair(at[i].first - res, 0LL)) - at.begin();
    int rb = upper_bound(at.begin(), at.end(),
      make_pair(at[i].first + res, 0LL)) - at.begin() - 1;
    int cnt = pref[rb].second - pref[lb].second + at[lb].second;
    PV(cnt);
    if (c < B) {
      vector<pair<i64, i64>> extras;
      PV(lb); PV(rb);
      if (lb > 0) extras.push_back(at[lb - 1]);
      if (rb < n - 1) extras.push_back(at[rb + 1]);
      sort(extras.begin(), extras.end(), [&](pair<i64, i64> a, pair<i64, i64> b) {
        return a.first - at[i].first < b.first - at[i].first;
        });
      for (int j = 0; j < (int) extras.size(); ++j) {
        i64 rem = B - c, used = min(rem / abval(extras[j].first - at[i].first), extras[j].second);
        cnt += used;
        c += abval(extras[j].first - at[i].first) * used;
      }
      PAR2(extras); PV(cnt); PV(c);
    }
    ans = max(ans, cnt);
  }
  return ans;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 316 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 3 ms 448 KB Output is correct
24 Correct 4 ms 468 KB Output is correct
25 Correct 6 ms 508 KB Output is correct
26 Correct 7 ms 468 KB Output is correct
27 Correct 8 ms 468 KB Output is correct
28 Correct 9 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 1100 KB Output is correct
2 Correct 41 ms 1228 KB Output is correct
3 Correct 151 ms 5696 KB Output is correct
4 Correct 225 ms 5564 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 6 ms 832 KB Output is correct
7 Correct 12 ms 1620 KB Output is correct
8 Correct 14 ms 1620 KB Output is correct
9 Correct 21 ms 1232 KB Output is correct
10 Correct 19 ms 1232 KB Output is correct
11 Correct 148 ms 5700 KB Output is correct
12 Correct 208 ms 5564 KB Output is correct
13 Correct 102 ms 2760 KB Output is correct
14 Correct 102 ms 2768 KB Output is correct
15 Correct 116 ms 4244 KB Output is correct
16 Correct 150 ms 4292 KB Output is correct
17 Correct 139 ms 5056 KB Output is correct
18 Correct 178 ms 4960 KB Output is correct
19 Correct 140 ms 5316 KB Output is correct
20 Correct 191 ms 5300 KB Output is correct