Submission #231243

# Submission time Handle Problem Language Result Execution time Memory
231243 2020-05-13T06:17:34 Z xiaowuc1 Candies (JOI18_candies) C++14
0 / 100
9 ms 768 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef pair<ll, pii> state;

int l[300000];
int torhs[300000];
int tolhs[300000];
int n, k;

set<state> cands;
map<pii, ll> rev;

void ins(pii link, ll cost) {
  rev[link] = cost;
  cands.insert(state(cost, link));
}
ll erase(pii link) {
  if(!rev.count(link)) return -1;
  ll cost = rev[link];
  if(cands.erase(state(cost, link)) == 1) {
    return cost;
  }
  return -1e18;
}

void solve() {
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> l[i];
  for(int i = 1; i <= n; i++) {
    ins({i-1, i}, -l[i]);
    torhs[i-1] = i;
    tolhs[i] = i-1;
  }
  k = (n+1)/2;
  ll ret = 0;
  while(k--) {
    state cand = *cands.begin();
    ret -= cand.first;
    cands.erase(cand);
    int nlhs = tolhs[cand.second.first];
    int nrhs = torhs[cand.second.second];
    vector<ll> v;
    v.push_back(erase({nlhs, cand.second.first}));
    v.push_back(erase({cand.second.second, nrhs}));
    if(min(v[0], v[1]) > -1e18) {
      ins({nlhs, nrhs}, (v[0] - (cand.first) + v[1]));
      torhs[nlhs] = nrhs;
      tolhs[nrhs] = nlhs;
    }
    cout << ret << "\n"; cout << flush;
  }
}


// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 768 KB Output isn't correct
2 Halted 0 ms 0 KB -