每日一九度之 题目1030:毕业bg
生活随笔
收集整理的這篇文章主要介紹了
每日一九度之 题目1030:毕业bg
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
時間限制:1 秒
內存限制:32 兆
特殊判題:否
提交:2046
解決:894
題目描述:??? 例如有4場bg:
??? 第1場快樂度為5,持續1小時,發起人必須在1小時后離開;
??? 第2場快樂度為10,持續2小時,發起人必須在3小時后離開;
??? 第3場快樂度為6,持續1小時,發起人必須在2小時后離開;
??? 第4場快樂度為3,持續1小時,發起人必須在1小時后離開。
??? 則獲得最大快樂度的安排應該是:先開始第3場,獲得快樂度6,在第1小時結束,發起人也來得及離開;再開始第2場,獲得快樂度10,在第3小時結束,發起人正好來得及離開。此時已經無法再安排其他的bg,因為發起人都已經離開了學校。因此獲得的最大快樂度為16。
??? 注意bg必須在發起人離開前結束,你不可以中途離開一場bg,也不可以中途加入一場bg。
又因為你的人緣太好,可能有多達30個團體bg你,所以你需要寫個程序來解決這個時間安排的問題。
??? h l t
??? 其中 h 是快樂度,l是持續時間(小時),t是發起人離校時間。數據保證l不大于t,因為若發起人必須在t小時后離開,bg必須在主人離開前結束。
??? 當N為負數時輸入結束。
??? 每個測試用例的輸出占一行,輸出最大快樂度。
第一眼的感覺就是這題不是貪心就是DP,果然---DP。
DP不熟啊!
參考代碼:http://blog.csdn.net/wtyvhreal/article/details/42076485
//Asimple #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cctype> #include <cstdlib> #include <stack> #include <cmath> #include <map> #include <string> #include <queue> #define INF 100000 using namespace std; const int maxn = 1005; typedef long long ll; int m[31][maxn]; int n; struct bg_typ{int h;int l;int t;bool operator < (const bg_typ& A) const {return A.t > t;} }; bg_typ bg[31];int main(){while( cin >> n && n >= 0 ){int mmax = 0;for(int i=1; i<=n; i++){cin >> bg[i].h >> bg[i].l >> bg[i].t ;if( bg[i].t > mmax ) mmax = bg[i].t; }sort(bg+1,bg+n+1);for(int i=0; i<=n; i++)m[i][0] = m[0][i] = 0;for(int i=1; i<=n; i++){for(int j=0; j<=mmax; j++){if( j<=bg[i].t && j-bg[i].l>=0){m[i][j] = max(m[i-1][j],m[i-1][j-bg[i].l]+bg[i].h);} else m[i][j] = m[i-1][j];}}int result=m[n][mmax]; for(int j=mmax;j>=0;--j)if(result<m[n][j]) result=m[n][j]; cout<<result<<endl; }return 0; }翻了翻題解,看到有的用dfs也做出來了,就測試了下是DP好,還是DFS好。
DFS代碼:
#include <cstdio> #include <algorithm> using namespace std; const int N = 30 + 5; struct Node { int h; int l; int t; }; int n; Node node[N]; int ans; int w; bool cmp(const Node &a, const Node &b); void dfs(int cur, int t, int h); int main() { #ifndef ONLINE_JUDGE freopen("e:\\uva_in.txt", "r", stdin); #endif // ONLINE_JUDGE while (scanf("%d", &n) == 1) { if (n < 0) break; w = 0; for (int i = 0; i < n; i++) { scanf("%d%d%d", &node[i].h, &node[i].l, &node[i].t); w += node[i].h; } sort(node, node + n, cmp); ans = 0; dfs(0, 0, 0); printf("%d\n", ans); } return 0; } bool cmp(const Node &a, const Node &b) { if (a.t != b.t) return a.t < b.t; return (double)a.h / a.l > (double)b.h / b.l; } void dfs(int cur, int t, int h) { if (cur == n) { if (ans < h) ans = h; return; } w -= node[cur].h; if (t + node[cur].l <= node[cur].t) { dfs(cur + 1, t + node[cur].l, h + node[cur].h); } if (h + w > ans) dfs(cur + 1, t, h); w += node[cur].h; }在杭電測試的,上面的是動態規劃,下面的是dfs。
| ?Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
| 18227261 | 2016-09-11 10:40:16 | Accepted | 1881 | 46MS | 1928K | 1480 B | C++ | Asimple |
| 18227252 | 2016-09-11 10:39:32 | Accepted | 1881 | 62MS | 1720K | 1405 B | C++ | Asimple |
各有各的優勢吧!
轉載于:https://www.cnblogs.com/Asimple/p/5861206.html
總結
以上是生活随笔為你收集整理的每日一九度之 题目1030:毕业bg的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LINUX 下tcp 和 udp 套接字
- 下一篇: 物联网之感知-分布式光纤传感-应用前景分