关于最长区间调度
Milking Time
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
题意:给 m 个区间,现在可以选择若干个区间,要求是两个相邻区间的 right 与 left 的差 >= R. 选择一个区间可以获得相应的值,问最后这个值最大是多少
思路:先对区间按右端点排序(因为二分的时候是根据右端点进行check)。。dp[i] 表示取到 i 个区间可以获得的最大值,那么每一次,可以二分一个符合题意的区间下标 index (有可能没有),那么 dp[i] = max(dp[i - 1],dp[index] + val[i])
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)
using namespace std;
const int maxn = 1010;
struct xx{
int fir,sec;
int effi;
}interval[maxn];
LL dp[maxn];
bool cmp(xx A,xx B){
if(A.sec != B.sec)
return A.sec < B.sec;
return A.fir < B.fir;
}
int n,m,R;
int Search(int st){
int l = 0,r = m;
int ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(interval[mid].sec + R > st)
r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
int main() {
while(~scanf("%d%d%d",&n,&m,&R)){
for(int i = 1;i <= m;++ i)
scanf("%d%d%d",&interval[i].fir,&interval[i].sec,&interval[i].effi);
sort(interval + 1,interval + 1 + m,cmp);
for(int i = 1;i <= m;++ i){
int index = Search(interval[i].fir);
LL maxx;
if(index > 0){
maxx = dp[index] + interval[i].effi;
}
else
maxx = interval[i].effi;
dp[i] = max(dp[i - 1],maxx);
}
printf("%lld\n",dp[m]);
}
return 0;
}