답안 #299307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
299307 2020-09-14T17:17:14 Z AlexPop28 Candies (JOI18_candies) C++11
100 / 100
227 ms 19420 KB
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

// chiar asa de prost implementez sau e gresit? :(

using namespace std;

const int64_t INF = (int64_t)1e18;

template<class T, class Container = vector<T>, class Compare = less<T>>
struct PriorityQueue {
  int sz = 0;
  priority_queue<T, Container, Compare> pq, del;

  void emplace(const T &val) { pq.emplace(val); ++sz; }
  void erase(const T &val) { del.emplace(val); --sz; }
  void pop() { erase(top()); }
  T top() {
    while (!del.empty() && !pq.empty() && del.top() == pq.top()) del.pop(), pq.pop();
    return pq.top();
  }
  int size() const {return sz;}
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n; cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  
  PriorityQueue<tuple<int64_t, int64_t, int64_t>> interv;
  vector<int64_t> lft(n), rgt(n), val(n);
  vector<bool> added(n);
  for (int i = 0; i < n; ++i) {
    lft[i] = i;
    rgt[i] = i;
    val[i] = v[i];
    added[i] = true;
    interv.emplace(make_tuple(v[i], i, i));
  }
  
  int64_t ans = 0;
  for (int k = 1; k <= (n + 1) / 2; ++k) {
    int64_t curr, l, r; tie(curr, l, r) = interv.top(); interv.pop();
    ans += curr;
    
    cout << ans << '\n';
    
    bool insert = true;
    if (l == 0 || r == n - 1) {
      insert = false;
    }
    
    curr = -curr;
    int64_t nl = l, nr = r;
    if (l - 1 >= 0) {
      int64_t x = lft[l - 1], y = l - 1;
      assert(val[x] != -INF);
      if (added[x]) interv.erase(make_tuple(val[x], x, y));
      else insert = false;
      curr += val[x];
      nl = x;
     }
    if (r + 1 < n) {
      int64_t x = r + 1, y = rgt[x];
      assert(val[x] != -INF);
      if (added[x]) interv.erase(make_tuple(val[x], x, y));
      else insert = false;
      curr += val[x];
      nr = y;
    }

    if (insert) {
      interv.emplace(make_tuple(curr, nl, nr));
      added[nl] = true;
    } else added[nl] = false;
    val[nl] = curr;
    lft[nr] = nl;
    rgt[nl] = nr;
    
    for (int i = nl + 1; i <= nr; ++i) {
      if (val[i] == -INF) break;
      val[i] = -INF;
    }
    for (int i = nr; i >= nl + 1; --i) {
      if (val[i] == INF) break;
      val[i] = -INF;
    }
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 544 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 544 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 2 ms 512 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 2 ms 512 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 2 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 227 ms 19164 KB Output is correct
22 Correct 222 ms 19292 KB Output is correct
23 Correct 220 ms 19292 KB Output is correct
24 Correct 171 ms 19032 KB Output is correct
25 Correct 170 ms 19036 KB Output is correct
26 Correct 172 ms 19036 KB Output is correct
27 Correct 183 ms 19164 KB Output is correct
28 Correct 184 ms 19164 KB Output is correct
29 Correct 181 ms 19292 KB Output is correct
30 Correct 188 ms 19292 KB Output is correct
31 Correct 191 ms 19268 KB Output is correct
32 Correct 193 ms 19288 KB Output is correct
33 Correct 203 ms 19040 KB Output is correct
34 Correct 207 ms 19036 KB Output is correct
35 Correct 203 ms 19040 KB Output is correct
36 Correct 220 ms 19420 KB Output is correct
37 Correct 223 ms 19164 KB Output is correct
38 Correct 217 ms 19420 KB Output is correct
39 Correct 170 ms 19036 KB Output is correct
40 Correct 171 ms 19036 KB Output is correct
41 Correct 172 ms 19036 KB Output is correct
42 Correct 182 ms 19388 KB Output is correct
43 Correct 185 ms 19312 KB Output is correct
44 Correct 181 ms 19160 KB Output is correct
45 Correct 188 ms 19420 KB Output is correct
46 Correct 193 ms 19380 KB Output is correct
47 Correct 190 ms 19292 KB Output is correct
48 Correct 207 ms 19036 KB Output is correct
49 Correct 205 ms 19036 KB Output is correct
50 Correct 206 ms 19164 KB Output is correct