Submission #1269868

#TimeUsernameProblemLanguageResultExecution timeMemory
1269868strange420Souvenirs (IOI25_souvenirs)C++20
53 / 100
12 ms408 KiB
#include "souvenirs.h"
#include <utility>
#include <vector>
#include <iostream>

void subtask3(int N, long long P0) {
  // Subtask 3
  std::vector<int> cnt(N, 0);
  std::vector<int> price(N, 0);

  long long current_price = P0 - 1;
  while (price[N-1] == 0) {
    std::pair<std::vector<int>, long long> res = transaction(current_price);

    for (auto x: res.first) {
      cnt[x]++;
    }

    if (res.first.size() == 1 && res.second == 0) {
      int i = res.first[0];
      price[i] = current_price;
      current_price --;
    } else {
      int i = res.first[0];
      price[i] = current_price - 1;
      current_price -= 2;
    }
  }

  for (int i=1; i<N; i++) {
    for (int j = cnt[i]; j < i; j++) {
      transaction(price[i]);
    }
  }
}

std::pair<std::vector<int>, long long> t(long long M, std::vector<int> &cnt, std::vector<long long> &price) {
  std::pair<std::vector<int>, long long> res = transaction(M);
  for (auto x: res.first) {
    cnt[x]++;
  }

  if (res.first.size() == 1) {
    price[res.first[0]] = M - res.second;
  }

  return res;
}

void half(long long P0, int target, std::vector<int> &cnt, std::vector<long long> &price, std::vector<std::pair<std::vector<int>, long long>> &equations) {
  long long current_price = P0 - 1;
  for (int i=1; price[target] == 0; i++) {
    std::pair<std::vector<int>, long long> res = t(current_price, cnt, price);
    equations.push_back(std::pair(res.first, current_price - res.second));

    if (res.second == 0 && res.first.size() == 1) {
      current_price--;
    } else {
      current_price = (current_price - res.second)/res.first.size();
    }
  }
}

void subtask5(int N, long long P0) {
  // Subtask 5
  std::vector<int> cnt(N, 0);
  std::vector<std::pair<std::vector<int>, long long>> equations;
  std::vector<long long> price(N+1, 0); price[0] = P0; price[N] = 1;
  half(P0, N-1, cnt, price, equations);

  for (int i=N-2; i>1; i--) {
    if (price[i]) continue;
    std::pair<std::vector<int>, long long> res = t(2*price[i+1], cnt, price);

    if (res.first.size() == 1) {
      price[i] = price[i+1]*2 - res.second;
      continue;
    }

    int zeros = 0, ind = -1;
    long long part = 0;
    std::vector<int> asd;
    for (int x: res.first) {
      if (!price[x]) {
        zeros++;
        ind = x;
        asd.push_back(x);
      } else {
        part += price[x];
      }
    }

    if (zeros == 1) {
      price[ind] = price[i+1]*2 - (res.second + part);
    } else {
      equations.push_back(std::pair(asd, res.second - part));
    }
  }

  for (int i=0; i<N; i++) {
    while (cnt[i] < i) {
      transaction(price[i]);
      cnt[i]++;
    }
  }
}

void buy_souvenirs(int N, long long P0) {
  if (N == 2) {
    // Subtask 1
    transaction(P0 - 1);
  } else if (P0 == N) {
    // Subtask 2
    for (long long i = N-1, k = 1; i>0; i--, k++) {
      for (int j = 0; j < k; j++) {
        transaction(i);
      }
    }
  } else if (N == 3) {
    // Subtask 4
    std::pair<std::vector<int>, long long> res = transaction(P0-1);
    long long price = P0-1-res.second;
    if (res.first.size() == 1) {
      transaction(price-1);
      transaction(price-1);
    } else {
      long long special = price / 2;
      transaction(special);
    }
  } else {
    subtask5(N, P0);
  }
}
#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...