2022/sector/10/pyramid solution
This commit is contained in:
68
2022/sector/10/pyramid/pyramid.cpp
Normal file
68
2022/sector/10/pyramid/pyramid.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream in("test.in");
|
||||
ofstream out("test.out");
|
||||
|
||||
|
||||
unordered_set<int> 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;
|
||||
}
|
||||
Reference in New Issue
Block a user