Skip to content

Commit a5975bd

Browse files
committed
tco 04 round 2 div 1 200
1 parent 0513db4 commit a5975bd

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

SchoolAssembly.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 SchoolAssembly{
19+
public:
20+
int getBeans(int k, int q) {
21+
int x = 0, p = 0;
22+
int a[5] = {0, 0, 0, 0, 0};
23+
while(true){
24+
++x;
25+
int total = 20;
26+
for(int i = 0; i < 5; i++){
27+
int j = (p+i)%5;
28+
int a1 = a[j]%q;
29+
if(i == 4){
30+
p = j;
31+
a[j] += total;
32+
}
33+
else{
34+
if(total == 0) continue;
35+
int a2 = min(total, q-1-a1);
36+
a[j] += a2;
37+
total -= a2;
38+
}
39+
}
40+
int n = 0;
41+
for(int i = 0; i < 5; i++) n += a[i]/q;
42+
if(n >= k) return x;
43+
}
44+
}
45+
46+
// BEGIN CUT HERE
47+
public:
48+
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(); }
49+
private:
50+
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(); }
51+
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; } }
52+
void test_case_0() { int Arg0 = 1; int Arg1 = 5; int Arg2 = 2; verify_case(0, Arg2, getBeans(Arg0, Arg1)); }
53+
void test_case_1() { int Arg0 = 1; int Arg1 = 2; int Arg2 = 1; verify_case(1, Arg2, getBeans(Arg0, Arg1)); }
54+
void test_case_2() { int Arg0 = 5; int Arg1 = 5; int Arg2 = 3; verify_case(2, Arg2, getBeans(Arg0, Arg1)); }
55+
void test_case_3() { int Arg0 = 223; int Arg1 = 15; int Arg2 = 171; verify_case(3, Arg2, getBeans(Arg0, Arg1)); }
56+
57+
// END CUT HERE
58+
59+
};
60+
61+
// BEGIN CUT HERE
62+
int main(){
63+
64+
SchoolAssembly ___test;
65+
___test.run_test(-1);
66+
}
67+
// END CUT HERE

SchoolAssembly.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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>You are a director of a school, having an assembly for your kids. The assembly is on the topic of consistency, and as a demonstration, you want to hand out a number of jelly beans to each kid, and have all the jelly beans that an individual kid receives be the same color (one kid's color may be different than another kid's color). At the store, each bag of jelly beans contains exactly 20 beans, and each bean can be one of 5 colors. The bags are opaque, so you cannot determine ahead of time exactly how many of each color bean are in each bag (a bag could have any combination of the 5 colors, including 20 of one color). Given an int <b>kids</b> (the number of children) and an int <b>quantity</b> (the number of beans you want to give each kid), return how many bags of jelly beans you must buy to ensure that you have enough jelly beans to give each individual kid <b>quantity</b> same-colored beans.</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>SchoolAssembly</td></tr><tr><td>Method:</td><td>getBeans</td></tr><tr><td>Parameters:</td><td>int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getBeans(int kids, int quantity)</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>kids</b> will be between 1 and 1000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>quantity</b> will be between 2 and 25, inclusive.</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>1</pre></td></tr><tr><td><pre>5</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">Only one kid to buy for. Since the first bag you buy could contain 4 of each color, you must buy a second bag to ensure the child has 5 of the same color.</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>1</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td></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>5</pre></td></tr><tr><td><pre>5</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">If we buy 2 bags, we will be able to give 4 of the children 5 beans. However, we could be left with 4 beans of each color, requiring the third bag.</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>223</pre></td></tr><tr><td><pre>15</pre></td></tr></table></td></tr><tr><td><pre>Returns: 171</pre></td></tr><tr><td></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)