답안 #1091802

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1091802 2024-09-22T07:44:12 Z vjudge1 Potatoes and fertilizers (LMIO19_bulves) C++17
100 / 100
391 ms 54096 KB
#include <climits>
#include <iostream>
#include <set>

int main() {
  std::cin.tie(NULL)->sync_with_stdio(false);

  int n;
  std::cin >> n;

  long long total = 0;
  long long m = 0, c = 0; // leftmost m & c
  long long shift = 0;
  std::multiset<long long> m_neg, m_pos;
  auto calc_y = [&](long long x) -> long long {
    long long cc = c, mm = m;
    long long xnow = LLONG_MIN;
    for (auto xx: m_neg) {
      if (xx > x) {
        // std::cerr << "x=" << x << " m=" << mm << " c=" << cc << std::endl;
        return mm * x + cc;
      }
      // mm * x + cc = (mm + 1) * x + (cc - x)
      mm++;
      cc -= xx;
    }
    for (auto xx: m_pos) {
      xx += shift;
      if (xx > x) {
        // std::cerr << "x=" << x << " m=" << mm << " c=" << cc << std::endl;
        return mm * x + cc;
      }
      mm++;
      cc -= xx;
    }
    return mm * x + cc;
  };


  for (int i = 0; i < n; i++) {
    long long a, b;
    std::cin >> a >> b;

    total += a - b;

    // take min
    // TODO: shift positive grad all by a
    if (!m_pos.empty()) {
      shift += a;
    }

    // insert
    if (total > 0) {
      // TODO: combine with y = total - x
      m--;
      c += total;
      m_neg.insert(total);
      m_pos.insert(total - shift);
    } else {
      // TODO: combine with y = x + (-total) if x >= 0, else -x -total
      m--;
      c += -total;
      m_neg.insert(0);
      m_pos.insert(-shift);
    }

    // normalize
    while (*m_neg.rbegin() > *m_pos.begin() + shift) {
      long long x_neg = *m_neg.rbegin();
      long long x_pos = *m_pos.begin();
      m_neg.erase(--m_neg.end());
      m_pos.erase(m_pos.begin());

      m_neg.insert(x_pos + shift);
      m_pos.insert(x_neg - shift);
    }

    // debug
    // std::cerr << std::endl;
    // std::cerr << "m=" << m << " c:" << c << std::endl;
    // std::cerr << "m_neg:"; for (auto x: m_neg) std::cerr << " " << x; std::cerr << std::endl;
    // std::cerr << "m_pos:"; for (auto x: m_pos) std::cerr << " " << x + shift; std::cerr << std::endl;
    // for (int j = 0; j < 10; j++) std::cerr << " " << calc_y(j); std::cerr << std::endl;
  }

  std::cout << calc_y(total) << std::endl;

  return 0;
}

Compilation message

bulves.cpp: In lambda function:
bulves.cpp:17:15: warning: unused variable 'xnow' [-Wunused-variable]
   17 |     long long xnow = LLONG_MIN;
      |               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 28 ms 5204 KB Output is correct
5 Correct 57 ms 10468 KB Output is correct
6 Correct 142 ms 27208 KB Output is correct
7 Correct 320 ms 54096 KB Output is correct
8 Correct 300 ms 52052 KB Output is correct
9 Correct 308 ms 51540 KB Output is correct
10 Correct 358 ms 49236 KB Output is correct
11 Correct 341 ms 49156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 28 ms 5204 KB Output is correct
5 Correct 57 ms 10468 KB Output is correct
6 Correct 142 ms 27208 KB Output is correct
7 Correct 320 ms 54096 KB Output is correct
8 Correct 300 ms 52052 KB Output is correct
9 Correct 308 ms 51540 KB Output is correct
10 Correct 358 ms 49236 KB Output is correct
11 Correct 341 ms 49156 KB Output is correct
12 Correct 93 ms 13704 KB Output is correct
13 Correct 204 ms 32596 KB Output is correct
14 Correct 301 ms 54096 KB Output is correct
15 Correct 314 ms 52048 KB Output is correct
16 Correct 389 ms 51580 KB Output is correct
17 Correct 245 ms 49260 KB Output is correct
18 Correct 2 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 600 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 600 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 28 ms 5204 KB Output is correct
12 Correct 57 ms 10468 KB Output is correct
13 Correct 142 ms 27208 KB Output is correct
14 Correct 320 ms 54096 KB Output is correct
15 Correct 300 ms 52052 KB Output is correct
16 Correct 308 ms 51540 KB Output is correct
17 Correct 358 ms 49236 KB Output is correct
18 Correct 341 ms 49156 KB Output is correct
19 Correct 93 ms 13704 KB Output is correct
20 Correct 204 ms 32596 KB Output is correct
21 Correct 301 ms 54096 KB Output is correct
22 Correct 314 ms 52048 KB Output is correct
23 Correct 389 ms 51580 KB Output is correct
24 Correct 245 ms 49260 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 600 KB Output is correct
33 Correct 71 ms 13652 KB Output is correct
34 Correct 200 ms 32492 KB Output is correct
35 Correct 357 ms 53956 KB Output is correct
36 Correct 361 ms 51536 KB Output is correct
37 Correct 293 ms 52140 KB Output is correct
38 Correct 335 ms 54092 KB Output is correct
39 Correct 325 ms 50244 KB Output is correct
40 Correct 220 ms 45652 KB Output is correct
41 Correct 391 ms 49148 KB Output is correct
42 Correct 346 ms 49492 KB Output is correct
43 Correct 387 ms 49236 KB Output is correct
44 Correct 312 ms 49172 KB Output is correct
45 Correct 385 ms 54004 KB Output is correct
46 Correct 260 ms 49488 KB Output is correct
47 Correct 255 ms 39504 KB Output is correct