Submission #329289

#TimeUsernameProblemLanguageResultExecution timeMemory
329289chenwz카니발 티켓 (IOI20_tickets)C++14
100 / 100
1104 ms67820 KiB
#include "tickets.h"
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
using namespace std;
typedef long long LL;
typedef vector<int> IVec;
typedef pair<LL, LL> LPair;
LL find_maximum(int K, vector<IVec> d) {
  int N = d.size(), M = d[0].size(); // N color ×M tickets
  vector<IVec> o(N); // o.resize(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) o[i].push_back(j); // o[N][i]:index of ticket which is ith min from left
    sort(o[i].begin(), o[i].end(), [&](int a, int b) { return d[i][a] < d[i][b]; });
  }
  auto dval = [&](int c, int i) { return d[c][o[c][i]]; }; // num of ticket of color c which is ith form left
  LL prize = 0;
  IVec Plus(N, 0), minus(N, 0), take(N, 0); // Plus[c] => How many '+'s the color c will send
  priority_queue<LPair> Q; // Q: (change to prize by turning a - into a +, color index)
  for (int c = 0; c < N; c++) {
    for (int j = 0; j < K; j++) prize -= dval(c, j); // First incur prize of all L_c
    assert(Plus[c] == 0);
    Q.push(make_pair(dval(c, M - 1 - Plus[c]) + dval(c, K - 1 - Plus[c]), c)); // Queue a possible - → +
  }

  for (int i = 0; i < N * K / 2; i++) { // trade NK/2 '-' with NK/2 '+'s
    LPair it = Q.top(); Q.pop();
    prize += it.first; // L(c) + R(c)
    int c = it.second, &pc = Plus[c]; // If the color hasn’t reached K +, queue another possible +
    if (++pc < K) Q.push(LPair(dval(c, M - pc - 1) + dval(c, K - pc - 1), c));
  }
  while (!Q.empty()) Q.pop(); // clean the Q

  for (int c = 0; c < N; c++) Q.push(LPair(Plus[c], c)); // Q of (+s remaining, color)
  vector<IVec> S(N, IVec(M, -1));
  // minus[c]: # of '-' color c has sent so far, take[c]: Does this c send a '+' in this round
  for (int ki = 0; ki < K; ki++) { // K rounds
    for (int c = 0; c < N / 2; c++) take[Q.top().second] = 1, Q.pop(); // Pick N/2 colors with the most +s left
    for (int c = 0; c < N; c++) { // Check if each color sent a - or +, and change S accordingly
      int &p = Plus[c], &m = minus[c];
      if (take[c]) // color c sent a '+', send the smallest '+', Queue c back
        S[c][o[c][M - p]] = ki, Q.push(LPair(--p, c));
      else
        S[c][o[c][m++]] = ki; // color c sent a - this round, send the smallest -
      take[c] = 0;
    }
  }

  allocate_tickets(S);
  return prize;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...