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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

PTA 1002 Business (35分)

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

想試試PTA Top Level的難度,然后隨便來了一題~

1002 Business (35分)

As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the following:

  • Project[1] takes 3 days, it must be finished in 3 days in order to gain 6 units of profit.
  • Project[2] takes 2 days, it must be finished in 2 days in order to gain 3 units of profit.
  • Project[3] takes 1 day only, it must be finished in 3 days in order to gain 4 units of profit.

You may take Project[1] to gain 6 units of profit. But if you take Project[2] first, then you will have 1 day left to complete Project[3] just in time, and hence gain 7 units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤50), and then followed by N lines of projects, each contains three numbers P, L, and D where P is the profit, L the lasting days of the project, and D the deadline. It is guaranteed that L is never more than D, and all the numbers are non-negative integers.

Output Specification:

For each test case, output in a line the maximum profit you can gain.

Sample Input:

4 7 1 3 10 2 3 6 1 2 5 1 1

Sample Output:

18

思路

很明顯的01背包問題,我們知道普通01背包不論枚舉物品的順序如何對最終結果都是沒有影響的。

而本題中由于deadline的限制,需要按照deadline從小到大排個序(這樣后面想替換掉前面的才方便),然后開始枚舉選和不選這件物品才可以保證得到正確的結果。

背包問題我的做法是畫表格來推狀態轉移方程:

dp[i][j] 前i件物品中,當第i件放入容量為j的背包可以獲得的最大利潤。

需要注意的是 枚舉的容量大于這件物品的deadline后:

  • 選擇 dp[i][j-1]
  • 不選 dp[i-1][j]
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);

代碼

#include <iostream> #include <algorithm> using namespace std; const int N = 55; const int MAX = 1e5; struct Project {int p,l,d;bool operator <(const Project x) const{return d < x.d;} } a[N];int dp[N][MAX];int main() {int n;cin>>n;int maxx = 0;for(int i = 1; i <= n; ++i){cin>>a[i].p>>a[i].l>>a[i].d;maxx = max(maxx,a[i].d);}sort(a+1,a+n+1);for(int i = 1; i <= n; ++i){for(int j = 1; j <= maxx; ++j){if(j <= a[i].d)if(j - a[i].l < 0)dp[i][j] = dp[i-1][j];else if(j - a[i].l == 0)dp[i][j] = max(dp[i-1][j],a[i].p);elsedp[i][j] = max(dp[i-1][j],dp[i-1][j-a[i].l]+a[i].p);elsedp[i][j] = max(dp[i-1][j],dp[i][j-1]);}}/*for(int i = 1; i <= n; ++i){for(int j = 1; j <= maxx; ++j){cout<<dp[i][j]<<" ";}cout<<endl;}*/cout<<dp[n][maxx]<<endl;return 0; } /* 6 7 1 3 10 2 3 6 1 2 5 1 1 26 6 7 1 1 8 */

嘗試壓縮空間復雜度

本題我A了后打算壓縮空間復雜度,但是我發現上面提到的當超過deadline部分修改成:

dp[j] = max(dp[j],dp[j-1]);

因為是倒著算,當dp[j-1]在要用的時候卻沒有算出來(當背包容量超過當前枚舉到的物品時的deadline,我打算選這個物品的時候)。。

應該是我處理方式不對,想了好久,還是壓縮空間復雜度失敗,留坑!!!

#include <iostream> #include <algorithm> using namespace std; const int N = 55; const int MAX = 1e5; struct Project {int p,l,d;bool operator <(const Project x) const{return d < x.d;} } a[N];int dp[MAX];int main() {int n;cin>>n;int maxx = 0;for(int i = 1; i <= n; ++i){cin>>a[i].p>>a[i].l>>a[i].d;maxx = max(maxx,a[i].d);}sort(a+1,a+n+1);for(int i = 1; i <= n; ++i){for(int j = maxx; j > 0; --j){if(j <= a[i].d){if(j - a[i].l >= 0)dp[j] = max(dp[j],dp[j-a[i].l]+a[i].p);}elsedp[j] = max(dp[j],dp[j-1]); //這里不能這樣!!!!}}cout<<dp[maxx]<<endl;return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的PTA 1002 Business (35分)的全部內容,希望文章能夠幫你解決所遇到的問題。

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