"Dynamic planning" mr368-the tree of the bishop

Source: Internet
Author: User

"The main topic"

The master has a circular garden where he wants to plant n trees evenly around the garden, but the soil of the master garden is very special, the trees are different for each location, and some trees may lose their ornamental value because they are unsuitable for the soil of this location. The Archbishop likes 3 kinds of trees most, the height of these 3 kinds of trees is 10,20,30 respectively. The leader hopes that this circle of trees have a hierarchical sense, so any one location of the tree than it is adjacent to the height of the two trees are high or low, and in this condition, the leader wants you to design a plan, make the value of the highest ornamental.

"Input Format"

The 1th behavior of the input is a positive integer n, which represents the tree number of trees that need to be planted.

The next n lines, each line 3 not more than 10000 positive integer ai,bi,ci, in a clockwise order, the first position of the height of the 10,20,30 tree can be obtained by the ornamental value.

The tree of the I position is adjacent to the tree in the i+1 position, in particular, the 1th position of the tree adjacent to the nth position of the tree.

"Output Format"

The output includes only a positive integer for maximum viewing value and.

"Sample Input"

4

1 3 2

3 1 2

3 1 2

3 1 2

"Sample Output"

11

"Sample description"

In the first place, the height of the 20,10,30,10 tree was planted in 1~n, which was the highest value.

"Data Size"

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

For 40% of the data, there are n≤100;

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

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

Ideas

F[I][0..3] Represents the sum of the maximum ornamental values of the first I tree, respectively.

F[i][0] The current tree height is 10, and the height of the tree before and after is greater than it (this is inevitable);

F[I][1] The current tree height is 20, and the tree height before and after is greater than it;

F[I][2] The current tree height is 20, and the height of the tree before and after is less than it;

F[I][3] The current tree height is 30, and the height of the tree before and after is less than it (which is also inevitable).

Next in the above four cases for the first tree for four times DP, each time the F[i]=max (f[n-1][the case of the previous tree), around the tree to return to the starting point, such as f[i][0] corresponding to the previous tree is f[i-1][2] and f[i-1][3].

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 Const intmaxn=100000+ -;6 using namespacestd;7 intf[maxn][4];8 /*f[i][0..3] Ö±ð±íê¾ç°i¿ãê÷µäxî´ó¹ûéí¼ûöµxüºí9 F[i][0]µ±ç°ê÷¸ß¶èîª10£¬çòç°ºóµäê÷¸ß¶è¾ù´óóúëü£¨õâêç±øè»µä£©Ten F[i][1]µ±ç°ê÷¸ß¶èîª20£¬çòç°ºóµäê÷¸ß¶è¾ù´óóúëü One F[i][2]µ±ç°ê÷¸ß¶èîª20£¬çòç°ºóµäê÷¸ß¶è¾ùð¡óúëü A F[i][3]µ±ç°ê÷¸ß¶èîª30£¬çòç°ºóê÷µä¸ß¶è¾ùð¡óúëü£¨õâò²êç±øè»µä£©*/ - inta[maxn][3];  - /*A[i][j]±íê¾µúi¸öî»öãµújööê÷µäéóãà¼ûöµ*/  the intN,ans; -  - voidInit () - { +scanf"%d",&n); -      for(intI=0; i<n;i++) +          for(intj=0;j<3; j + +) scanf ("%d",&a[i][j]); A } at  - voiddpintx) - { -      for(intI=0;i<4; i++) f[0][i]=-0x7fffffff; -f[0][x]=a[0[(x+1)/2]; -      for(intI=1; i<n;i++) in     { -f[i][0]=max (f[i-1][2],f[i-1][3]) +a[i][0];  tof[i][1]=f[i-1][3]+a[i][1]; +f[i][2]=f[i-1][0]+a[i][1]; -f[i][3]=max (f[i-1][0],f[i-1][1]) +a[i][2]; the     } * } $ Panax Notoginseng  - voidmainprocess () the { +ans=-1; Adp0); theAns=max (Ans,max (f[n-1][2],f[n-1][3])); +dp1); -Ans=max (ans,f[n-1][3]); $dp2); $Ans=max (ans,f[n-1][0]); -dp3); -Ans=max (Ans,max (f[n-1][0],f[n-1][1])); thecout<<ans<<Endl; - }Wuyi  the intMain () - { WuFreopen ("Mr368.in0","R", stdin); -Freopen ("mr368.ou0","W", stdout); About init (); $ mainprocess (); -     return 0; -}

"Dynamic planning" mr368-the tree of the bishop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.