Submission #1251127

#TimeUsernameProblemLanguageResultExecution timeMemory
1251127wng_Souvenirs (IOI25_souvenirs)C++20
39 / 100
13 ms412 KiB
#include <bits/stdc++.h>
#include "souvenirs.h"

using namespace std;
using ll = long long;

using res_type = pair<vector<int>, ll>;

void buy_souvenirs(int N, ll P0) {
  res_type res;

  if (N == 2) { // s1
    res = transaction(P0 - 1);
    return;
  }

  if (N == 3) { // s4
    res = transaction(P0 - 1);

    bool bought_p2 = find(res.first.begin(), res.first.end(), 2) != res.first.end();

    ll change = res.second;
    ll used = (P0 - 1) - change;

    // for (int i : res.first)
    //   cerr << i << ' ';
    // cerr << '\n';

    if (bought_p2) {
      ll p1 = used / 2 + 1; // note, floordiv

      transaction(used - p1);
    } else { // then (p0 - 1) was not enough?
      ll p1 = used;

      transaction(p1 - 1);
      transaction(p1 - 1);
    }

    return;
  }
  
  if (P0 == N) { // s2
    for (int i = 1; i < N; i++) {
      int price = N - i;
      for (int amt = 1; amt <= i; amt++) {
        transaction(price); 
      }
    }
    return;
  }

  vector<ll> cnt(N + 1);
  for (int i = 1; i < N; i++) {
    cnt[i] = i;
  }

  ll prv = P0;
  ll P_i = P0;
  ll to_buy = 0;
  for (int i = 1; i < N; i++) {
    int left = N - i;

    P_i = max(1ll, prv - 2);
    if (left == 1)
      P_i = prv - 1;

    auto [bought, change] = transaction(P_i);
    for (int tp : bought) {
      cnt[tp]--;
    }

    if (find(bought.begin(), bought.end(), i) != bought.end())  {
      P_i = max(1ll, prv - 2);
    } else {
      P_i = max(1ll, prv - 1);
    }

    if (left == 1)
      P_i = prv - 1;

    // if (cnt[i] < 0) {
    //   cerr << cnt[i] << '\n';
    //   assert(cnt[i] >= 0);
    // }

    while (cnt[i] > 0) {
      auto [bought, change] = transaction(P_i);
      
      for (int tp : bought) {
        cnt[tp]--;
      }
    }

    prv = P_i;
  }

  return;
}
#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...