Skip to content

Commit 800170f

Browse files
committed
srm 222 div 1 250
1 parent 175c2a0 commit 800170f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

GroceryBagger.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class GroceryBagger{
19+
public:
20+
int minimumBags(int strength, vector <string> itemType) {
21+
map<string, int> M;
22+
for(string s : itemType) M[s]++;
23+
int ret = 0;
24+
for(pair<string, int> p : M) ret += (p.second+strength-1)/strength;
25+
return ret;
26+
}
27+
28+
// BEGIN CUT HERE
29+
public:
30+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
31+
private:
32+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
33+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
34+
void test_case_0() { int Arg0 = 2; string Arr1[] = {"DAIRY",
35+
"DAIRY",
36+
"PRODUCE",
37+
"PRODUCE",
38+
"PRODUCE",
39+
"MEAT"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, minimumBags(Arg0, Arg1)); }
40+
void test_case_1() { int Arg0 = 3; string Arr1[] = {"DAIRY",
41+
"DAIRY",
42+
"PRODUCE",
43+
"PRODUCE",
44+
"PRODUCE",
45+
"MEAT"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(1, Arg2, minimumBags(Arg0, Arg1)); }
46+
void test_case_2() { int Arg0 = 10; string Arr1[] = {}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(2, Arg2, minimumBags(Arg0, Arg1)); }
47+
void test_case_3() { int Arg0 = 5; string Arr1[] = {"CANNED", "CANNED", "PRODUCE",
48+
"DAIRY", "MEAT", "BREAD",
49+
"HOUSEHOLD","PRODUCE", "FROZEN",
50+
"PRODUCE", "DAIRY"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 7; verify_case(3, Arg2, minimumBags(Arg0, Arg1)); }
51+
void test_case_4() { int Arg0 = 2; string Arr1[] = {"CANNED", "CANNED", "PRODUCE",
52+
"DAIRY", "MEAT", "BREAD",
53+
"HOUSEHOLD","PRODUCE", "FROZEN",
54+
"PRODUCE", "DAIRY"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 8; verify_case(4, Arg2, minimumBags(Arg0, Arg1)); }
55+
56+
// END CUT HERE
57+
58+
};
59+
60+
// BEGIN CUT HERE
61+
int main(){
62+
63+
GroceryBagger ___test;
64+
___test.run_test(-1);
65+
}
66+
// END CUT HERE

GroceryBagger.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>You have just begun working as a grocery bagger at the local TopGrocer food store. Your job is to place all of a customer's items into bags, so they can be carried from the store. Your manager has instructed you to use as few bags as possible, to minimize the store's overall cost. However, for the customer's convenience, you are instructed that only items of the same type can be placed in the same bag. For instance, a produce item can be bagged with any other produce items, but not with dairy items.</p>
2+
3+
<p>You are given a vector &lt;string&gt; <b>itemType</b> indicating the type of each item that needs to be bagged. You are also given an int <b>strength</b> indicating the maximum number of items that can be placed in each bag. You are to return an int indicating the minimum number of bags required to package the customer's items.</p>
4+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>GroceryBagger</td></tr><tr><td>Method:</td><td>minimumBags</td></tr><tr><td>Parameters:</td><td>int, vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int minimumBags(int strength, vector &lt;string&gt; itemType)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>strength</b> will be between 1 and 50, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>itemType</b> will contain between 0 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>itemType</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>itemType</b> will contain only the characters 'A'-'Z'.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>2</pre></td></tr><tr><td><pre>{&quot;DAIRY&quot;,
5+
&quot;DAIRY&quot;,
6+
&quot;PRODUCE&quot;,
7+
&quot;PRODUCE&quot;,
8+
&quot;PRODUCE&quot;,
9+
&quot;MEAT&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">Here, you have six items. You could put two items in each bag, but you would have to mix item types. The single meat item must get its own bag. The two dairy items fit in one bag. The three produce items will require two bags.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>3</pre></td></tr><tr><td><pre>{&quot;DAIRY&quot;,
10+
&quot;DAIRY&quot;,
11+
&quot;PRODUCE&quot;,
12+
&quot;PRODUCE&quot;,
13+
&quot;PRODUCE&quot;,
14+
&quot;MEAT&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Similar to above, but now we have stronger bags. Note again, though, that if we were allowed to mix item types, we could get away with only 2 bags. But since item types cannot be mixed, we need 3 bags.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>10</pre></td></tr><tr><td><pre>{}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">The bags are really strong, but we didn't buy anything.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>5</pre></td></tr><tr><td><pre>{&quot;CANNED&quot;, &quot;CANNED&quot;, &quot;PRODUCE&quot;,
15+
&quot;DAIRY&quot;, &quot;MEAT&quot;, &quot;BREAD&quot;,
16+
&quot;HOUSEHOLD&quot;,&quot;PRODUCE&quot;, &quot;FROZEN&quot;,
17+
&quot;PRODUCE&quot;, &quot;DAIRY&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 7</pre></td></tr><tr><td><table><tr><td colspan="2">Notice that a customer doesn't necessarily pay for the items in any particular order, but the bagger still has to be responsible for sorting them out. In this case, though, one bag for each item type suffices.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>2</pre></td></tr><tr><td><pre>{&quot;CANNED&quot;, &quot;CANNED&quot;, &quot;PRODUCE&quot;,
18+
&quot;DAIRY&quot;, &quot;MEAT&quot;, &quot;BREAD&quot;,
19+
&quot;HOUSEHOLD&quot;,&quot;PRODUCE&quot;, &quot;FROZEN&quot;,
20+
&quot;PRODUCE&quot;, &quot;DAIRY&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 8</pre></td></tr><tr><td><table><tr><td colspan="2">As above, but our produce requires two bags now.</td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)