Skip to content

Commit b400c0a

Browse files
committed
srm 649 div 2 500
1 parent ac441af commit b400c0a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

CartInSupermarketEasy.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
int dp[105][105];
18+
int d = 0;
19+
20+
int fun(int n, int k){
21+
if(dp[n][k] != -1) return dp[n][k];
22+
if(k == 0) return n;
23+
if(n == 1) return n;
24+
int minv = 1000000;
25+
d++;
26+
string s(d, ' ');
27+
// cout<<s<<n<<" "<<k<<endl;
28+
for(int a = 1; a < n; a++){
29+
int b = n-a;
30+
for(int j = 0; j < k; j++)
31+
minv = min(minv, max(fun(a, k-1-j), fun(b, j)) + 1);
32+
}
33+
minv = min(minv, fun(n-1, k) + 1);
34+
dp[n][k] = minv;
35+
d--;
36+
string t(d, ' ');
37+
// cout<<t<<n<<" "<<k<<" "<<minv<<endl;
38+
return dp[n][k];
39+
}
40+
41+
class CartInSupermarketEasy{
42+
public:
43+
int calc(int N, int K) {
44+
for(int i = 0; i <= 100; i++)
45+
for(int j = 0; j <= 100; j++)
46+
dp[i][j] = -1;
47+
return fun(N, K);
48+
}
49+
50+
// BEGIN CUT HERE
51+
public:
52+
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(); if ((Case == -1) || (Case == 5)) test_case_5(); }
53+
private:
54+
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(); }
55+
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; } }
56+
void test_case_0() { int Arg0 = 5; int Arg1 = 0; int Arg2 = 5; verify_case(0, Arg2, calc(Arg0, Arg1)); }
57+
void test_case_1() { int Arg0 = 5; int Arg1 = 2; int Arg2 = 4; verify_case(1, Arg2, calc(Arg0, Arg1)); }
58+
void test_case_2() { int Arg0 = 15; int Arg1 = 4; int Arg2 = 6; verify_case(2, Arg2, calc(Arg0, Arg1)); }
59+
void test_case_3() { int Arg0 = 7; int Arg1 = 100; int Arg2 = 4; verify_case(3, Arg2, calc(Arg0, Arg1)); }
60+
void test_case_4() { int Arg0 = 45; int Arg1 = 5; int Arg2 = 11; verify_case(4, Arg2, calc(Arg0, Arg1)); }
61+
void test_case_5() { int Arg0 = 100; int Arg1 = 100; int Arg2 = 8; verify_case(5, Arg2, calc(Arg0, Arg1)); }
62+
63+
// END CUT HERE
64+
65+
};
66+
67+
// BEGIN CUT HERE
68+
int main(){
69+
70+
CartInSupermarketEasy ___test;
71+
___test.run_test(-1);
72+
}
73+
// END CUT HERE

CartInSupermarketEasy.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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>You have a sequence that consists of <b>N</b> shopping carts.
2+
You want to remove all of them as quickly as possible.<br></br><br></br>
3+
4+
The process of removing the carts will consist of one or more turns.
5+
Each turn will take exactly one minute.
6+
At the beginning of each turn, you will have some sequences of carts.
7+
For each of those sequences you can choose between two options:
8+
<ul>
9+
<li>split it (in an arbitrary place) into two shorter sequences</li>
10+
<li>remove one shopping cart from the sequence</li>
11+
</ul><br></br><br></br>
12+
13+
There is one additional constraint: during the entire process you can only choose to split a sequence at most <b>K</b> times.<br></br><br></br>
14+
15+
You are given the ints <b>N</b> and <b>K</b>.
16+
Compute and return the smallest number of minutes in which it is possible to remove all the carts.</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>CartInSupermarketEasy</td></tr><tr><td>Method:</td><td>calc</td></tr><tr><td>Parameters:</td><td>int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int calc(int N, int K)</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>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>N</b> will be between 1 and 100, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>K</b> will be between 0 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</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 5</pre></td></tr><tr><td><table><tr><td colspan="2">As <b>K</b>=0, you can never split any sequence. In each turn you have to remove one cart from your sequence. Hence, it will take 5 minutes to remove all 5 carts.</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</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">One optimal solution: {5} -> {2,3} -> {1,2} -> {1,1} -> {}. We used two splits: once when splitting the sequence of 5 carts into 2+3 and the second time when splitting the sequence of 2 carts into 1+1.</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>15</pre></td></tr><tr><td><pre>4</pre></td></tr></table></td></tr><tr><td><pre>Returns: 6</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>7</pre></td></tr><tr><td><pre>100</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">You don't have to split exactly <b>K</b> times.</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>45</pre></td></tr><tr><td><pre>5</pre></td></tr></table></td></tr><tr><td><pre>Returns: 11</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>100</pre></td></tr><tr><td><pre>100</pre></td></tr></table></td></tr><tr><td><pre>Returns: 8</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)