Usaco Section1.5 Number triangles problem solving report

Source: Internet
Author: User

Numtri Problem Solving report--icedream61 Blog Park (reproduced please specify the source)
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
There is a pyramid of numbers, shaped as follows
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5
To start from the top, each time only left or right down, the number and the maximum value.
"Data Range"
1<=r<=1000
0<= each number <=100
"Input Sample"
5
7
3 8
8 1 0
2 7 4 4
4 5 2) 6 5
"Output Example"
30
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Dp.
Set NUM[I][J] is the number of I row J columns, D[i][j] is the current optimal solution from 1 rows and 1 columns to the I row J column.
D[1][1]=NUM[1][1];
d[i][j]=0; I>1 or J>1
From 1 rows and 1 columns to the maximum value of a column in the R row, it can be directly recursive, the state transition equation is as follows:
When i==1,j>=2, D[i][j]=d[i-1][j]+num[i][j];
When i>=2,j>=2, D[i][j]=max{d[i-1][j]+num[i][j],d[i-1][j-1]+num[i][j]};
But the problem, recursion has boundary conditions, more trouble, recursion more convenient, the state transfer equation is as follows:
When I>=2,2<=j<r
D[I+1][J]=MAX{D[I+1][J],D[I][J]+NUM[I+1][J]};
D[i+1][j+1]=max{d[i+1][j+1],d[i][j]+num[i+1][j+1]};
Finally, just sweep over d[r][1~r] to get the maximum output.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
Once again AC.

--------------------------------------------------------------------------------------------------------------- ---------------------------------

Code

1 /*2 id:icedrea13 Prob:numtri4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten  One intR; A intnum[1001][1001],d[1001][1001]; -  - voidChangeint&r,intx) {if(X>r) r=x;} the  - intMain () - { -Ifstreaminch("numtri.in"); +Ofstream out("Numtri.out"); -  +     inch>>R; A      for(intI=1; i<=r;++i) at          for(intj=1; j<=i;++j)inch>>Num[i][j]; -  -d[1][1]=num[1][1]; -      for(intI=1; i<r;++i) -          for(intj=1; j<=i;++j) -         { inChange (d[i+1][j],d[i][j]+num[i+1][j]); -Change (d[i+1][j+1],d[i][j]+num[i+1][j+1]); to         } +  -     intmaxsum=0; the      for(intj=1; j<=r;++j) Change (Maxsum,d[r][j]); *      out<<maxSum<<Endl; $ Panax Notoginseng     inch. Close (); -      out. Close (); the     return 0; +}

Usaco Section1.5 Number triangles problem solving report

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.