forked from arshdeep/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadNeighbors.cpp
More file actions
64 lines (51 loc) · 1.51 KB
/
BadNeighbors.cpp
File metadata and controls
64 lines (51 loc) · 1.51 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
#include <climits>
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <unordered_set>
#include <map>
#include <time.h>
using namespace std;
/* BadNeighbors https://www.topcoder.com/stat?c=problem_statement&pm=2402&rd=5009 */
int maxDonationsEx(int* donations, int n)
{
vector<int> dp(n);
int max = INT_MIN;
dp[0] = donations[0];
dp[1] = donations[1];
dp[2] = donations[0] + donations[2];
for (int i = 3; i < n; ++i)
{
dp[i] = std::max(dp[i - 3], dp[i - 2]) + donations[i];
max = std::max(dp[i], max);
}
return max;
}
int maxDonations(int* donations, int n)
{
if (n == 2)
return std::max(donations[0], donations[1]);
int max = INT_MIN;
max = maxDonationsEx(donations, n - 1);
max = std::max(max, maxDonationsEx(donations + 1, n - 1));
return max;
}
int main(int argc, char *argv[])
{
int donations[] = { 10, 3, 2, 5, 7, 8 };
cout<<maxDonations(donations, sizeof(donations)/sizeof(int))<<endl;
int donations2[] = { 7, 7, 7, 7, 7, 7, 7 };
cout<<maxDonations(donations2, sizeof(donations2)/sizeof(int))<<endl;
int donations3[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
cout<<maxDonations(donations3, sizeof(donations3)/sizeof(int))<<endl;
int donations4[] = { 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 };
cout<<maxDonations(donations4, sizeof(donations4)/sizeof(int))<<endl;
getchar();
return 0;
}