답안 #1090729

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1090729 2024-09-18T16:02:50 Z vjudge1 Safety (NOI18_safety) C++17
100 / 100
239 ms 21076 KB
#include <cassert>
#include <iostream>
#include <set>

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

  int m = 0;
  long long shift = -h, c = 0;
  std::multiset<long long> m_changes[2]; // xpos for each m changes +1
  for (int i = 0; i < n; i++) {
    long long x;
    std::cin >> x;

    // shift by h
    shift += h;
    c -= h * m;

    // append to line
    m_changes[0].insert(x + shift);
    m_changes[1].insert(x - shift);
    
    // normalize
    while (true) {
      long long x_l = *m_changes[0].rbegin();
      long long x_r = *m_changes[1].begin();
      if (x_l - shift <= x_r + shift) {
        break;
      }

      m_changes[0].erase(m_changes[0].find(x_l));
      m_changes[1].erase(m_changes[1].find(x_r));
      x_l -= shift;
      x_r += shift;
      m_changes[0].insert(x_r + shift);
      m_changes[1].insert(x_l - shift);
    }

    // m*x + c + (x - x0) = (m+1)*x + (c - x0)
    m++;
    c -= x;
  }

  while (!m_changes[1].empty()) {
    // pop largest
    long long x = *m_changes[1].rbegin();
    m_changes[1].erase(m_changes[1].find(x));

    // apply shift
    x += shift;
    assert(x >= 0);

    // m*x + c = (m-1)*x + (x + c)
    m--;
    c += x;
  }

  assert(m == 0);
  std::cout << c << std::endl;

  return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 452 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 13408 KB Output is correct
2 Correct 187 ms 19280 KB Output is correct
3 Correct 218 ms 20132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 372 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 372 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 4 ms 860 KB Output is correct
29 Correct 3 ms 860 KB Output is correct
30 Correct 3 ms 860 KB Output is correct
31 Correct 2 ms 856 KB Output is correct
32 Correct 2 ms 856 KB Output is correct
33 Correct 3 ms 724 KB Output is correct
34 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 372 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 4 ms 860 KB Output is correct
35 Correct 3 ms 860 KB Output is correct
36 Correct 3 ms 860 KB Output is correct
37 Correct 2 ms 856 KB Output is correct
38 Correct 2 ms 856 KB Output is correct
39 Correct 3 ms 724 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 4 ms 860 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 3 ms 860 KB Output is correct
46 Correct 2 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 452 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 125 ms 13408 KB Output is correct
20 Correct 187 ms 19280 KB Output is correct
21 Correct 218 ms 20132 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 372 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 4 ms 860 KB Output is correct
38 Correct 3 ms 860 KB Output is correct
39 Correct 3 ms 860 KB Output is correct
40 Correct 2 ms 856 KB Output is correct
41 Correct 2 ms 856 KB Output is correct
42 Correct 3 ms 724 KB Output is correct
43 Correct 2 ms 860 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 2 ms 860 KB Output is correct
46 Correct 2 ms 860 KB Output is correct
47 Correct 4 ms 860 KB Output is correct
48 Correct 3 ms 860 KB Output is correct
49 Correct 2 ms 856 KB Output is correct
50 Correct 239 ms 21076 KB Output is correct
51 Correct 144 ms 17488 KB Output is correct
52 Correct 78 ms 16656 KB Output is correct
53 Correct 85 ms 19616 KB Output is correct
54 Correct 127 ms 20048 KB Output is correct
55 Correct 86 ms 16980 KB Output is correct
56 Correct 81 ms 18688 KB Output is correct
57 Correct 85 ms 17232 KB Output is correct
58 Correct 95 ms 14416 KB Output is correct
59 Correct 92 ms 19128 KB Output is correct