P1133 master's garden, P1133 master's garden
Description
The instructor has a circular garden. He wants to plant n trees evenly around the garden, but the soil in the garden is very special. The trees suitable for planting are different in each location, some trees may lose ornamental value because they are not suitable for the soil at this position.
The principal prefers three trees. The height of the three trees is 10, 20, and 30 respectively. The instructor hopes that the tree in this circle will have a sense of hierarchy, so the height of the tree at any position is higher or lower than that of the adjacent trees. Under such conditions, the instructor wants you to design a set of solutions to make the sum of viewing values the highest.
Input/Output Format
Input Format:
The 1st behavior of the input file "garden. in" is a positive integer n, indicating the tree to be planted.
In the next n rows, three positive integers (ai, bi, and ci) of no more than 10000 are displayed in clockwise order, indicating the ornamental value of a tree at the position I of 10, 20, and 30.
The tree at the position I is adjacent to the tree at the position I + 1. In particular, the tree at the position 1st is adjacent to the tree at the position n.
Output Format:
The output file "garden. out" contains only one positive integer, which is the maximum ornamental value and.
Input and Output sample input sample #1:
4 1 3 2 3 1 2 3 1 2 3 1 2
Output sample #1:
11
Description
[Example]
1st ~ Trees of 20, 10, 30, and 10 are planted at n locations, with the highest value.
[Data scale and Conventions]
For 20% of the data, n ≤ 10;
For 40% of data, n ≤ 100;
For 60% of data, n ≤ 1000;
For 100% of data, there are 4 ≤ n ≤ 100000, and ensure that n must be an even number. '
Dp [I] [j] [k] is used to represent the I point, j is used to plant trees, and k is used to represent the ascending or descending sequence.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=200001; 7 void read(int &n) 8 { 9 char c='+';int x=0;bool flag=0;10 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}11 while(c>='0'&&c<='9')12 {x=x*10+c-48;c=getchar();}13 flag==1?n=-x:n=x;14 }15 int dp[MAXN][5][3];16 int n;17 int v[MAXN][5];18 int main()19 {20 read(n);21 for(int i=1;i<=n;i++)22 {23 read(v[i][1]);24 read(v[i][2]);25 read(v[i][3]);26 }27 for(int i=2;i<=n;i++)28 {29 dp[i][1][1]=max(dp[i-1][2][0],dp[i-1][3][0])+v[i][1];30 dp[i][2][1]=dp[i-1][3][0]+v[i][2];31 dp[i][2][0]=dp[i-1][1][1]+v[i][2];32 dp[i][3][0]=max(dp[i-1][2][1],dp[i-1][1][1])+v[i][3];33 }34 int ans=0;35 ans=max(ans,dp[n][1][1]+v[1][2]);36 ans=max(ans,dp[n][1][1]+v[1][3]);37 ans=max(ans,dp[n][2][0]+v[1][1]);38 ans=max(ans,dp[n][2][1]+v[1][3]);39 ans=max(ans,dp[n][3][0]+v[1][1]);40 ans=max(ans,dp[n][3][0]+v[1][2]);41 printf("%d",ans);42 return 0;43 }