-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfStacks.cpp
95 lines (75 loc) · 2.38 KB
/
GameOfStacks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// GameOfStacks.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stack>
#include <iostream>
using namespace std;
void removeOneOfAlphas(stack<int> &elementsFromAlpha, int &sum, int& score) {
if (!elementsFromAlpha.empty()) {
sum -= elementsFromAlpha.top();
elementsFromAlpha.pop();
score--;
}
}
int biggerOfTwo(int first, int second) {
if (first > second)return first;
else return second;
}
void initStack(stack<int> &sourceStack, stack<int> &scoreStack, int &sum, int &score, int limit) {
while (!sourceStack.empty() && sourceStack.top() + sum < limit) {
sum += sourceStack.top();
scoreStack.push(sourceStack.top());
sourceStack.pop();
score++;
}
}
void saturateScore(stack<int> &sourceStack, int &sum, int &score, int limit) {
while (!sourceStack.empty() && sourceStack.top() + sum < limit) {
sum += sourceStack.top();
sourceStack.pop();
score++;
}
}
int playTheGame(stack<int> stackAlpha, stack<int> stackBravo, int limit) {//might be doable with a "greedy" algorythm
//nope. It's not doable with a greedy algorythm.
stack<int> elementsFromAlpha;//size of auxStack is NOT current score
int sum = 0, score = 0, maxScore = 0;
initStack(stackAlpha, elementsFromAlpha, sum, score, limit);
saturateScore(stackBravo, sum, score, limit);
maxScore = biggerOfTwo(score, maxScore);
while (!elementsFromAlpha.empty()) {
removeOneOfAlphas(elementsFromAlpha, sum, score);
saturateScore(stackBravo, sum, score, limit);
maxScore = biggerOfTwo(score, maxScore);
}
return maxScore;
}
void fillStack(stack<int> &targetStack, int stackSize) {
stack<int> reverseStack;
for (int i = 0; i < stackSize; i++) {
int input;
cin >> input;
reverseStack.push(input);
}
for (int i = 0; i < stackSize; i++) {
targetStack.push(reverseStack.top());
reverseStack.pop();
}
}
int main() {
int numberOfGames;
cin >> numberOfGames;
int* gameResults = new int[numberOfGames];
for (int gameIterator = 0; gameIterator < numberOfGames; gameIterator++) {
int stackAlphaSize, stackBravoSize, limit;
stack<int> inputStack, stackAlpha, stackBravo;
cin >> stackAlphaSize >> stackBravoSize >> limit;
fillStack(stackAlpha, stackAlphaSize);
fillStack(stackBravo, stackBravoSize);
gameResults[gameIterator] = playTheGame(stackAlpha, stackBravo, limit);
}
for (int i = 0; i < numberOfGames; i++) {
cout << gameResults[i] << endl;
}
return 0;
}