Skip to content

Commit 7a11e96

Browse files
committed
Bits Jaipur 2015 500
1 parent 6484d88 commit 7a11e96

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Diff for: TaroJumps.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 TaroJumps{
19+
public:
20+
int getNumber(vector <int> r, int A, int B) {
21+
int maxv = 0;
22+
set<int> rocks;
23+
for(int i : r) rocks.insert(i);
24+
for(int rock : rocks){
25+
for(int k = 0; k <= 30; k++){
26+
int t = 0;
27+
int start = rock - (1<<k) + 1;
28+
if(start < A || start > B) continue;
29+
for(int pos = start + 1, i = 1; pos <= 1e9; pos += (1<<i), i++){
30+
if(rocks.count(pos)) t++;
31+
}
32+
maxv = max(maxv, t);
33+
}
34+
}
35+
return maxv;
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(); if ((Case == -1) || (Case == 4)) test_case_4(); }
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 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; } }
44+
void test_case_0() { int Arr0[] = {2, 6, 16}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 1; int Arg3 = 2; verify_case(0, Arg3, getNumber(Arg0, Arg1, Arg2)); }
45+
void test_case_1() { int Arr0[] = {2, 4, 9}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 0; int Arg3 = 0; verify_case(1, Arg3, getNumber(Arg0, Arg1, Arg2)); }
46+
void test_case_2() { int Arr0[] = {4, 10, 3, 6}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 5; int Arg3 = 3; verify_case(2, Arg3, getNumber(Arg0, Arg1, Arg2)); }
47+
void test_case_3() { int Arr0[] = {1, 1000000000}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 10; int Arg3 = 1; verify_case(3, Arg3, getNumber(Arg0, Arg1, Arg2)); }
48+
void test_case_4() { int Arr0[] = {1, 47, 74, 1000000000}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 1000000000; int Arg3 = 1; verify_case(4, Arg3, getNumber(Arg0, Arg1, Arg2)); }
49+
50+
// END CUT HERE
51+
52+
};
53+
54+
// BEGIN CUT HERE
55+
int main(){
56+
57+
TaroJumps ___test;
58+
___test.run_test(-1);
59+
}
60+
// END CUT HERE

Diff for: TaroJumps.html

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
Cat Taro jumps quite well. He decided to jump onto some rocks.
3+
</p>
4+
<p>
5+
</p>
6+
<p>
7+
There are N rocks on a straight line. For each i, rock i (0-based index) is at the coordinate <b>rocks</b>[i].
8+
Taro starts his travel from a point with an integer coordinate.
9+
Each time Taro jumps, his coordinate increases.
10+
For each i, the length of his i-th jump (1-based count) is 2^(i-1).
11+
That is, his first jump has length 1, his second jump has length 2, his third jump has length 4, and so on.
12+
If he starts at the coordinate X, his jumps will take him to X+1, then X+3, then X+7, and so on.
13+
</p>
14+
<p>
15+
</p>
16+
<p>
17+
Taro wants to maximize the number of times he jumps onto a rock.
18+
He can start his jumping from any integer point between <b>A</b> and <b>B</b>, inclusive.
19+
You are given the vector &lt;int&gt; <b>rocks</b>, the int <b>A</b> and the int <b>B</b>.
20+
Return the maximum possible number of times Taro jumps onto a rock.
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>TaroJumps</td></tr><tr><td>Method:</td><td>getNumber</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;, int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getNumber(vector &lt;int&gt; rocks, int A, int B)</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></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>rocks</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>All elements of <b>rocks</b> will be distinct.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>rocks</b> will be between 0 and 1,000,000,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>B</b> will be between 0 and 1,000,000,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>A</b> will be between 0 and <b>B</b>, 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>{2, 6, 16}</pre></td></tr><tr><td><pre>0</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">In this test case the valid starting points are 0 and 1.
22+
<p>
23+
</p>
24+
If Taro starts at coordinate 0, he will jump to 1, 3, 7, 15, and so on.
25+
In this case he will not jump onto any rock.
26+
<p>
27+
</p>
28+
If he starts at coordinate 1, he will jump to 2, 4, 8, 16, and so on.
29+
In this case, he will jump onto a rock twice: the first time at the coordinate 2, and then at the coordinate 16.
30+
<p>
31+
</p>
32+
Thus, the answer is 2.</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>{2, 4, 9}</pre></td></tr><tr><td><pre>0</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2"></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>{4, 10, 3, 6}</pre></td></tr><tr><td><pre>2</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">Here the best starting coordinate is 3. Note that Taro does not jump onto his starting coordinate. If there is a rock at the coordinate where he starts, we do not count it.
33+
<p>
34+
</p>
35+
Also note that he can jump onto the rocks in any order.</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>{1, 1000000000}</pre></td></tr><tr><td><pre>0</pre></td></tr><tr><td><pre>10</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{1, 47, 74, 1000000000}</pre></td></tr><tr><td><pre>0</pre></td></tr><tr><td><pre>1000000000</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</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)