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 1Sample Output:
18思路
很明顯的01背包問題,我們知道普通01背包不論枚舉物品的順序如何對(duì)最終結(jié)果都是沒有影響的。
而本題中由于deadline的限制,需要按照deadline從小到大排個(gè)序(這樣后面想替換掉前面的才方便),然后開始枚舉選和不選這件物品才可以保證得到正確的結(jié)果。
背包問題我的做法是畫表格來推狀態(tài)轉(zhuǎn)移方程:
dp[i][j] 前i件物品中,當(dāng)?shù)趇件放入容量為j的背包可以獲得的最大利潤(rùn)。
需要注意的是 枚舉的容量大于這件物品的deadline后:
- 選擇 dp[i][j-1]
- 不選 dp[i-1][j]
代碼
#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 */嘗試壓縮空間復(fù)雜度
本題我A了后打算壓縮空間復(fù)雜度,但是我發(fā)現(xiàn)上面提到的當(dāng)超過deadline部分修改成:
dp[j] = max(dp[j],dp[j-1]);因?yàn)槭堑怪?#xff0c;當(dāng)dp[j-1]在要用的時(shí)候卻沒有算出來(當(dāng)背包容量超過當(dāng)前枚舉到的物品時(shí)的deadline,我打算選這個(gè)物品的時(shí)候)。。
應(yīng)該是我處理方式不對(duì),想了好久,還是壓縮空間復(fù)雜度失敗,留坑!!!
#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位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的PTA 1002 Business (35分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微型统计分析系统README
- 下一篇: 数据结构-堆(最大堆)