Milking Time【動態規劃-dp】

Milking Time

?POJ - 3616?

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,m,r,n代表最大時間,下面m行分別輸入三個整數s,e,eff,表示從s時刻開始到e時刻結束共能收獲eff的價值,注意每兩段工作時間之間必須間隔r小時。

解決方法:先將其按照開始時間從小到大排序,然后求出dp[i]=max(dp[i],dp[j]+arr[i].eff)。

AC代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{int s;int e;int eff;
}arr[1200];
bool cmp(node a,node b)
{if(a.s==b.s)return a.e<b.e;elsereturn a.s<b.s;
}
int dp[1200];
int main() 
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int n,m,r;cin>>n>>m>>r;rep(i,1,m) {cin>>arr[i].s>>arr[i].e>>arr[i].eff;}sort(arr+1,arr+1+m,cmp);int ans=-1;for(int i=1;i<=m;i++){dp[i]=arr[i].eff;for(int j=1;j<i;j++){if(arr[j].e+r<=arr[i].s){dp[i]=max(dp[i],dp[j]+arr[i].eff);}}ans=max(ans,dp[i]);}cout<<ans<<endl;return 0;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536358.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536358.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536358.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTTP首部(1)

1、報文首部 HTTP協議的請求和響應必定包含HTTP首部&#xff0c;它包括了客戶端和服務端分別處理請求和響應提供所需要的信息。報文主體字兒是所需要的用戶和資源的信息都在這邊。  HTTP請求報文組成 方法&#xff0c;URL&#xff0c;HTTP版本&#xff0c;HTTP首部字段 HTTP響…

UVA272--TEX Quotes【字符串】

TEX Quotes UVA - 272 題目傳送門 題目大意&#xff1a;將輸入字符串中的所有對雙引號的做雙引號改為 &#xff0c;右雙引號改為 。 解決方法&#xff1a;遍歷一遍及時修改即可。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <…

XMLHttpRequest+WebForm模式(接口IHttpHandler)實現ajax

首先引入ajax.js文件 創建xmlhttpRequest對象 Code//創建XMLHttpRequest對象var xmlHttp;function newXMLHttpRequest() { if (window.XMLHttpRequest) { xmlHttp new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp …

UVA----10082?WERTYU【字符串】

WERTYU UVA - 10082 題目傳送門 題目大意&#xff1a;按照所給的鍵盤樣式&#xff0c;以及錯誤的字符串&#xff0c;輸出正確的字符串&#xff0c;其輸入的每一個字符都按照鍵盤樣式向右錯移了一位。 解決方法&#xff1a;將整個鍵盤用數組存起來&#xff0c;遍歷一遍即可。…

關于C生成的匯編與C++生成的匯編在函數名稱上的差異

最近用到ucos&#xff0c;這個RTOS本身是用C語言和部分匯編編寫&#xff0c;而自己又打算用C來寫應用&#xff0c;在其中遇到幾個問題&#xff0c;一番折騰之后&#xff0c;讓我更加深刻認識到了在一些一般不注意的細節上&#xff0c;C與C的不同。 1、對于ucos&#xff0c;雖…

UVA401????????Palindromes【字符串】

Palindromes UVA - 401 題目傳送門 題目大意&#xff1a;給你一個字符串&#xff0c;判斷其是回文串還是鏡像串。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #…

IIS 5 與IIS 6 原理介紹

[ 轉] ASP.NET Process Model之一&#xff1a;IIS 和 ASP.NET ISAPI 前幾天有一個朋友在MSN上問我“ASP.NET 從最初的接收到Http request到最終生成Response的整個流程到底是怎樣的&#xff1f;”我覺得這個問題涉及到IIS和ASP.NETASP.NET Runtime的處理模型的問題&#xff0c;…

UVA340????????Master-Mind Hints【數組】

Master-Mind Hints UVA - 340 題目傳送門 題目大意&#xff1a;先輸入一個整數n&#xff0c;表示有n個數字&#xff0c;下面第一行代表正確答案&#xff0c;其下每一行代表用戶猜的答案&#xff0c;需統計其有多少數字位置正確&#xff08;A&#xff09;&#xff0c;有多少數…

教你如何把自己從好友的QQ中刪除

