Skip to content

Commit 8fa82f0

Browse files
committed
srm 610
1 parent 3bb1ecf commit 8fa82f0

2 files changed

Lines changed: 344 additions & 0 deletions

File tree

TopCoder/SRM/610/TheMatrix.cpp

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
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+
137+
int hh[105][105], vv[105][105];
138+
139+
class TheMatrix {
140+
public:
141+
int MaxArea(vector <string> board) {
142+
int n = board.size(), m = board[0].length();
143+
REP(i,n) {
144+
REP(j,m) {
145+
hh[i][j] = 1;
146+
FOR(ii,j+1,m-1) {
147+
if (board[i][ii] != board[i][ii - 1]) ++hh[i][j];
148+
else break;
149+
}
150+
vv[i][j] = 1;
151+
FOR(ii,i+1,n-1) {
152+
if (board[ii][j] != board[ii - 1][j]) ++vv[i][j];
153+
else break;
154+
}
155+
}
156+
}
157+
158+
int res = 1;
159+
REP(i,n) {
160+
REP(j,m) {
161+
if ((n - i) * (m - j) <= res) continue;
162+
int t = vv[i][j];
163+
REP(ii,hh[i][j]) {
164+
t = min(t, vv[i][j + ii]);
165+
res = max(res, t * (ii + 1));
166+
}
167+
}
168+
}
169+
return res;
170+
}
171+
};
172+
// BEGIN CUT HERE
173+
int main() {
174+
{
175+
string boardARRAY[] = {"1",
176+
"0"};
177+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
178+
TheMatrix theObject;
179+
eq(0, theObject.MaxArea(board),2);
180+
}
181+
{
182+
string boardARRAY[] = {"0000"};
183+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
184+
TheMatrix theObject;
185+
eq(1, theObject.MaxArea(board),1);
186+
}
187+
{
188+
string boardARRAY[] = {"01"};
189+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
190+
TheMatrix theObject;
191+
eq(2, theObject.MaxArea(board),2);
192+
}
193+
{
194+
string boardARRAY[] = {"001",
195+
"000",
196+
"100"};
197+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
198+
TheMatrix theObject;
199+
eq(3, theObject.MaxArea(board),2);
200+
}
201+
{
202+
string boardARRAY[] = {"0"};
203+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
204+
TheMatrix theObject;
205+
eq(4, theObject.MaxArea(board),1);
206+
}
207+
{
208+
string boardARRAY[] = {"101",
209+
"010"};
210+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
211+
TheMatrix theObject;
212+
eq(5, theObject.MaxArea(board),6);
213+
}
214+
{
215+
string boardARRAY[] = {"101",
216+
"011",
217+
"101",
218+
"010"};
219+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
220+
TheMatrix theObject;
221+
eq(6, theObject.MaxArea(board),8);
222+
}
223+
{
224+
string boardARRAY[] = {"11001110011000110001111001001110110011010110001011",
225+
"10100100010111111011111001011110101111010011100001",
226+
"11101111001110100110010101101100011100101000010001",
227+
"01000010001010101100010011111000100100110111111000",
228+
"10110100000101100000111000100001011101111101010010",
229+
"00111010000011100001110110010011010110010011100100",
230+
"01100001111101001101001101100001111000111001101010",
231+
"11010000000011011010100010000000111011001001100101",
232+
"10100000000100010100100011010100110110110001000001",
233+
"01101010101100001100000110100110100000010100100010",
234+
"11010000001110111111011010011110001101100011100010",
235+
"11101111000000011010011100100001100011111111110111",
236+
"11000001101100100011000110111010011001010100000001",
237+
"00100001111001010000101101100010000001100100001000",
238+
"01001110110111101011010000111111101011000110010111",
239+
"01001010000111111001100000100010101100100101010100",
240+
"11111101001101110011011011011000111001101100011011",
241+
"10000100110111000001110110010000000000111100101101",
242+
"01010011101101101110000011000110011111001111011100",
243+
"01101010011111010000011001111101011010011100001101",
244+
"11011000011000110010101111100000101011011111101100",
245+
"11100001001000110010100011001010101101001010001100",
246+
"11011011001100111101001100111100000101011101101011",
247+
"11110111100100101011100101111101000111001111110111",
248+
"00011001100110111100111100001100101001111100001111",
249+
"10001111100101110111001111100000000011110000100111",
250+
"10101010110110100110010001001010000111100110100011",
251+
"01100110100000001110101001101011001010001101110101",
252+
"10110101110100110110101001100111110000101111100110",
253+
"01011000001001101110100001101001110011001001110001",
254+
"00100101010001100110110101001010010100001011000011",
255+
"00011101100100001010100000000011000010100110011100",
256+
"11001001011000000101111111000000110010001101101110",
257+
"10101010110110010000010011001100110101110100111011",
258+
"01101001010111010001101000100011101001110101000110",
259+
"00110101101110110001110101110010100100110000101101",
260+
"11010101000111010011110011000001101111010011110011",
261+
"10010000010001110011011101001110110010001100011100",
262+
"00111101110001001100101001110100110010100110110000",
263+
"00010011011000101000100001101110111100100000010100",
264+
"01101110001101000001001000001011101010011101011110",
265+
"00000100110011001011101011110011011101100001110111",
266+
"00110011110000011001011100001110101010100110010110",
267+
"00111001010011011111010100000100100000101101110001",
268+
"10101101101110111110000011111011001011100011110001",
269+
"00101110010101111000001010110100001110111011100011",
270+
"01111110010100111010110001111000111101110100111011"};
271+
vector <string> board( boardARRAY, boardARRAY+ARRSIZE(boardARRAY) );
272+
TheMatrix theObject;
273+
eq(7, theObject.MaxArea(board),12);
274+
}
275+
return 0;
276+
}
277+
// END CUT HERE

