Skip to content

Commit 8cd35e7

Browse files
committed
tco 04 round 4 500
1 parent 4e7da22 commit 8cd35e7

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Diff for: HeatDeath.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
17+
class HeatDeath{
18+
public:
19+
int maxTime(vi e){
20+
int n = e.size();
21+
int t = 0;
22+
while(true){
23+
sort(e.begin(), e.end());
24+
bool done = false;
25+
for(int i = 0; i+1 < n; i++)
26+
if(e[i+1]-e[i] >= 2){
27+
t++;
28+
e[i]++;
29+
e[i+1]--;
30+
done = true;
31+
i = n;
32+
}
33+
34+
if(done) continue;
35+
int j = 1;
36+
while(j < n && e[j] <= e[0]+1) j++;
37+
if(j < n){
38+
e[0]++;
39+
e[j]--;
40+
t++;
41+
}
42+
else break;
43+
}
44+
return t;
45+
}
46+
47+
// BEGIN CUT HERE
48+
public:
49+
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(); }
50+
private:
51+
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(); }
52+
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; } }
53+
void test_case_0() { int Arr0[] = { 5, 7, 9 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(0, Arg1, maxTime(Arg0)); }
54+
void test_case_1() { int Arr0[] = { 5, 6, 5, 6 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(1, Arg1, maxTime(Arg0)); }
55+
void test_case_2() { int Arr0[] = { 1, 1, 1, 1, 1, 999, 999, 999, 999, 999 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 12435; verify_case(2, Arg1, maxTime(Arg0)); }
56+
void test_case_3() { int Arr0[] = { 354, 903, 100, 951, 669, 311, 339, 500, 942, 72, 712, 54, 64,
57+
572, 7, 977, 74, 524, 314, 160, 526, 729, 114, 691, 771, 704,
58+
288, 47, 735, 85, 694, 124, 349, 905, 611, 371, 885, 738, 165,
59+
442, 138, 348, 605, 239, 535, 33, 142, 946, 4, 231 }
60+
; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 214090; verify_case(3, Arg1, maxTime(Arg0)); }
61+
62+
// END CUT HERE
63+
64+
};
65+
66+
// BEGIN CUT HERE
67+
int main(){
68+
69+
HeatDeath ___test;
70+
___test.run_test(-1);
71+
}
72+
// END CUT HERE

Diff for: HeatDeath.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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>
2+
The heat death of the universe occurs when all available energy has
3+
moved from &quot;hot&quot; (high energy) locations to &quot;cold&quot; (low energy)
4+
locations. Hot and cold are relative concepts--energy can flow from
5+
one location to another if and only if the energy level of the first location is at
6+
least 2 units greater than the energy level of the second location.
7+
Heat death occurs when there are no more locations between which energy can flow.
8+
Naturally, we would like to put off heat death for
9+
as long as possible.
10+
</p>
11+
12+
<p>
13+
You will be given a vector &lt;int&gt; <b>energy</b> that contains the
14+
energy levels of all locations in a closed system at time 0. Every microsecond, a random pair
15+
of locations whose energy levels differ by at least two units will be chosen,
16+
and a single unit of energy will flow from the hotter location to the colder location.
17+
Notice that the locations do not have to be adjacent.
18+
Depending on the order in which pairs of locations are chosen, heat death could arrive
19+
in different amounts of time. Calculate and return the maximum number of microseconds until
20+
the system reaches heat death.
21+
</p></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>HeatDeath</td></tr><tr><td>Method:</td><td>maxTime</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int maxTime(vector &lt;int&gt; energy)</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>energy</b> contains between 2 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>energy</b> is between 1 and 999, 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>{ 5, 7, 9 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Heat death could arrive in as little as 2 microseconds, if energy flows twice from the third location to the first location. On the other hand, if energy first flows from the third location to the second location, then from the third location to the first location, and finally from the second location to the first location, heat death can be put off for a total of 3 microseconds.
22+
(Other sequences may also achieve the maximum.)</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>{ 5, 6, 5, 6 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">No heat can flow. This system has already reached heat death.</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>{ 1, 1, 1, 1, 1, 999, 999, 999, 999, 999 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: 12435</pre></td></tr><tr><td></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>{ 354, 903, 100, 951, 669, 311, 339, 500, 942, 72, 712, 54, 64,
23+
572, 7, 977, 74, 524, 314, 160, 526, 729, 114, 691, 771, 704,
24+
288, 47, 735, 85, 694, 124, 349, 905, 611, 371, 885, 738, 165,
25+
442, 138, 348, 605, 239, 535, 33, 142, 946, 4, 231 }
26+
</pre></td></tr></table></td></tr><tr><td><pre>Returns: 214090</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)