Skip to content

Commit 7311a6f

Browse files
committed
srm 204 div 1 300
1 parent fb33b57 commit 7311a6f

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Apothecary.cpp

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 Apothecary{
19+
public:
20+
int next(int x){
21+
int i = 1;
22+
while(i < x) i *= 3;
23+
return i;
24+
}
25+
vi balance(int W){
26+
vi v[2];
27+
int n = W, m = 1;
28+
while(true){
29+
int k = next(n);
30+
if(k/2 < n){
31+
v[m].push_back(k);
32+
if(k == n) break;
33+
n = k-n;
34+
m = (m+1)%2;
35+
}
36+
else{
37+
v[m].push_back(k/3);
38+
n = n-k/3;
39+
}
40+
}
41+
vi r;
42+
for(int p : v[0]) r.push_back(-p);
43+
for(int p : v[1]) r.push_back(p);
44+
sort(r.begin(), r.end());
45+
return r;
46+
}
47+
48+
// BEGIN CUT HERE
49+
public:
50+
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(); }
51+
private:
52+
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(); }
53+
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; } }
54+
void test_case_0() { int Arg0 = 17; int Arr1[] = { -9, -1, 27 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, balance(Arg0)); }
55+
void test_case_1() { int Arg0 = 1; int Arr1[] = { 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, balance(Arg0)); }
56+
void test_case_2() { int Arg0 = 2016; int Arr1[] = { -243, -9, 81, 2187 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, balance(Arg0)); }
57+
void test_case_3() { int Arg0 = 1000000; int Arr1[] = { -531441, -59049, -6561, -243, -27, 1, 81, 729, 2187, 1594323 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, balance(Arg0)); }
58+
59+
// END CUT HERE
60+
61+
};
62+
63+
// BEGIN CUT HERE
64+
int main(){
65+
66+
Apothecary ___test;
67+
___test.run_test(-1);
68+
}
69+
// END CUT HERE

Apothecary.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
An accurate scale is one of the most important tools of the apothecary (an old-time pharmacist).
3+
To measure the weight of an object, the apothecary places the object on one
4+
pan of the scale, along with some weights of known size, and adds more weights
5+
of known size to the other pan until the scales balance. For example, if an object
6+
weighs 17 grains, the apothecary could balance the scales by placing a 1-grain weight
7+
and a 9-grain weight in the pan with the object, and a 27-grain weight in the other
8+
pan.
9+
</p>
10+
11+
<p>
12+
The apothecary owns weights in a range of sizes starting at 1 grain. In particular, he owns one weight for each
13+
power of 3: 1 grain, 3 grains, 9 grains, 27 grains, etc. Determine, for an object weighing <b>W</b> grains, how to distribute the weights among the
14+
pans to balance the object. This distribution will be unique. Return a vector &lt;int&gt; of the weights used. The sign of each weight should be
15+
negative if the weight goes in the same pan as the object, and positive if it goes in the other pan. The vector &lt;int&gt; should be arranged in increasing order.
16+
</p>
17+
</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>Apothecary</td></tr><tr><td>Method:</td><td>balance</td></tr><tr><td>Parameters:</td><td>int</td></tr><tr><td>Returns:</td><td>vector &lt;int&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;int&gt; balance(int W)</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>W</b> is between 1 and 1000000, 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>17</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -9, -1, 27 }</pre></td></tr><tr><td><table><tr><td colspan="2">The example above.</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></table></td></tr><tr><td><pre>Returns: { 1 }</pre></td></tr><tr><td><table><tr><td colspan="2">A 1-grain weight is placed in the pan opposite the object being measured.</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>2016</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -243, -9, 81, 2187 }</pre></td></tr><tr><td><table><tr><td colspan="2">A 9-grain weight and a 243-grain weight are placed in the pan with the object,
18+
and an 81-grain weight and a 2187-grain weight are placed in the opposite pan.</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>1000000</pre></td></tr></table></td></tr><tr><td><pre>Returns: { -531441, -59049, -6561, -243, -27, 1, 81, 729, 2187, 1594323 }</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)