在QQ中&#xff0c;有些人看了不太順眼&#xff0c;真不知當初為何讓他加自己為好友的&#xff01; 那有什么辦法&#xff0c;可以把自己從對方的QQ中刪除呢&#xff1f; 其實&#xff0c;用QQ就可以輕松搞定&#xff01; 讓我來為你支一招吧&#xff01; 打開QQ&#xff0…

UVA1583?Digit Generator

Digit Generator UVA - 1583 題目傳送門 題目大意&#xff1a;若x的各位數之和加上x本身等于y&#xff0c;則證明x是y的生成元&#xff0c;求輸入數字n的最小生成元。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> …

C++內存詳解

偉大的Bill Gates 曾經失言&#xff1a; 640K ought to be enough for everybody — Bill Gates 1981 程序員們經常編寫內存管理程序&#xff0c;往往提心吊膽。如果不想觸雷&#xff0c;唯一的解決辦法就是發現所有潛伏的地雷并且排除它們&#xff0c;躲是躲不了的。本文的內…

UVA1584????????Circular Sequence【字符串】

Circular Sequence UVA - 1584 題目傳送門 題目大意&#xff1a;輸入一個環形字符串&#xff0c;需輸出其最小字典序的形式的字符串。 AC代碼&#xff1a; #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #includ…

UVA1585 Score

Score UVA - 1585 題目傳送門 題目大意&#xff1a;輸入一個字符串&#xff0c;O的分數為1&#xff0c;若出現連續的O&#xff0c;如OOOO...&#xff0c;分數為1,2,3,4...&#xff0c;X為0分&#xff0c;求最終的分數 AC代碼&#xff1a; #include <cstdio> #includ…

operater int()

class Number { int number; public: explicit Number(int n){number n;} operator int() //注意一定不能聲明返回值 { return number; } }; int main () { Number n1 Number(100); int n2 n1; cout << n2 << endl; re…

UVA1586???????? Molar mass

Molar mass UVA - 1586 題目傳送門 題目大意&#xff1a;給你一個只包含C,H,O,N分子式&#xff0c;其中C,H,O,N的原子量分別為&#xff1a;12.01,1.008,16.00,14.01&#xff0c;求其分子量 AC代碼&#xff1a; #include <cstdio> #include <iostream> #includ…

SharePoint v3:忘掉模擬用戶Impersonate,SPSecurity.RunWithElevatedPrivileges來了

回顧&#xff1a; 在SharePoint V2 大家應該都用過模擬用戶Impersonate這個功能&#xff0c; 這個功能用來暫時提升某個用戶的權限&#xff0c;比如某個普通用戶的本來不能修改某個列表的值&#xff0c;但是我們功能需要在修改。 缺點&#xff1a; 我們使用這個模擬用戶功能…

UVA1225????????Digit Counting

Digit Counting UVA - 1225 題目傳送門 題目大意&#xff1a;輸入一個數字T&#xff0c;代表有T組測試數據&#xff0c;下面每行有一個整數n&#xff0c;求將1到n的數字連成一串后每個數字出現的個數。 AC代碼&#xff1a; #include <cstdio> #include <iostream&…

Chess Queen【數學】

Chess Queen UVA - 11538 題目傳送門 題目大意&#xff1a;輸入兩個整數n,m&#xff0c;在n行m列的棋盤中放入白黑兩個棋子&#xff0c;棋子在同一行、同一列或同一對角線上能相互進攻&#xff0c;問有多少種擺放方案。 AC代碼&#xff1a; #include <cstdio> #incl…

Java開發中保證接口的冪等性問題

目錄 1、解決方案 2、使用token保證接口冪等性的例子 3、在實際項目中&#xff0c;如何有效地使用token法來保證接口的冪等性&#xff1f; 4、3示例中如何獲取請求中的 token 5、如果token驗證失敗&#xff0c;如何處理 6、在上述示例代碼中加上token過期后重置的功能 7…

typedef 的四個用途和兩大陷阱

>>>>>用途一&#xff1a;定義一種類型的別名&#xff0c;而不只是簡單的宏替換。可以用作同時聲明指針型的多個對象。比如&#xff1a;char* pa, pb; // 這多數不符合我們的意圖&#xff0c;它只聲明了一個指向字符變量的指針&#xff0c; // 和一個字符變量&am…