forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStamp.cpp
More file actions
169 lines (142 loc) · 2.86 KB
/
Stamp.cpp
File metadata and controls
169 lines (142 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <queue>
using namespace std;
/* http://community.topcoder.com/stat?c=problem_statement&pm=11835 */
#define SIZEOFARRAY(s) sizeof(s)/sizeof(s[0])
#define REP(i,n) for(int i=0;i<(n);++i)
#define LOOP(i,v,n) for(int i=v;i<(n);++i)
#define LL long long
#define memSet(m, v) memset(m, v, sizeof(m))
LL INF = 51 * 1000000000000L;
class Stamp
{
public:
int getMinimumCost(string desiredColor, int stampCost, int pushCost)
{
int n = desiredColor.length();
int res = INT_MAX;
LOOP(L, 1, n + 1)
{
vector<int> cost(n + 1, INT_MAX);
cost[0] = 0;
REP(i, n)
{
int color = 7;
LOOP(j, i, n)
{
char ch = desiredColor[j];
switch (ch)
{
case 'R':
color &= 1;
break;
case 'G':
color &= 2;
break;
case 'B':
color &= (1<<2);
case '*':
color &= 7;
}
if (!color) break;
int seg = j - i + 1;
if (seg < L) continue;
if (cost[i] != INT_MAX)
cost[j + 1] = std::min(cost[j + 1], cost[i] + ((seg + L - 1) / L) * pushCost);
}
}
if (cost[n] != INT_MAX)
res = std::min(res, cost[n] + L * stampCost);
}
return res;
}
};
void TEST(string desiredColor, int stampCost, int pushCost, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
Stamp stamp;
int result = stamp.getMinimumCost(desiredColor, stampCost, pushCost);
assert( result == expected );
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
}
template <class T>
vector<T> convert(T *list, int n)
{
vector<T> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
void Test1()
{
TEST("R*RR*GG", 10, 58, 204);
TEST("RRGGBB", 1, 1, 5);
TEST("R**GB*", 1, 1, 5);
TEST("BRRB", 2, 7, 30);
TEST("*B**B**B*BB*G*BBB**B**B*", 5, 2, 33);
TEST("*R*RG*G*GR*RGG*G*GGR***RR*GG", 7, 1, 30);
TEST("**************************************************", 100000, 100000, 1500000);
TEST("RGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRG", 100000, 100000, 5100000);
TEST( "RR****BBBB****R***R*R*G**GR**BGG**B*GB*GRRBG**G", 76223, 87458, 4186749);
TEST( "RR****BBBB****R***R*R*G**GR**BGG**B*GB*GRRBG**G", 76223, 87458, 4186749);
TEST("BB*R*G*BBR*GRR*", 39197, 70687 ,1099502 );
}
void Test2()
{
}
void Test3()
{
}
void Test4()
{
}
void Test5()
{
}
void Test6()
{
}
void Test7()
{
}
void Test8()
{
}
void Test9()
{
}
void Test10()
{
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
Test10();
cout<<"success"<<endl;
getchar();
return 0;
}