답안 #435819

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435819 2021-06-23T18:42:04 Z bicsi 사탕 분배 (IOI21_candies) C++17
8 / 100
4093 ms 11668 KB
#include "candies.h"
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int BS = 333;
 

vector<int> compute_function(const vector<int>& ops) {
  vector<int> stk;
  stk.push_back(2e9);
  stk.push_back(2e9);
  for (auto delta : ops) {
    if (delta == 0) continue;
    if (delta > 0) {
      int rem = delta, px = 0;
      while (true) {
        int x = stk.back();
        if (x - px > rem) {
          stk.push_back(px + rem);
          stk.push_back(0);
          break;
        } 
        rem -= (x - px);
        stk.pop_back();
        px = stk.back();
        stk.pop_back();
      }
    } else {
      int rem = -delta;
      while (true) {
        int px = stk.back(); stk.pop_back();
        int x = stk.back();
        if (x - px > rem) {
          stk.push_back(px + rem);
          break;
        }
        rem -= (x - px);
        stk.pop_back();
      }
    }
  }
  
  stk.push_back(0);
  stk.push_back(0);
  reverse(stk.begin(), stk.end());
  return stk;
}

vector<int> distribute_candies(
    vector<int> c, vector<int> l,
    vector<int> r, vector<int> v) {
  int n = c.size();
 
  
  // Sort each bucket increasingly by c[i] to facilitate easy evaluation of f.
  vector<int> order(n);
  iota(order.begin(), order.end(), 0);
  for (int i = 0; i < n; i += BS) {
    int j = min(i + BS, n);
    sort(order.begin() + i, order.begin() + j, [&](int a, int b) {
      return c[a] < c[b];
    });
  }

  vector<int> stk;
  vector<int> a(n, 0), na(n, 0);
  vector<vector<int>> ops(n / BS + 1);

  auto commit = [&](int bi) {
    if (ops[bi].empty()) return;
    int l = bi * BS, r = min(n, (bi + 1) * BS);

    // compute f(.) and evaluate it in all c[i] of this bucket
    auto stk = compute_function(ops[bi]);
    int value = 0;
    int ptr = 0;
    for (int i = bi * BS; i < (bi + 1) * BS && i < n; ++i) {
      int ci = c[order[i]];
      while (stk[ptr + 1] < ci) {
        if (ptr % 2 == 0)
          value += stk[ptr + 1] - stk[ptr];
        ++ptr;
      }
      if (ptr % 2 == 0) {
        na[order[i]] = value + (ci - stk[ptr]);
      } else {
        na[order[i]] = value;
      }
    }
  
    // Adapt for non-zero initial values.
    ll mind = 0, maxd = 0, nowd = 0;
    for (auto x : ops[bi]) {
      nowd += x;
      mind = min(mind, nowd);
      maxd = max(maxd, nowd);
    }
    for (int i = bi * BS; i < (bi + 1) * BS && i < n; ++i) {
      ll hi = min(c[i] - maxd, (ll)a[i]);
      ll lo = -mind;
      if (lo < hi) 
        na[i] += hi - lo; 
    }
 
    // Housekeeping.
    for (int i = bi * BS; i < (bi + 1) * BS && i < n; ++i) 
      a[i] = na[i];
    ops[bi].clear();
  };
 
  int q = l.size();
  for (int i = 0; i < q; ++i) {
    int lf = l[i], rt = r[i];
    int bl = l[i] / BS, br = r[i] / BS;
    commit(bl); commit(br); 
    for (int j = l[i]; j <= r[i]; ) {
      if (j % BS == 0 && (j + BS - 1) <= r[i]) {
        ops[j / BS].push_back(v[i]);
        j += BS;
      } else {
        a[j] = max(0, min(a[j] + v[i], c[j]));
        j += 1;
      }
    }
  }
 
  for (int i = 0; i < n; i += BS)
    commit(i / BS);
  return a;
}

Compilation message

candies.cpp: In lambda function:
candies.cpp:72:9: warning: unused variable 'l' [-Wunused-variable]
   72 |     int l = bi * BS, r = min(n, (bi + 1) * BS);
      |         ^
candies.cpp:72:22: warning: unused variable 'r' [-Wunused-variable]
   72 |     int l = bi * BS, r = min(n, (bi + 1) * BS);
      |                      ^
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:114:9: warning: unused variable 'lf' [-Wunused-variable]
  114 |     int lf = l[i], rt = r[i];
      |         ^~
candies.cpp:114:20: warning: unused variable 'rt' [-Wunused-variable]
  114 |     int lf = l[i], rt = r[i];
      |                    ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Runtime error 2 ms 516 KB Execution killed with signal 11
# 결과 실행 시간 메모리 Grader output
1 Correct 3743 ms 11668 KB Output is correct
2 Correct 4022 ms 11604 KB Output is correct
3 Correct 4093 ms 11608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Runtime error 2 ms 516 KB Execution killed with signal 11