forked from aseembrahma/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuminator.cpp
217 lines (194 loc) · 4.99 KB
/
Suminator.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <sstream>
#include <queue>
#include <string>
#include <cmath>
#include <set>
#include <limits>
#include <numeric>
#include <bitset>
#include <stack>
#include <utility>
#include <unordered_set>
#define DEBUG 0
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
#define INF_INT numeric_limits<int>::max()
#define MIN_INT numeric_limits<int>::min()
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> pii;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
template <typename K, typename V>
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() ) {
return defval;
}
else {
return it->second;
}
}
template<typename T>
void print_vector(const vector<T> &v) {
cerr << " { ";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
cerr << *it << ", ";
}
cerr << " } " << endl;
}
class Suminator {
/*
Rather than trying to replace -1 with some number and summing, keep track
of the indices that would get summed up if the -1 were replace by 0, and if
it were replaced by some positive number
*/
public:
int findMissing(vector<int> program, int wantedResult) {
vector<int> empty;
{
// If -1 were replaced by 0
stack<vector<int> > s;
for (int i = 0; i < program.size(); ++i) {
if (0 == program[i] || -1 == program[i]) {
vector<int> u(empty);
if (!s.empty()) {
u = s.top();
s.pop();
}
vector<int> v(empty);
if (!s.empty()) {
v = s.top();
s.pop();
}
vector<int> w(u.begin(), u.end());
w.insert(w.end(), v.begin(), v.end());
//print_vector(w);
if (!w.empty())
s.push(w);
}
else {
vector<int> u(1, i);
//print_vector(u);
s.push(u);
}
}
int sum = 0;
vector<int> u(empty);
if (!s.empty()) {
u = s.top();
}
//print_vector(u);
for (int i = 0; i < u.size(); ++i) {
if (program[u[i]] != -1) {
int new_sum = sum + program[u[i]];
if (new_sum < sum)
return -1;
else
sum = new_sum;
}
}
if (sum == wantedResult)
return 0;
}
{
// if -1 were replaced by a positive number
int idx = 0;
stack<vector<int> > s;
for (int i = 0; i < program.size(); ++i) {
if (0 == program[i]) {
vector<int> u(empty);
if (!s.empty()) {
u = s.top();
s.pop();
}
vector<int> v(empty);
if (!s.empty()) {
v = s.top();
s.pop();
}
vector<int> w(u.begin(), u.end());
w.insert(w.end(), v.begin(), v.end());
if (!w.empty())
s.push(w);
}
else {
if (-1 == program[i])
idx = i;
vector<int> u(1, i);
s.push(u);
}
}
int sum = 0;
vector<int> u(empty);
if (!s.empty()) {
u = s.top();
}
if (find(u.begin(), u.end(), idx) == u.end())
return -1;
for (int i = 0; i < u.size(); ++i) {
if (program[u[i]] != -1) {
int new_sum = sum + program[u[i]];
if (new_sum < sum)
return -1;
else
sum = new_sum;
}
}
if (wantedResult > sum)
return (wantedResult - sum);
else
return -1;
}
return -1;
}
bool runTest(vector<int> program, int wantedResult, int solution) {
int output = findMissing(program, wantedResult);
if (output != solution)
{
cerr << "Failed for " << wantedResult << endl;
print_vector(program);
cerr << "Expected: " << solution << endl;
cerr << "Found instead:" << output << endl;
return false;
}
else {
return true;
}
}
};
int main(int argc, char* argv[]) {
Suminator test;
{
test.runTest({7,-1,0}, 10, 3);
}
{
test.runTest({100, 200, 300, 0, 100, -1}, 600, 0);
}
{
test.runTest({-1, 7, 3, 0, 1, 2, 0, 0}, 13, 0);
}
{
test.runTest({-1, 8, 4, 0, 1, 2, 0, 0}, 16, -1);
}
{
test.runTest({1000000000, 1000000000, 1000000000, 1000000000, -1, 0, 0, 0, 0}, 1000000000, -1);
}
}