题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4502
腾讯第二届马拉松3月20日第0场第三题。
DP题。和HIT秋季校赛的一道题目:http://blog.youkuaiyun.com/niuox/article/details/8469017 基本类似。
只要把超过end超过m的先删除掉即可。其中best[i]表示选择第i分工作获得的最大收益,即第i份工作必须选择。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct Job
{
int start;
int end;
int price;
};
Job p[1005];
int best[1005];
bool cmp(Job a,Job b)
{
return a.end<b.end;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
scanf(" %d",&t);
while(t--)
{
int m,n;
scanf(" %d %d",&m,&n);
for(int i=0; i<n; i++)
{
scanf(" %d %d %d",&p[i].start,&p[i].end,&p[i].price);
}
sort(p,p+n,cmp);
int temp = n;
for(int i=0;i<n;i++)
{
if(p[i].end>m)
{
temp = i;
break;
}
}
n = temp;
for(int i=0; i<n; i++)
{
best[i] = p[i].price;
}
for(int i=0; i<n; i++)
{
for(int j=i-1; j>=0; j--)
{
if(p[i].start>p[j].end)
{
best[i] = max(best[i],best[j] + p[i].price);
}
}
}
int ans = 0;
for(int i=0; i<n; i++)
{
if(best[i]>ans)
{
ans = best[i];
}
}
printf("%d\n",ans);
}
return 0;
}
比较贴合思维方式的是二维的DP。详见代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct Job
{
int start;
int end;
int price;
};
Job p[1005];
int best[1005][105];//best[i][j]表示选择前i份截至日期在j获取的最大的
bool cmp(Job a,Job b)
{
return a.start<b.start;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
scanf(" %d",&t);
while(t--)
{
int m,n;
scanf(" %d %d",&m,&n);
for(int i=1; i<=n; i++)
{
scanf(" %d %d %d",&p[i].start,&p[i].end,&p[i].price);
}
sort(p+1,p+n+1,cmp);
for(int j=0;j<=m;j++)
{
if(j<p[1].end)
{
best[1][j] = 0;
}
else
{
best[1][j] = p[1].price;
}
}
for(int j=2;j<=n;j++)
{
for(int k=1;k<=m;k++)
{
if(k<p[j].end)
{
best[j][k] = best[j-1][k];
}
else if(best[j-1][p[j].start-1] + p[j].price > best[j-1][k])
{
best[j][k] = best[j-1][p[j].start - 1] + p[j].price;
}
else
{
best[j][k] = best[j-1][k];
}
}
}
printf("%d\n",best[n][m]);
}
return 0;
}