-
Notifications
You must be signed in to change notification settings - Fork 0
/
1010_stamps.cpp
171 lines (163 loc) · 3.62 KB
/
1010_stamps.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
#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct ResList {
vector<int> res;
bool tie;
ResList()
: tie(false)
{}
ResList(const vector<int>& res, bool tie)
: res(res), tie(tie)
{}
};
ostream& operator<<(ostream& out, const vector<int> list)
{
out << "list=";
for (size_t i=0; i<list.size(); ++i)
out << list[i] << " ";
return out;
}
ostream& operator<<(ostream& out, const ResList& resList)
{
out << resList.res << endl
<< "tie=" << resList.tie;
return out;
}
void compRes(ResList& resList, const ResList& newResList, int stamps[])
{
// cout << "CompRes: resList=" << resList << endl
// << " newResList=" << newResList << endl;
if (resList.res.size() == 0) {
resList = newResList;
return ;
}
set<int> resSet(resList.res.begin(), resList.res.end());
set<int> newResSet(newResList.res.begin(), newResList.res.end());
int resLargest = stamps[*resSet.rbegin()];
int newResLargest = stamps[*newResSet.rbegin()];
if (newResSet.size() < resSet.size()) // most type
{
return ;
} else if (newResSet.size() == resSet.size())
{
if (newResList.res.size() > resList.res.size()) // fewest total
return ;
else if ( newResList.res.size() == resList.res.size())
{
if ( resLargest > newResLargest)
return ;
else if (resLargest == newResLargest)
{
// cout << "*****tie*****"<< endl;
resList.tie = true;
return ;
}
}
}
resList = newResList;
}
// resList return index
ResList findPair(int customer, int n, int index, int stamps[], const vector<int> currList)
{
// cout << "cus=" << customer << "n=" << n << "currList=" << currList << endl;
if (customer == 0)
{
return ResList(currList, false);
}
if (n == 0) {
return ResList();
}
ResList resList;
for (size_t i=0; i<index; ++i)
{
vector<int> newList = currList;
if (customer >= stamps[i]
&& (currList.size() == 0 || (currList.size() > 0 && *currList.rbegin() <= i) ) )
{
newList.push_back(i);
ResList newResList = findPair(customer-stamps[i], n-1, index, stamps, newList);
newList.erase(newList.end()-1);
if (newResList.res.size() > 0)
compRes(resList, newResList, stamps);
}
}
return resList;
}
void preProcess(int& index, int stamps[])
{
if (index == 0)
return ;
// sort
for (int i=0;i<index-1;++i)
for (int j=i+1; j< index; ++j)
if (stamps[i]>stamps[j])
{
int x = stamps[i];
stamps[i] = stamps[j];
stamps[j] =x;
}
int newIndex=1;
int currValue = stamps[0];
int count = 1;
for (int i=1; i< index; ++i)
{
if (stamps[i] == currValue)
{
++count;
if (count > 5) {
continue;
} else {
stamps[newIndex++]=stamps[i];
}
} else {
stamps[newIndex++] = stamps[i];
currValue = stamps[i];
count =1;
}
}
index = newIndex;
// for (int i = 0; i< index; ++i)
// cout << stamps[i] << " ";
// cout << endl;
}
void cal(int customer, int index, int stamps[])
{
preProcess(index, stamps);
ResList resList = findPair(customer, 4, index, stamps, vector<int>() );
set<int> resSet(resList.res.begin(), resList.res.end());
if (resList.res.size() == 0)
cout << customer << " ---- none " << endl;
else if ( !resList.tie)
{
cout << customer << " (" << resSet.size() << "):";
for (size_t i=0;i<resList.res.size(); ++i)
cout << " " << stamps[resList.res[i]];
cout << endl;
} else
{
cout << customer << " ("<< resSet.size() << "): tie" << endl;
}
}
int main()
{
int stamps[100];
int index = -1;
while (cin >> stamps[++index])
{
if (stamps[index] == 0)
{
int customer;
while (cin >> customer)
{
if (customer == 0){
index = -1; // next set
break;
}
cal(customer, index, stamps);
}
}
}
return 0;
}