From 30f71c56047007ab671c23721daa3bdd0f307062 Mon Sep 17 00:00:00 2001 From: oleg20111511 Date: Wed, 23 Apr 2025 18:35:06 +0300 Subject: [PATCH] 2022/sector/10/pyramid solution --- .../sector/10/numere/{main.cpp => numere.cpp} | 0 2022/sector/10/pyramid/pyramid.cpp | 68 +++++++++++++++++++ 2 files changed, 68 insertions(+) rename 2022/sector/10/numere/{main.cpp => numere.cpp} (100%) create mode 100644 2022/sector/10/pyramid/pyramid.cpp diff --git a/2022/sector/10/numere/main.cpp b/2022/sector/10/numere/numere.cpp similarity index 100% rename from 2022/sector/10/numere/main.cpp rename to 2022/sector/10/numere/numere.cpp diff --git a/2022/sector/10/pyramid/pyramid.cpp b/2022/sector/10/pyramid/pyramid.cpp new file mode 100644 index 0000000..931c2f7 --- /dev/null +++ b/2022/sector/10/pyramid/pyramid.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +ifstream in("test.in"); +ofstream out("test.out"); + + +unordered_set cardRequirements; + + +// Pre-fill a set containing possible amounts of cards required to build towers +void fillRequirements() { + cardRequirements.insert(2); + // At height 1: + int levelCards = 2; + int cards = 2; + // (1 <= n <= 10 * 9) + int stop = pow(10, 9); + + // height > 1 + while (cards <= stop) { + // cout << "calculating for cards " << cards << endl; + levelCards += 3; + cards += levelCards; + cardRequirements.insert(cards); + } +} + + +void solveTestCase() { + int cards; + in >> cards; + // cout << "here" << endl; + + if (cards == 1) { + out << 0 << endl; + return; + } + + int towerAmount = 0; + while (cards > 1) { + int cardsToUse; + // Go backwards in amount of cards and check if that amount can be used to build a valid tower + for (cardsToUse = cards; cardsToUse > 1; cardsToUse--) { + if (cardRequirements.count(cardsToUse) == 1) { + towerAmount++; + cards -= cardsToUse; + break; + } + } + } + out << towerAmount << endl; +} + + +int main() { + fillRequirements(); + + int t; + in >> t; + + for (int i = 0; i < t; i++) { + solveTestCase(); + } + + return 0; +}