TopCoder/SRM/610/TheMatrix.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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>Have you ever had a dream, that you were so sure was real? What if you were unable to wake from that dream? How would you know the difference between the dream world and the real world?</p><br></br><br></br>
2+
3+
<p>To answer this complex puzzle, one of the questions that must be answered is to find out whether the world that you live in can be represented by a <i>chess matrix</i>.</p><br></br><br></br>
4+
5+
<p>Cells of a matrix are called adjacent if they share an edge.
6+
A matrix of zeroes and ones is called a chess matrix if there are no two adjacent cells that share the same value.
7+
Hence, in a chess matrix the zeroes and ones have to alternate in the same way the colors alternate on a chessboard:</p><br></br><br></br>
8+
9+
<p><img src="http://i60.tinypic.com/2n8vclz.png"></img></p><br></br><br></br>
10+
11+
<p>You are given a vector &lt;string&gt; <b>board</b> that represents a rectangular grid of cells, with a 0 or a 1 in each cell.
12+
Each character of each element of <b>board</b> will be either '0' or '1'.
13+
In this grid we selected some rectangular subgrid that is a chess matrix.
14+
Return the largest possible area of the selected subgrid.</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>TheMatrix</td></tr><tr><td>Method:</td><td>MaxArea</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int MaxArea(vector &lt;string&gt; board)</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>board</b> will contain between 1 and 100 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of the <b>board</b> is a string containing between 1 and 100 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>All elements of <b>board</b> will have the same length.</td></tr><tr><td align="center" valign="top">-</td><td>Each character of each element of <b>board</b> will be either '0' or '1'.</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;1&quot;,
15+
&quot;0&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">The entire board is a chess matrix.</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;0000&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2">The largest possible chess matrix here is just a single cell.</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;01&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">Again, the entire board is a chess matrix.</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;001&quot;,
16+
&quot;000&quot;,
17+
&quot;100&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">Each rectangular subgrid is determined by a contiguous range of rows and a contiguous range of columns. The four corners of this grid do not form a valid rectangular subgrid.</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;0&quot;}</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">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;101&quot;,
18+
&quot;010&quot;}</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">6)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;101&quot;,
19+
&quot;011&quot;,
20+
&quot;101&quot;,
21+
&quot;010&quot;}</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><tr><td align="center" nowrap="true">7)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;11001110011000110001111001001110110011010110001011&quot;,
22+
&quot;10100100010111111011111001011110101111010011100001&quot;,
23+
&quot;11101111001110100110010101101100011100101000010001&quot;,
24+
&quot;01000010001010101100010011111000100100110111111000&quot;,
25+
&quot;10110100000101100000111000100001011101111101010010&quot;,
26+
&quot;00111010000011100001110110010011010110010011100100&quot;,
27+
&quot;01100001111101001101001101100001111000111001101010&quot;,
28+
&quot;11010000000011011010100010000000111011001001100101&quot;,
29+
&quot;10100000000100010100100011010100110110110001000001&quot;,
30+
&quot;01101010101100001100000110100110100000010100100010&quot;,
31+
&quot;11010000001110111111011010011110001101100011100010&quot;,
32+
&quot;11101111000000011010011100100001100011111111110111&quot;,
33+
&quot;11000001101100100011000110111010011001010100000001&quot;,
34+
&quot;00100001111001010000101101100010000001100100001000&quot;,
35+
&quot;01001110110111101011010000111111101011000110010111&quot;,
36+
&quot;01001010000111111001100000100010101100100101010100&quot;,
37+
&quot;11111101001101110011011011011000111001101100011011&quot;,
38+
&quot;10000100110111000001110110010000000000111100101101&quot;,
39+
&quot;01010011101101101110000011000110011111001111011100&quot;,
40+
&quot;01101010011111010000011001111101011010011100001101&quot;,
41+
&quot;11011000011000110010101111100000101011011111101100&quot;,
42+
&quot;11100001001000110010100011001010101101001010001100&quot;,
43+
&quot;11011011001100111101001100111100000101011101101011&quot;,
44+
&quot;11110111100100101011100101111101000111001111110111&quot;,
45+
&quot;00011001100110111100111100001100101001111100001111&quot;,
46+
&quot;10001111100101110111001111100000000011110000100111&quot;,
47+
&quot;10101010110110100110010001001010000111100110100011&quot;,
48+
&quot;01100110100000001110101001101011001010001101110101&quot;,
49+
&quot;10110101110100110110101001100111110000101111100110&quot;,
50+
&quot;01011000001001101110100001101001110011001001110001&quot;,
51+
&quot;00100101010001100110110101001010010100001011000011&quot;,
52+
&quot;00011101100100001010100000000011000010100110011100&quot;,
53+
&quot;11001001011000000101111111000000110010001101101110&quot;,
54+
&quot;10101010110110010000010011001100110101110100111011&quot;,
55+
&quot;01101001010111010001101000100011101001110101000110&quot;,
56+
&quot;00110101101110110001110101110010100100110000101101&quot;,
57+
&quot;11010101000111010011110011000001101111010011110011&quot;,
58+
&quot;10010000010001110011011101001110110010001100011100&quot;,
59+
&quot;00111101110001001100101001110100110010100110110000&quot;,
60+
&quot;00010011011000101000100001101110111100100000010100&quot;,
61+
&quot;01101110001101000001001000001011101010011101011110&quot;,
62+
&quot;00000100110011001011101011110011011101100001110111&quot;,
63+
&quot;00110011110000011001011100001110101010100110010110&quot;,
64+
&quot;00111001010011011111010100000100100000101101110001&quot;,
65+
&quot;10101101101110111110000011111011001011100011110001&quot;,
66+
&quot;00101110010101111000001010110100001110111011100011&quot;,
67+
&quot;01111110010100111010110001111000111101110100111011&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 12</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)