Skip to content

Commit 7f893f6

Browse files
committed
tccc 04 online round 4
1 parent 0d1eab1 commit 7f893f6

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

BadNeighbors.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
using namespace std;
9+
int p[1005];
10+
int a[1005];
11+
int b[1005];
12+
13+
class BadNeighbors {
14+
public:
15+
int maxDonations(vector <int> d)
16+
{
17+
memset(p, 0, sizeof(p));
18+
memset(a, 0, sizeof(a));
19+
memset(b, 0, sizeof(b));
20+
int n = d.size();
21+
if(n == 2) return max(d[0], d[1]);
22+
n--;
23+
a[n] = d[n]; a[n-1] = 0;
24+
b[n] = 0; b[n-1] = d[n-1];
25+
p[n] = p[n-1] = -1;
26+
int maxa = 0, maxb = 0;
27+
for(int i = n-2; i >= 1; i--){
28+
maxa = 0, maxb = 0;
29+
for(int j = i+2; j <= n; j++){
30+
if(a[j] > maxa) maxa = a[j];
31+
if(b[j] > maxb) maxb = b[j];
32+
}
33+
a[i] = d[i] + maxa;
34+
b[i] = d[i] + maxb;
35+
}
36+
maxa = 0; maxb = 0;
37+
for(int i = 1; i <= n; i++) maxa = max(maxa, a[i]);
38+
for(int i = 2; i <= n; i++) maxb = max(maxb, b[i]);
39+
cout<<a[1]<<endl<<b[1]<<endl;
40+
a[0] = maxa;
41+
b[0] = max(b[1], maxb+d[0]);
42+
return max(a[0], b[0]);
43+
}
44+
45+
// BEGIN CUT HERE
46+
public:
47+
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(); }
48+
private:
49+
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(); }
50+
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; } }
51+
void test_case_0() { int Arr0[] = { 10, 3, 2, 5, 7, 8 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 19; verify_case(0, Arg1, maxDonations(Arg0)); }
52+
void test_case_1() { int Arr0[] = { 11, 15 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 15; verify_case(1, Arg1, maxDonations(Arg0)); }
53+
void test_case_2() { int Arr0[] = { 7, 7, 7, 7, 7, 7, 7 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 21; verify_case(2, Arg1, maxDonations(Arg0)); }
54+
void test_case_3() { int Arr0[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 16; verify_case(3, Arg1, maxDonations(Arg0)); }
55+
void test_case_4() { int Arr0[] = { 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
56+
6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
57+
52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2926; verify_case(4, Arg1, maxDonations(Arg0)); }
58+
59+
// END CUT HERE
60+
61+
};
62+
63+
// BEGIN CUT HERE
64+
int main()
65+
{
66+
BadNeighbors ___test;
67+
___test.run_test(-1);
68+
}
69+
// END CUT HERE

BadNeighbors.txt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
PROBLEM STATEMENT
2+
3+
The old song declares "Go ahead and hate your neighbor", and the residents of
4+
Onetinville have taken those words to heart. Every resident hates his next-door
5+
neighbors on both sides. Nobody is willing to live farther away from the town's
6+
well than his neighbors, so the town has been arranged in a big circle around
7+
the well. Unfortunately, the town's well is in disrepair and needs to be restored. You
8+
have been hired to collect donations for the Save Our Well fund.
9+
10+
11+
12+
Each of the town's residents is willing to donate a certain amount, as specified in the vector <int>
13+
donations, which is listed in clockwise order around the well. However, nobody is willing to
14+
contribute to a fund to which his neighbor has also contributed. Next-door neighbors are always listed
15+
consecutively in donations, except that the first and last entries in donations are also
16+
for next-door neighbors. You must calculate and return the maximum amount of donations that can be collected.
17+
18+
19+
20+
DEFINITION
21+
Class:BadNeighbors
22+
Method:maxDonations
23+
Parameters:vector <int>
24+
Returns:int
25+
Method signature:int maxDonations(vector <int> donations)
26+
27+
28+
CONSTRAINTS
29+
-donations contains between 2 and 40 elements, inclusive.
30+
-Each element in donations is between 1 and 1000, inclusive.
31+
32+
33+
EXAMPLES
34+
35+
0)
36+
{ 10, 3, 2, 5, 7, 8 }
37+
38+
Returns: 19
39+
40+
The maximum donation is 19, achieved by 10+2+7. It would be better to take 10+5+8 except that
41+
the 10 and 8 donations are from neighbors.
42+
43+
1)
44+
{ 11, 15 }
45+
46+
Returns: 15
47+
48+
2)
49+
{ 7, 7, 7, 7, 7, 7, 7 }
50+
51+
Returns: 21
52+
53+
3)
54+
{ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
55+
56+
Returns: 16
57+
58+
4)
59+
{ 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
60+
6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
61+
52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 }
62+
63+
Returns: 2926

0 commit comments

Comments
 (0)