Skip to content

Commit 4af939a

Browse files
committed
tco14 r1a
1 parent 0c9e73e commit 4af939a

File tree

6 files changed

+643
-0
lines changed

6 files changed

+643
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
#include <vector>
9+
#include <queue>
10+
#include <set>
11+
#include <map>
12+
#include <cstdio>
13+
#include <cstdlib>
14+
#include <cctype>
15+
#include <cmath>
16+
#include <string>
17+
#include <cstring>
18+
using namespace std;
19+
20+
// BEGIN CUT HERE
21+
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
22+
23+
template<typename T> void print( T a ) {
24+
cerr << a;
25+
}
26+
static void print( long long a ) {
27+
cerr << a << "L";
28+
}
29+
static void print( string a ) {
30+
cerr << '"' << a << '"';
31+
}
32+
template<typename T> void print( vector<T> a ) {
33+
cerr << "{";
34+
for ( int i = 0 ; i != a.size() ; i++ ) {
35+
if ( i != 0 ) cerr << ", ";
36+
print( a[i] );
37+
}
38+
cerr << "}" << endl;
39+
}
40+
template<typename T> void eq( int n, T have, T need ) {
41+
if ( have == need ) {
42+
cerr << "Case " << n << " passed." << endl;
43+
} else {
44+
cerr << "Case " << n << " failed: expected ";
45+
print( need );
46+
cerr << " received ";
47+
print( have );
48+
cerr << "." << endl;
49+
}
50+
}
51+
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
52+
if( have.size() != need.size() ) {
53+
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
54+
print( have );
55+
print( need );
56+
return;
57+
}
58+
for( int i= 0; i < have.size(); i++ ) {
59+
if( have[i] != need[i] ) {
60+
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
61+
print( have );
62+
print( need );
63+
return;
64+
}
65+
}
66+
cerr << "Case " << n << " passed." << endl;
67+
}
68+
static void eq( int n, string have, string need ) {
69+
if ( have == need ) {
70+
cerr << "Case " << n << " passed." << endl;
71+
} else {
72+
cerr << "Case " << n << " failed: expected ";
73+
print( need );
74+
cerr << " received ";
75+
print( have );
76+
cerr << "." << endl;
77+
}
78+
}
79+
// END CUT HERE
80+
81+
#define REP(i,n) for(int i=0;i<(n);++i)
82+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
83+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
84+
#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it)
85+
#define CLR(x) memset((x),0,sizeof((x)))
86+
typedef long long LL;
87+
typedef vector<int> VI;
88+
typedef vector<string> VS;
89+
90+
// BEGIN CUT HERE
91+
vector<string> split( const string& s, const string& delim =" " ) {
92+
vector<string> res;
93+
string t;
94+
for ( int i = 0 ; i != s.size() ; i++ ) {
95+
if ( delim.find( s[i] ) != string::npos ) {
96+
if ( !t.empty() ) {
97+
res.push_back( t );
98+
t = "";
99+
}
100+
} else {
101+
t += s[i];
102+
}
103+
}
104+
if ( !t.empty() ) {
105+
res.push_back(t);
106+
}
107+
return res;
108+
}
109+
110+
vector<int> splitInt( const string& s, const string& delim =" " ) {
111+
vector<string> tok = split( s, delim );
112+
vector<int> res;
113+
for ( int i = 0 ; i != tok.size(); i++ )
114+
res.push_back( atoi( tok[i].c_str() ) );
115+
return res;
116+
}
117+
// END CUT HERE
118+
119+
// BEGIN CUT HERE
120+
int s2i(string s) {
121+
stringstream ss;
122+
ss << s;
123+
int res;
124+
ss >> res;
125+
return res;
126+
}
127+
128+
string i2s(int n) {
129+
stringstream ss;
130+
ss << n;
131+
string res;
132+
ss >> res;
133+
return res;
134+
}
135+
// END CUT HERE
136+
int mm[55][2][2][2];
137+
int state[55];
138+
int n;
139+
140+
class EllysLamps {
141+
public:
142+
int dp(int idx, int lit, int addU, int addN) {
143+
int& ret = mm[idx][lit][addU][addN];
144+
if (ret != -1) return ret;
145+
146+
if (idx == n - 1) return min(lit + addN, !lit + addU);
147+
148+
ret = 0;
149+
REP(lt,2) {
150+
REP(rt,2) {
151+
int t1 = dp(idx + 1, state[idx + 1] ^ rt, lit ^ lt ^ 1, lit ^ 1) + addU;
152+
int t2 = dp(idx + 1, state[idx + 1], lit ^ lt, lit) + addN;
153+
ret = max(ret, min(t1, t2));
154+
}
155+
}
156+
157+
return ret;
158+
}
159+
int getMin(string lamps) {
160+
n = lamps.length();
161+
REP(i,n) state[i] = lamps[i] == 'Y';
162+
memset(mm, -1, sizeof(mm));
163+
int res = dp(0, state[0], 0, 0);
164+
return res;
165+
}
166+
};
167+
// BEGIN CUT HERE
168+
int main() {
169+
{
170+
EllysLamps theObject;
171+
eq(0, theObject.getMin("YNNYN"),2);
172+
}
173+
{
174+
EllysLamps theObject;
175+
eq(1, theObject.getMin("NNN"),0);
176+
}
177+
{
178+
EllysLamps theObject;
179+
eq(2, theObject.getMin("YY"),0);
180+
}
181+
{
182+
EllysLamps theObject;
183+
eq(3, theObject.getMin("YNYYYNNNY"),3);
184+
}
185+
{
186+
EllysLamps theObject;
187+
eq(4, theObject.getMin("YNYYYYNYNNYYNNNNNNYNYNYNYNNYNYYYNY"),13);
188+
}
189+
return 0;
190+
}
191+
// END CUT HERE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html><body bgcolor="#ffffff" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>Elly has a row of N lamps, conveniently numbered from 0 to N-1. Some of them are initially lit and the rest are not. She can control the lamps using a controller. On the controller there is a row of N buttons. Clicking the button with index i changes the state of lamp i: it goes off, if it was on, and it goes on if it was off.<br></br><br></br>
2+
3+
Unfortunately the controller has some defects. Each of the buttons correctly changes the state of the corresponding lamp. However, it is possible that some of the buttons also change the state of one or both adjacent lamps as well. This means that pressing the button with index i has the following effects:
4+
<ul>
5+
<li>The state of lamp i changes.</li>
6+
<li>If there is a lamp with index i-1, its state might change.</li>
7+
<li>If there is a lamp with index i+1, its state might change.</li>
8+
</ul>
9+
The girl does not initially know which lamps change their state when each of the buttons is pressed. She knows, however, that every time she clicks a particular button, the same set of lamps will change their states.<br></br><br></br>
10+
11+
Elly wants to reach a configuration in which the number of lamps that are turned on is minimized. Help her determine how many of them will remain lit in the worst possible case. (That is, for the worst possible way how the buttons change the state of the lamps.) She can use each of the buttons as many times as she likes, in any order she likes.<br></br><br></br>
12+
13+
You will be given a string <b>lamps</b>, which gives information about the initial state of the lamps. The i-th character of lamps will be 'Y' if the i-th lamp is lit, and 'N', if it is not. Return the minimal number of lit lamps the girl can get in the worst possible case.</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>EllysLamps</td></tr><tr><td>Method:</td><td>getMin</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getMin(string lamps)</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>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>The defects in the switches might not be symmetric. This means that if a lamp with index i happens to change the state of lamp i+1, this does not necessarily mean that the using the switch of lamp i+1 changes the state of lamp i.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>lamps</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>lamps</b> will be either 'Y' or 'N'.</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;YNNYN&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">In this case Elly will only make things worse (or the same) by pressing buttons. For example, suppose that:
14+
<ul>
15+
<li>buttons 0 and 1 each change the state of both lamps 0 and 1</li>
16+
<li>buttons 2 and 3 each change the state of both lamps 2 and 3</li>
17+
<li>button 4 only changes the state of lamp 4</li>
18+
</ul>
19+
In this situation, Elly cannot reach any configuration with fewer than two lit lamps.</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;NNN&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">Obviously, with no initially lit lamps, Elly can just sit and relax.</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;YY&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">If one of the lamps influences the other one, then Elly can use it and turn both off with one button press. Otherwise, each button changes the state of its lamp only, thus Elly can turn them both off by pressing both buttons.</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;YNYYYNNNY&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</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>&quot;YNYYYYNYNNYYNNNNNNYNYNYNYNNYNYYYNY&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 13</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)