Skip to content

Commit cb77f10

Browse files
committed
TCO14 R2A
1 parent 348de00 commit cb77f10

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
85+
// BEGIN CUT HERE
86+
vector<string> split( const string& s, const string& delim =" " ) {
87+
vector<string> res;
88+
string t;
89+
for ( int i = 0 ; i != s.size() ; i++ ) {
90+
if ( delim.find( s[i] ) != string::npos ) {
91+
if ( !t.empty() ) {
92+
res.push_back( t );
93+
t = "";
94+
}
95+
} else {
96+
t += s[i];
97+
}
98+
}
99+
if ( !t.empty() ) {
100+
res.push_back(t);
101+
}
102+
return res;
103+
}
104+
105+
vector<int> splitInt( const string& s, const string& delim =" " ) {
106+
vector<string> tok = split( s, delim );
107+
vector<int> res;
108+
for ( int i = 0 ; i != tok.size(); i++ )
109+
res.push_back( atoi( tok[i].c_str() ) );
110+
return res;
111+
}
112+
// END CUT HERE
113+
114+
// BEGIN CUT HERE
115+
int s2i(string s) {
116+
stringstream ss;
117+
ss << s;
118+
int res;
119+
ss >> res;
120+
return res;
121+
}
122+
123+
string i2s(int n) {
124+
stringstream ss;
125+
ss << n;
126+
string res;
127+
ss >> res;
128+
return res;
129+
}
130+
// END CUT HERE
131+
132+
class SixteenBricks {
133+
public:
134+
int maximumSurface(vector <int> height) {
135+
sort(height.begin(), height.end());
136+
int res = 16;
137+
REP(i,16) res += height[i] * 4;
138+
FOR(i,0,1) res -= height[i] * 4 * 2;
139+
FOR(i,2,5) res -= height[i] * 3 * 2;
140+
FOR(i,6,7) res -= height[i] * 2 * 2;
141+
return res;
142+
}
143+
};
144+
// BEGIN CUT HERE
145+
int main() {
146+
{
147+
int heightARRAY[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
148+
vector <int> height( heightARRAY, heightARRAY+ARRSIZE(heightARRAY) );
149+
SixteenBricks theObject;
150+
eq(0, theObject.maximumSurface(height),32);
151+
}
152+
{
153+
int heightARRAY[] = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
154+
vector <int> height( heightARRAY, heightARRAY+ARRSIZE(heightARRAY) );
155+
SixteenBricks theObject;
156+
eq(1, theObject.maximumSurface(height),64);
157+
}
158+
{
159+
int heightARRAY[] = {77, 78, 58, 34, 30, 20, 8, 71, 37, 74, 21, 45, 39, 16, 4, 59}
160+
;
161+
vector <int> height( heightARRAY, heightARRAY+ARRSIZE(heightARRAY) );
162+
SixteenBricks theObject;
163+
eq(2, theObject.maximumSurface(height),1798);
164+
}
165+
return 0;
166+
}
167+
// END CUT HERE
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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><p>You have 16 bricks. Each brick has the shape of a rectangular box. You are given a vector &lt;int&gt; <b>height</b>. For each i, one of your bricks has dimensions 1 x 1 x <b>height</b>[i].</p>
2+
<br></br>
3+
<p>You also have an opaque table. You are going to place your 16 bricks onto the table in a specific way. You are not allowed to rotate the bricks while placing them: the dimension given in <b>height</b> must always be vertical. On the table, there is a 4 x 4 grid of squares. You have to place exactly one of your bricks onto each of the squares.</p>
4+
<br></br>
5+
<p>After you place all the bricks, we will look at the solid formed by them. We are interested in the visible surface area of the solid. Note that the bottom sides of your bricks are not a part of the visible surface, as they stand on the table. Also, adjacent bricks always touch each other and the parts where they touch are not visible.</p>
6+
<br></br>
7+
<p>Different arrangements of bricks may lead to different visible surfaces. Return the largest possible visible surface area.</p>
8+
</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>SixteenBricks</td></tr><tr><td>Method:</td><td>maximumSurface</td></tr><tr><td>Parameters:</td><td>vector &lt;int&gt;</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int maximumSurface(vector &lt;int&gt; height)</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>height</b> will contain exactly 16 elements.</td></tr><tr><td align="center" valign="top">-</td><td>Each element of <b>height</b> will be between 1 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>{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 32</pre></td></tr><tr><td><table><tr><td colspan="2">All your bricks look the same.
9+
The only solid you can construct is a 1 x 4 x 4 box.
10+
The bottom side of the box is not visible, the other five sides are.
11+
The total visible surface area is 4*4 + 4*(1*4) = 32.</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>{1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 64</pre></td></tr><tr><td><table><tr><td colspan="2">In order to maximize the visible surface area, you should produce a configuration in which no two bricks with height 2 share a common face.</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>{77, 78, 58, 34, 30, 20, 8, 71, 37, 74, 21, 45, 39, 16, 4, 59}
12+
</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1798</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)