日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

The Triangle

發布時間:2024/4/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 The Triangle 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目限制

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 59262 Accepted: 35557
時間限制: 1000ms 內存限制: 10000k
提交:59262 接受:35557

Description

描述

73 88 1 02 7 4 44 5 2 6 5

(Figure 1)
Figure 1 shows a number triangle.
圖1顯示了一個數字三角形。
Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base.
編寫一個程序,計算從頂部開始,在底部某個位置結束的路由上傳遞的最大數字總和。
Each step can go either diagonally down to the left or diagonally down to the right.
每一步都可以向左斜向下或向右斜向下。

Input

輸入
Your program is to read from standard input.
您的程序將從標準輸入中讀取。
The first line contains one integer N: the number of rows in the triangle.
第一行包含一個整數n:三角形中的行數。
The following N lines describe the data of the triangle.
下面的n行描述了三角形的數據。
The number of rows in the triangle is > 1 but <= 100.
三角形中的行數大于1但小于等于100。
The numbers in the triangle, all integers, are between 0 and 99.
三角形中的數字,所有整數,都在0到99之間。

Output

輸出
Your program is to write to standard output.
您的程序將寫入標準輸出。
The highest sum is written as an integer.
最高的和被寫為一個整數。

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30
Source
IOI 1994

解題思路

用二維數組存放數字三角形

D(i,j):第i行第j個數字(r,j從1開始算)
MaxSum(r,j):從D(i,j)到底邊的各條路徑,最佳路徑的數字之和。

問題:求MaxSum(1,1)

典型的遞歸問題:
D(i,j)出發,下一步只能走到D(i+1,j)或者D(i+1,j+1)。

遞歸代碼

#include <iostream> #include <algorithm> #define MAX 101 using namespace std; int D[MAX][MAX]; int n; int MaxSum(int i,int j) {if(i==n){return D[i][j];}int x=MaxSum(i+1,j);int y=MaxSum(i+1,j+1);return max(x,y)+D[i][j]; } int main() {int i,j;cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>D[i][j];}}cout<<MaxSum(1,1)<<endl;return 0; }

想都不用想,肯定會:Time Limit Exceeded

為什么會超時?

重復計算:
如果采用遞歸的方法,深度遍歷每條路徑,存在大量重復計算,時間復雜度為2n

改進

如果每算出一個MaxSum()就保存起來,下次用到其值的時候直接取用,則可免去重復計算。
因為三角形的數字總數是n(n+1)/2,那么可以用O(n2)時間完成計算。

記憶遞歸型動態規劃程序

#include <iostream> #include <algorithm> #define MAX 101 using namespace std; int maxSum[MAX][MAX]; int D[MAX][MAX]; int n; int MaxSum(int i,int j) {if(maxSum[i][j]!=-1){return maxSum[i][j];}if(i==n){maxSum[i][j]=D[i][j];}else{int x=MaxSum(i+1,j);int y=MaxSum(i+1,j+1);maxSum[i][j]=max(x,y)+D[i][j];}return maxSum[i][j]; } int main() {int i,j;cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>D[i][j];maxSum[i][j]=-1;}}cout<<MaxSum(1,1)<<endl;return 0; }

遞歸轉遞推

從下往上,每兩個數往上找一層最大數:

#include <iostream> #include <algorithm> #define MAX 101 using namespace std; int D[MAX][MAX],n; int maxSum[MAX][MAX]; int main () {cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>D[i][j];if(i==n) maxSum[n][j]=D[n][j];}}for(int i=n-1;i>=1;i--){for(int j=1;j<=i;j++){maxSum[i][j]=max(maxSum[i+1][j],maxSum[i+1][j+1])+D[i][j];}}cout<<maxSum[1][1]<<endl;return 0; }

空間優化

直接用D的最后一行存放maxSum

#include <iostream> #include <algorithm> #define MAX 101 using namespace std; int D[MAX][MAX],n; int * maxSum; int main () {cin>>n;for(int i=1;i<=n;i++){for(int j=1;j<=i;j++){cin>>D[i][j];}}maxSum=D[n];for(int i=n-1;i>=1;i--){for(int j=1;j<=i;j++){maxSum[j]=max(maxSum[j],maxSum[j+1])+D[i][j];}}cout<<maxSum[1]<<endl;return 0; }

總結

以上是生活随笔為你收集整理的The Triangle的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。