吉哥系列故事——臨時工計劃
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3479 Accepted Submission(s): 1364
已知吉哥一共有m天的假期,每天的編號從1到m,一共有n份可以做的工作,每份工作都知道起始時間s,終止時間e和對應的工資c,每份工作的起始和終止時間以天爲單位(即天數編號),每份工作必須從起始時間做到終止時間才能得到總工資c,且不能存在時間重疊的工作。比如,第1天起始第2天結束的工作不能和第2天起始,第4天結束的工作一起被選定,因爲第2天吉哥只能在一個地方工作。
現在,吉哥想知道怎麼安排才能在假期的m天內獲得最大的工資數(第m+1天吉哥必須返回學校,m天以後起始或終止的工作是不能完成的)。
[Technical Specification]
1<=T<=1000
9<m<=100
0<n<=1000
s<=100, e<=100, s<=e
c<=10000
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int s,e,w;
};
node x[1004];
int MAX(int x,int y);
bool cmp(node x,node y)
{
if(x.s==y.s) return x.e<y.e;
return x.s<y.s;
}
int main()
{
int T=0;
scanf("%d",&T);
while(T--)
{
int n=0,m=0;
int dp[150]={0};
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
{
int a=0,b=0,c=0;
scanf("%d%d%d",&a,&b,&c);
if(a>=1&&b<=m)
{
x[i].s=a;
x[i].e=b;
x[i].w=c;
}
else
{
i--;
n--;
}
}
sort(x,x+n,cmp);
for(int i=0;i<n;i++)
{
for(int j=x[i].e;j<=m;j++)
{
dp[j]=MAX(dp[x[i].s-1]+x[i].w,dp[j]);
}
}
printf("%d\n",dp[m]);
}
return 0;
}
int MAX(int x,int y)
{
if(x>y) return x;
return y;
}