Leader of the garden

Title Description

The garden has a ring leader, he wanted to be uniformly n planted trees around the garden, but the leader of the garden soil is very special, trees planted for each location is different, some trees may not be suitable for this position because of soil The loss of ornamental value.

Guru favorite 3 trees, three trees height were 10, 20 . The ring leader hopes species must have a sense of depth, so a tree of any height position of its adjacent two trees are higher or lower than both, and in this condition, the leader you want to design a program, so that the sum of the highest ornamental value.

 

Input and output formats

Input formats:

The first line a positive integer the n- , expressed the need for tree species of trees.

Next n lines, each line . 3 months not more than 10000 . 1 0 0 0 0 positive integer a_i, B_i, C_i , showing the clockwise order of I I species height position 10, 20 of the tree can be obtained ornamental value.

The first i tree and the second positions i + 1 tree locations adjacent, in particular, the first tree of a position of the first n trees adjacent positions.

Output formats:

A positive integer, the largest and ornamental value.

 

Sample input and output

Input Sample # 1: 
4 
1 3 2 
3 1 2 
3 1 2 
3 1 2
Output Sample # 1: 
11

Explanation

[Sample Description]

First to an n-th positions, respectively the height of species 2 0 , 1 0 , . 3 0 , a tree is 0, the highest value.

[Agreed] with the scale data

For 2 0 % of the data, there are n ≤ 10 ;

For . 4 0 % of the data, there n≤100 ;

For 60% of the data, there n≤1000 ;

For . 1 0 0 % of the data, there 4≤n≤100000 , and to ensure that n must be an even number.

 

analysis:

This problem is more troublesome, the main reason is two-dimensional DP can not be transferred, need to be extended for the rare three-dimensional DP.

State transition equation is:

f[j][0][0]=max(f[j-1][1][1],f[j-1][2][1])+a[j][0];

f[j][1][0]=f[j-1][2][1]+a[j][1];

f[j][1][1]=f[j-1][0][0]+a[j][1];

f[j][2][1]=max(f[j-1][1][0],f[j-1][0][0])+a[j][2];

 

CODE:

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 int n;
 8 int a[100005][3];
 9 int f[100005][3][2];
10 int ans;
11 inline int get(){
12     char c=getchar();
13     int res=0;
14     while (c<'0'||c>'9') c=getchar();
15     while (c>='0'&&c<='9'){
16         res=(res<<3)+(res<<1)+c-'0';
17         c=getchar();
18     }
19     return res;
20 }
21 int main(){
22     n=get();
23     for(int i=1;i<=n;i++){
24         a[i][0]=get();
25         a[i][1]=get();
26         a[i][2]=get();
27     }
28     for(int i=0;i<3;i++){
29         for(int j=0;j<3;j++){
30             for(int k=0;k<2;k++){
31                 f[1][j][k]=0;
32             }
33         }
34         f[1][i][0]=f[1][i][1]=a[1][i];
35         for(int j=2;j<=n;j++){
36             f[j][0][0]=max(f[j-1][1][1],f[j-1][2][1])+a[j][0];
37             f[j][1][0]=f[j-1][2][1]+a[j][1];
38             f[j][1][1]=f[j-1][0][0]+a[j][1];
39             f[j][2][1]=max(f[j-1][1][0],f[j-1][0][0])+a[j][2];
40         }
41         for(int j=0;j<i;j++) ans=max(ans,f[n][j][0]);
42         for(int j=2;j>i;j--) ans=max(ans,f[n][j][1]);
43     }
44     printf("%d\n",ans);
45     return 0;
46 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11116154.html