RQNOJ 569 Milking Time
Title Description
Portal
Can be placed on a number line M line segments, each segment starting end termination given (an integer), and each segment has a score, asking how to select from a number of segments such that the distance between any two segments is greater than R. Each segment belongs to [0, N]. How to select these segments, so that the sum of the maximum score?
FROM USACO
Input Format
The first row of three integers, N, M, R. M <= 1000; N <= 1000000
the M rows, each row of three integers, indicates the start point, end point scores.
Output Format
An integer, maximum score.
Sample
Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Output
43
The main idea
This question look, a glance is a classic dp title - Select the line, but more than a "distance greater than R" . The selected line of the title, it is extended by the 01 backpack comes, but more endpoints from the point into a line. Here's how, will speak about the topic selected segments :( want to know the contents of this section chiefs skip directly to the picture below)
if we are to change the title of the "R" excised for "any two segments do not coincidence " , is the traditional line selection problem. Such problems, only a structure of the left end of open storage segment (L), right point (r) and the score (w), and then the structure is sorted by ascending left point, it becomes a knapsack problem. With DP [i] represents the largest fraction in the selected article segment i, and then the left end point prior to enumeration segment i j, then just consider the right end point of the line segment j is smaller than i is equal to the left end of the segment, if it is on the transfer, otherwise unchanged. At the same time, not necessarily due to the last segment is selected, the answer is not necessarily dp [m] (e.g. dp [m] is negative infinity), so to update each step ans.
Transfer process is as follows:
here, the only problem is that new entrants to the distance R, in order to solve this problem, I took by far the most retarded way: Suppose each line segment longer than the original R, is not solved? Thus, for read, the right end point of the line segment to + R.
code show as below
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <map>
#include <stack>
#include <cstring>
using namespace std;
#define ll long long
struct nod{//存储线段
int l,r,w;
}a[1005];
int n,m,r,ans=0;
int dp[1000005];
bool cmp(nod x,nod y)
{
return x.l<y.l;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin>>n>>m>>r;
for(int i=1;i<=m;i++)
{
cin>>a[i].l>>a[i].r>>a[i].w;
a[i].r+=r;//距离问题
}
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
{
dp[i]=a[i].w;//首先必须选自己
for(int j=1;j<i;j++)//由于排好序,只需枚举前面的线段即可
if(a[j].r<=a[i].l)//如果合法
dp[i]=max(dp[i],dp[j]+a[i].w);//转移
ans=max(ans,dp[i]);//更新答案
}
cout<<ans<<endl;//输出
return 0;
}