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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【动态规划】Part1

發布時間:2023/11/27 生活经验 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【动态规划】Part1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 硬幣找零

題目描述:假設有幾種硬幣,如1、3、5,并且數量無限。請找出能夠組成某個數目的找零所使用最少的硬幣數。

分析: ? dp [0] = 0
? ? ? ? ? ?dp [1] = 1 + dp [1-1]
? ? ? ? ? ?dp [2] = 1 + dp [2-1]
? ? ? ? ? ?dp [3] = min (dp [3 - 1] + 1, dp [3 - 3] + 1)

 1 #include<iostream>
 2 #include<algorithm>
 3 #define INF 32767
 4 using namespace std;
 5 
 6 int dp[100];
 7 int coin[3] = { 1, 2, 3 };
 8 
 9 int main()
10 {
11     int sum;
12     cin >> sum;
13     dp[0] = 0;
14     for (int i = 1; i <= 100; ++i)
15         dp[i] = INF;
16     for (int i = 1; i <= sum; ++i)
17         for (int j = 0; j <= 2; ++j)
18             if (coin[j] <= i)
19                 dp[i] = min(dp[i], dp[i - coin[j]] + 1);
20     cout << dp[sum] << endl;
21     return 0;
22 }

2. 最長遞增子序列

? 題目描述:最長遞增子序列(Longest Increasing Subsequence)是指找到一個給定序列的最長子序列的長度,使得子序列中的所有元素單調遞增。

給定一個序列,求解它的最長 遞增 子序列 的長度。比如: arr[] = {3,1,4,1,5,9,2,6,5} ? 的最長遞增子序列長度為4。即為:1,4,5,9

?

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int arr[9] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
 6 int dp[9];
 7 
 8 int main()
 9 {
10     for (int i = 0; i < 9; ++i)
11         dp[i] = 1;
12     for (int i = 1; i < 9; ++i)
13         for (int j = 0; j < i; ++j)
14             if (arr[i] > arr[j])
15                 dp[i] = max(dp[i], dp[j] + 1);
16     int mi = 0;
17     for (int i = 0; i < 6; ++i)
18         mi = max(mi, dp[i]);
19     cout << mi << endl;
20     return 0;
21 }

3. 數字三角形

Problem description
7

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

(Figure 1)

Figure 1 shows a number triangle. 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 T, the number of test cases, for each test case: the first line contain a integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.?

Output
Your program is to write to standard output. The highest sum is written as an integer for each test case one line.

Sample Input
15
7
3 8
8 1 0 
2 7 4 4
4 5 2 6 5
Sample Output
30
Problem Source
IOI?1994

代碼:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int dp[100][100];
 6 int arr[100][100];
 7 
 8 int main()
 9 {
10     int N;
11     cin >> N;
12     if (N == 1)
13         cout << N;
14     for (int i = 0; i < N; ++i)
15         for (int j = 0; j <= i; ++j)
16             cin >> arr[i][j];
17     for (int i = 0; i < N; ++i)
18         dp[N - 1][i] = arr[N - 1][i];
19     for (int i = N - 2; i >= 0; --i)
20         for (int j = 0; j <= i; ++j)
21             dp[i][j] = max(arr[i][j] + dp[i + 1][j], arr[i][j] + dp[i + 1][j + 1]);
22     cout << dp[0][0] << endl;
23     return 0;
24 }

?4. 最大最大連續子序列和/積

??求取數組中最大連續子序列和,例如給定數組為A={1, 3, -2, 4, -5}, 則最大連續子序列和為6,即1+3+(-2)+ 4 = 6。

??求取數組中最大連續子序列積。

?

?

?

?

參考資料

? 常見動態規劃問題分析與求解
??關于序列的面試題2------------最大連續子序列和以及積

?

?

轉載于:https://www.cnblogs.com/sunbines/p/9011149.html

總結

以上是生活随笔為你收集整理的【动态规划】Part1的全部內容,希望文章能夠幫你解決所遇到的問題。

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