Skip to content

Commit 1f7bd83

Browse files
committed
tco 04 round 3 250
1 parent 03320ff commit 1f7bd83

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

Diff for: Boxing.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
struct interval{
18+
int s, f;
19+
interval(int s, int f) : s(s), f(f) {}
20+
};
21+
22+
bool operator<(interval a, interval b){
23+
return a.f < b.f || (a.f == b.f && a.s < b.s);
24+
}
25+
26+
class Boxing{
27+
public:
28+
int maxCredit(vector <int> a, vector <int> b, vector <int> c, vector <int> d, vector <int> e) {
29+
set<interval> S;
30+
string s = "00111";
31+
do{
32+
vvi t(3);
33+
int k = 0;
34+
for(int i = 0; i < 5; i++){
35+
if(s[i] == '1'){
36+
switch(i){
37+
case 0:
38+
t[k] = a;
39+
break;
40+
case 1:
41+
t[k] = b;
42+
break;
43+
case 2:
44+
t[k] = c;
45+
break;
46+
case 3:
47+
t[k] = d;
48+
break;
49+
case 4:
50+
t[k] = e;
51+
break;
52+
}
53+
k++;
54+
}
55+
}
56+
57+
for(int x : t[0])
58+
for(int y : t[1])
59+
for(int z : t[2]){
60+
int mi = min(x, min(y, z)), mx = max(x, max(y, z));
61+
if(mx - mi <= 1000)
62+
S.insert(interval(mi, mx));
63+
}
64+
}
65+
while(next_permutation(s.begin(), s.end()));
66+
int lf = -1, r = 0;
67+
for(interval t : S)
68+
if(lf < t.s){
69+
lf = t.f;
70+
r++;
71+
}
72+
73+
return r;
74+
}
75+
76+
// BEGIN CUT HERE
77+
public:
78+
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(); }
79+
private:
80+
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(); }
81+
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; } }
82+
void test_case_0() { int Arr0[] = {1,2,3,4,5,6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,3,4,5,6,7}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {1,2,3,4,5,6}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {0,1,2}; vector <int> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arr4[] = {1,2,3,4,5,6,7,8}; vector <int> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 6; verify_case(0, Arg5, maxCredit(Arg0, Arg1, Arg2, Arg3, Arg4)); }
83+
void test_case_1() { int Arr0[] = {100,200,300,1200,6000}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {900,902,1200,4000,5000,6001}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {0,2000,6002}; vector <int> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arr4[] = {1,2,3,4,5,6,7,8}; vector <int> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 3; verify_case(1, Arg5, maxCredit(Arg0, Arg1, Arg2, Arg3, Arg4)); }
84+
void test_case_2() { int Arr0[] = {5000,6500}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {6000}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arr2[] = {6500}; vector <int> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); int Arr3[] = {6000}; vector <int> Arg3(Arr3, Arr3 + (sizeof(Arr3) / sizeof(Arr3[0]))); int Arr4[] = {0,5800,6000}; vector <int> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); int Arg5 = 1; verify_case(2, Arg5, maxCredit(Arg0, Arg1, Arg2, Arg3, Arg4)); }
85+
86+
// END CUT HERE
87+
88+
};
89+
90+
// BEGIN CUT HERE
91+
int main(){
92+
93+
Boxing ___test;
94+
___test.run_test(-1);
95+
}
96+
// END CUT HERE

Diff for: Boxing.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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>In Olympic boxing, there are five judges who press a button when they think
2+
that a particular boxer has just landed a punch. The times of the button
3+
presses are recorded, and the boxer receives credit for a punch if at least
4+
three of the judges press their buttons within 1 second of each other.
5+
<p>
6+
This &quot;algorithm&quot; needs a lot of refinement. Here is the version that you
7+
should implement.
8+
Find the maximum number of disjoint time intervals that can be chosen such that
9+
each interval is of duration 1 second or less and contains button presses from
10+
at least 3 different judges. Two intervals are disjoint if every time within one interval is
11+
strictly less than every time in the other. We give the boxer credit for one punch during
12+
each interval.
13+
</p><p>
14+
The duration of an interval is the amount of time between the instant when it starts and when it
15+
ends, and a punch may be included in an interval if its recorded time is
16+
at the start, end, or in between.
17+
So, for example, the interval from 1 to 4 has duration 3, and a punch recorded at time 1, 2, 3, or 4
18+
is in that interval. The intervals 1 to 4 and 5 to 22 are disjoint, but the intervals 1 to 4 and 4 to
19+
22 are not disjoint.
20+
</p><p>
21+
Create a class Boxing that contains a method maxCredit that is given five vector &lt;int&gt;s,
22+
<b>a</b>, <b>b</b>, <b>c</b>, <b>d</b>, and <b>e</b>, representing the times of the button presses of the five judges
23+
in milliseconds. The method returns the maximum credits that can be given to the
24+
boxer.
25+
</p>
26+
</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>Boxing</td></tr><tr><td>Method:</td><td>maxCredit</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, vector &lt;int&gt;, vector &lt;int&gt;, vector &lt;int&gt;, vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int maxCredit(vector &lt;int&gt; a, vector &lt;int&gt; b, vector &lt;int&gt; c, vector &lt;int&gt; d, vector &lt;int&gt; e)</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>Each of the five arguments will contain between 0 and 50 elements inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of each of the arguments will be between 0 and 180,000 inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The elements within each of the arguments will be in strictly increasing order.</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,2,3,4,5,6}</pre></td></tr><tr><td><pre>{1,2,3,4,5,6,7}</pre></td></tr><tr><td><pre>{1,2,3,4,5,6}</pre></td></tr><tr><td><pre>{0,1,2}</pre></td></tr><tr><td><pre>{1,2,3,4,5,6,7,8}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 6</pre></td></tr><tr><td><table><tr><td colspan="2">
27+
28+
This match had a fast start, with 6 punches credited in the first 6
29+
milliseconds of the match! One way to choose 6 disjoint intervals is
30+
to choose [1,1], [2,2], [3,3], [4,4], [5,5], [6,6] each of which
31+
contains button presses from judges a, b, and c.
32+
There are three button presses in the time interval from
33+
7 through 8, but
34+
only from two different judges so no additional credit can be given.
35+
36+
</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>{100,200,300,1200,6000}</pre></td></tr><tr><td><pre>{}</pre></td></tr><tr><td><pre>{900,902,1200,4000,5000,6001}</pre></td></tr><tr><td><pre>{0,2000,6002}</pre></td></tr><tr><td><pre>{1,2,3,4,5,6,7,8}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">
37+
38+
One way to form three intervals is [0,1000], [1001,2000], [6000,6002]
39+
</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>{5000,6500}</pre></td></tr><tr><td><pre>{6000}</pre></td></tr><tr><td><pre>{6500}</pre></td></tr><tr><td><pre>{6000}</pre></td></tr><tr><td><pre>{0,5800,6000}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">
40+
Any interval that doesn't include 6000 will not have enough button presses,
41+
so we can form only one disjoint interval with credit for a punch.
42+
</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)