Skip to content

Commit 1e4a12c

Browse files
committed
srm 558 div 1 250
1 parent 2d561a7 commit 1e4a12c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

Stamp.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
#include <numeric>
12+
using namespace std;
13+
typedef pair<int,int> pi;
14+
typedef set<int> si;
15+
typedef vector<int> vi;
16+
typedef vector<vi> vvi;
17+
typedef vector<string> vs;
18+
typedef long long ll;
19+
#define sz size()
20+
#define mp make_pair
21+
#define pb push_back
22+
#define inf 1000000000
23+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
24+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
25+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
26+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
27+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
28+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
29+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
30+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
31+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
32+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
33+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
34+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
35+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
36+
int A[55][55];
37+
string s;
38+
bool valid(int i, int j){
39+
set<char> S;
40+
for(int k = i; k <= j; k++)
41+
if(s[k] != '*') S.insert(s[k]);
42+
return S.size() <= 1;
43+
}
44+
45+
class Stamp {
46+
public:
47+
int getMinimumCost(string desiredColor, int stampCost, int pushCost) {
48+
s = desiredColor;
49+
int mincost = inf, n = desiredColor.size();
50+
for(int L = 1; L <= n; L++){
51+
fi(55) fj(55) A[i][j] = 0;
52+
fie(n-L){
53+
int j = i+L-1;
54+
if(valid(i, j)) A[i][j] = 1;
55+
}
56+
for(int l = L+1; l <= n; l++)
57+
for(int i = 0; i <= n-l; i++){
58+
int j = i+l-1;
59+
if(A[i][j]) continue;
60+
if(valid(i, j)){
61+
A[i][j] = (l+L-1)/L;
62+
continue;
63+
}
64+
int minv = inf;
65+
for(int k = i; k < j; k++){
66+
int a = A[i][k], b = A[k+1][j];
67+
if(a == 0 || b == 0) continue;
68+
minv = min(minv, a+b);
69+
}
70+
if(minv != inf) A[i][j] = minv;
71+
}
72+
int val = A[0][n-1];
73+
if(val == 0) continue;
74+
mincost = min(mincost, stampCost*L + pushCost*val);
75+
}
76+
return mincost;
77+
}
78+
79+
// BEGIN CUT HERE
80+
public:
81+
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(); }
82+
private:
83+
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(); }
84+
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; } }
85+
void test_case_0() { string Arg0 = "RRGGBB"; int Arg1 = 1; int Arg2 = 1; int Arg3 = 5; verify_case(0, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
86+
void test_case_1() { string Arg0 = "R**GB*"; int Arg1 = 1; int Arg2 = 1; int Arg3 = 5; verify_case(1, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
87+
void test_case_2() { string Arg0 = "BRRB"; int Arg1 = 2; int Arg2 = 7; int Arg3 = 30; verify_case(2, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
88+
void test_case_3() { string Arg0 = "R*RR*GG"; int Arg1 = 10; int Arg2 = 58; int Arg3 = 204; verify_case(3, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
89+
void test_case_4() { string Arg0 = "*B**B**B*BB*G*BBB**B**B*"; int Arg1 = 5; int Arg2 = 2; int Arg3 = 33; verify_case(4, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
90+
void test_case_5() { string Arg0 = "*R*RG*G*GR*RGG*G*GGR***RR*GG"; int Arg1 = 7; int Arg2 = 1; int Arg3 = 30; verify_case(5, Arg3, getMinimumCost(Arg0, Arg1, Arg2)); }
91+
92+
// END CUT HERE
93+
94+
};
95+
96+
// BEGIN CUT HERE
97+
int main()
98+
{
99+
Stamp ___test;
100+
___test.run_test(-1);
101+
}
102+
// END CUT HERE
103+

Stamp.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>Little Fox Jiro has a rectangular board.
2+
On the board there is a row of N unit cells.
3+
The cells are numbered 0 through N-1 from the left to the right.
4+
Initially, the cells are not colored.
5+
Jiro must color each of the cells red, green, or blue.
6+
<p></p>
7+
You are given a string <b>desiredColor</b> with N characters.
8+
For each i, character i of <b>desiredColor</b> represents the color Jiro must use for cell i.
9+
If a character is one of 'R' (as red), 'G' (as green), and 'B' (as blue), it means that Jiro must use that particular color.
10+
If a character is '*', Jiro may use any of the three colors for the particular cell.
11+
<p></p>
12+
You are also given the ints <b>stampCost</b> and <b>pushCost</b> that describe the cost of the coloring process.
13+
The coloring process consists of two phases.
14+
In the first phase, Jiro must pick a single stamp he will then use to color all the cells.
15+
The length L of the stamp can be any integer between 1 and N, inclusive.
16+
A stamp of length L costs L*<b>stampCost</b>.
17+
<p></p>
18+
In the second phase, Jiro must repeatedly use the stamp to color the cells.
19+
Each use of the stamp works as follows:
20+
<ol>
21+
<li>Jiro picks one of the three colors and pushes the stamp into ink of the chosen color C.</li>
22+
<li>Jiro picks a segment of L contiguous cells such that each of them is either uncolored or already has the color C.
23+
The segment must be completely inside the board.
24+
That is, the leftmost cell of the segment must be one of the cells 0 through N-L.</li>
25+
<li>Jiro pushes the stamp onto the chosen segment of cells. All the cells now have color C.</li>
26+
</ol>
27+
Each use of the stamp costs <b>pushCost</b>.
28+
<p></p>
29+
Return the smallest possible total cost of coloring all the cells using the above process.
30+
</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>Stamp</td></tr><tr><td>Method:</td><td>getMinimumCost</td></tr><tr><td>Parameters:</td><td>string, int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getMinimumCost(string desiredColor, int stampCost, int pushCost)</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>desiredColor</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character of <b>desiredColor</b> will be either 'R' or 'G' or 'B' or '*'.</td></tr><tr><td align="center" valign="top">-</td><td><b>stampCost</b> will be between 1 and 100,000, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>pushCost</b> will be between 1 and 100,000, 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>&quot;RRGGBB&quot;</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 5</pre></td></tr><tr><td><table><tr><td colspan="2">The optimal solution is to choose L=2 and then stamp three times: using red color for cells [0,1], green for [2,3], and blue for [4,5].
31+
The stamp costs 2*1 = 2, each of the three uses costs 1, so the total costs is 2*1 + 3*1 = 5.</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>&quot;R**GB*&quot;</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 5</pre></td></tr><tr><td><table><tr><td colspan="2">The optimal solution is the same as in the previous example.
32+
Note that you must color all the cells, so choosing L=1 and then using the stamp three times is not a valid solution.</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>&quot;BRRB&quot;</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>7</pre></td></tr></table></td></tr><tr><td><pre>Returns: 30</pre></td></tr><tr><td><table><tr><td colspan="2">Also, note that once a cell is colored, you are not allowed to stamp over it using a different color.
33+
Therefore, you can only choose L=1 in this case.</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>&quot;R*RR*GG&quot;</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>58</pre></td></tr></table></td></tr><tr><td><pre>Returns: 204</pre></td></tr><tr><td><table><tr><td colspan="2">It is allowed to stamp the same cell multiple times if all of those stamps use the same color.</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>&quot;*B**B**B*BB*G*BBB**B**B*&quot;</pre></td></tr><tr><td><pre>5</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 33</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>&quot;*R*RG*G*GR*RGG*G*GGR***RR*GG&quot;</pre></td></tr><tr><td><pre>7</pre></td></tr><tr><td><pre>1</pre></td></tr></table></td></tr><tr><td><pre>Returns: 30</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)