Skip to content

Commit 8a21fb1

Browse files
committed
srm 645 div 2 250
1 parent 5b6e793 commit 8a21fb1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

BacteriesColony.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
18+
class BacteriesColony{
19+
public:
20+
vector <int> performTheExperiment(vector <int> a) {
21+
vi b = a;
22+
int n = a.size();
23+
vi c = b;
24+
do{
25+
c = b;
26+
for(int i = 1; i <= n-2; i++){
27+
if(a[i] > a[i-1] && a[i] > a[i+1])
28+
b[i]--;
29+
if(a[i] < a[i-1] && a[i] < a[i+1])
30+
b[i]++;
31+
}
32+
a = b;
33+
}
34+
while(c != b);
35+
return b;
36+
}
37+
38+
// BEGIN CUT HERE
39+
public:
40+
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(); }
41+
private:
42+
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(); }
43+
void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
44+
void test_case_0() { int Arr0[] = {5, 3, 4, 6, 1 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5, 4, 4, 4, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, performTheExperiment(Arg0)); }
45+
void test_case_1() { int Arr0[] = {1, 5, 4, 9 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 4, 5, 9 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, performTheExperiment(Arg0)); }
46+
void test_case_2() { int Arr0[] = {78, 34, 3, 54, 44, 99 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {78, 34, 34, 49, 49, 99 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, performTheExperiment(Arg0)); }
47+
void test_case_3() { int Arr0[] = {32, 68, 50, 89, 34, 56, 47, 30, 82, 7, 21, 16, 82, 24, 91 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {32, 59, 59, 59, 47, 47, 47, 47, 47, 18, 18, 19, 53, 53, 91 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, performTheExperiment(Arg0)); }
48+
49+
// END CUT HERE
50+
51+
};
52+
53+
// BEGIN CUT HERE
54+
int main(){
55+
56+
BacteriesColony ___test;
57+
___test.run_test(-1);
58+
}
59+
// END CUT HERE

BacteriesColony.html

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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>Grazyna works in a laboratory.
2+
Her team is about to perform an experiment with special bacteria.
3+
</p>
4+
<p>
5+
There is a row of vessels.
6+
Each vessel contains a colony of bacteria.
7+
The colonies may currently have different sizes.
8+
Namely, for each valid i there are currently <b>colonies</b>[i] bacteria in the i-th vessel (0-based index).
9+
</p>
10+
<p>
11+
Grazyna has a theory about how the colony sizes change.
12+
According to the theory, the colony sizes only change during nights and it only happens in two specific cases:
13+
<ul>
14+
<li>If during a day a colony finds itself immediately between two larger colonies, during the next night its size will increase by 1.</li>
15+
<li>If during a day a colony finds itself immediately between two smaller colonies, during the next night its size will decrease by 1.</li>
16+
</ul>
17+
Note that the colonies in the first and last vessel never change their sizes as they are never between two colonies.
18+
Also note that multiple colonies can change their size each night.
19+
</p>
20+
<p>
21+
The experiment will go on for as long as some colonies keep changing their sizes.
22+
You are given the vector &lt;int&gt; <b>colonies</b>.
23+
Compute the result of the experiment, assuming that the colony sizes change according to Grazyna's theory.
24+
Return a vector &lt;int&gt; with the same number of elements as <b>colonies</b>.
25+
For each valid i, element i of the return value should be the final size of the colony in the i-th vessel.
26+
</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>BacteriesColony</td></tr><tr><td>Method:</td><td>performTheExperiment</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>vector &lt;int&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;int&gt; performTheExperiment(vector &lt;int&gt; colonies)</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>256</td></tr><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>For each valid input the experiment will terminate after finitely many steps.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>colonies</b> will have between 3 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>colonies</b> will be between 1 and 100, 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, 3, 4, 6, 1 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: {5, 4, 4, 4, 1 }</pre></td></tr><tr><td><table><tr><td colspan="2"><ul>
27+
<li>The colony sizes during the first day are given as input: {5, 3, 4, 6, 1}.</li>
28+
<li>During the first night colony 1 (0-based index) will grow and colony 3 will shrink.</li>
29+
<li>During the second day the colony sizes will be {5, 4, 4, 5, 1}.</li>
30+
<li>During the second night colony 3 will shrink again.</li>
31+
<li>During the third day the colony sizes will be {5, 4, 4, 4, 1}.</li>
32+
<li>There are no more changes during the third night, so that is the final state and the experiment ends.</li>
33+
</ul></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, 5, 4, 9 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: {1, 4, 5, 9 }</pre></td></tr><tr><td><table><tr><td colspan="2">During the first night colony 1 will shrink from 5 to 4 and at the same time colony 2 will grow from 4 to 5. Afterwards there will be no more changes.</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>{78, 34, 3, 54, 44, 99 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: {78, 34, 34, 49, 49, 99 }</pre></td></tr><tr><td><table><tr><td colspan="2"></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>{32, 68, 50, 89, 34, 56, 47, 30, 82, 7, 21, 16, 82, 24, 91 }</pre></td></tr></table></td></tr><tr><td><pre>Returns: {32, 59, 59, 59, 47, 47, 47, 47, 47, 18, 18, 19, 53, 53, 91 }</pre></td></tr><tr><td><table><tr><td colspan="2"></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)