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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【POJ - 3616】Milking Time (贪心+dp)

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3616】Milking Time (贪心+dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next?N?(1 ≤?N?≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.

Farmer John has a list of?M?(1 ≤?M?≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval?i?has a starting hour (0 ≤?starting_houri?≤?N), an ending hour (starting_houri?<?ending_houri?≤?N), and a corresponding efficiency (1 ≤?efficiencyi?≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest?R?(1 ≤?R?≤?N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the?N?hours.

Input

* Line 1: Three space-separated integers:?N,?M, and?R
* Lines 2..M+1: Line?i+1 describes FJ's ith milking interval withthree space-separated integers:?starting_houri?,?ending_houri?, and?efficiencyi

Output

* Line 1: The maximum number of gallons of milk that Bessie can product in the?Nhours

Sample Input

12 4 2 1 2 8 10 12 19 3 6 24 7 10 31

Sample Output

43

題目大意:

貝茜是一個勤勞的牛。事實上,她如此??專注于最大化她的生產力,于是她決定安排下一個N(1≤N≤1,000,000)小時(方便地標記為0..N-1),以便她生產盡可能多的牛奶。

農民約翰有一個M(1≤M≤1,000)可能重疊的間隔列表,他可以在那里進行擠奶。每個區間我有一個起始小時(0≤starting_houri≤N),一個結束小時(starting_houri <ending_houri≤N),以及相應的效率(1≤efficiencyi≤1,000,000),表示他可以從中獲取多少加侖的牛奶。貝西在那段時間。 Farmer John分別在開始時間和結束時間開始時開始和停止擠奶。在擠奶時,Bessie必須在整個間隔內擠奶。

盡管貝茜有其局限性。在任何間隔期間擠奶后,她必須休息R(1≤R≤N)小時才能再次開始擠奶。鑒于Farmer Johns的間隔清單,確定Bessie在N小時內可以產生的最大牛奶量。

一句話題意:

給奶牛擠奶,共m次可以擠,給出每次開始擠奶的時間st,結束擠奶的時間ed,還有擠奶的量ef,每次擠完奶要休息r時間,問最大擠奶量.

解題報告:

對于每一次擠奶,結束時間+=休息時間.

先把m次擠奶按照開始時間排個序,用f[i]表示擠完第i個時間段的奶以后的最大擠奶量,那么有:

f[i]=max(f[i],f[j]+(第i次擠奶.ef)) (1<=j<i&&(第j次擠奶).ed<=(第i次擠奶).st).

附上一年前寫的垃圾代碼Orz

這題一個點,一般這種題都是考慮貪心,從時間軸上去貪心,但是這題是dp,從第 i 個段 去考慮。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; typedef struct Node{int start;int end;int value; } node; int dp[1005]; bool cmp (const node & a,const node & b) {return a.start<b.start; } int main() {int n,m,r;node cow[1005];cin>>n>>m>>r;for(int i = 1 ; i<=m; i++) {scanf("%d%d%d",&cow[i].start,&cow[i].end,&cow[i].value);cow[i].end+=r;} sort(cow+1,cow+m+1,cmp);for(int i = 1 ; i<=m; i++) {dp[i]=cow[i].value;} for(int i = 1; i<=m; i++) {for(int j = 1; j<=i; j++) {if(cow[i].start>=cow[j].end) {dp[i]=max(dp[j]+cow[i].value,dp[i]);}}}printf("%d\n",*max_element(dp+1,dp+m+1));return 0 ;} /* 12 4 2 1 2 8 10 12 19 3 6 24 7 10 31*/

?

總結

以上是生活随笔為你收集整理的【POJ - 3616】Milking Time (贪心+dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

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