九度1499:項目安排http://ac.jobdu.com/problem.php?pid=1499
九度1463:招聘會http://ac.jobdu.com/problem.php?pid=1463
都是時間安排問題,dp數組下標可以表示時間也可以表示項目數,具體方法可以學習博客http://blog.csdn.net/wdy_yx/article/details/9833897,總結的很好。
我自己在做的時候,忘記了先按結束時間排序,如果不按照結束時間先後來安排,如先安排結束晚的,再安排結束早的,那麼先開始早的再開始晚的這種情況就不會被考慮到。另外,項目安排這題雖然用dp數組下標表示時間是可以正確得出結果的,但由於時間可能很大,遍歷數組費時,所以很容易超時。ac代碼如下:
1463:
#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
struct hui
{
int s,e;
bool operator < (const hui &a) const
{
return e<a.e;
}
}hui[1000];
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int late=0;
for (int i=0;i<n;++i)
{
scanf("%d %d",&hui[i].s,&hui[i].e);
if (late<hui[i].e) late=hui[i].e;
}
sort(hui,hui+n);
int dp[late+1];
memset(dp,0,sizeof dp);
for (int i=0;i<n;++i)
for (int j=late;j>=hui[i].e;--j)
if (dp[j]<dp[hui[i].s]+1)
dp[j]=dp[hui[i].s]+1;
printf("%d\n",dp[late]);
}
return 0;
}
1499:
#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;
struct pro
{
int s,e,v;
bool operator < (const pro &a) const
{
return e<a.e;
}
}buf[10000];
int main()
{
freopen("input.txt","r",stdin);
int n;
while (scanf("%d",&n)!=EOF)
{
int late=0;
for (int i=1;i<=n;++i)
{
scanf("%d%d%d",&buf[i].s,&buf[i].e,&buf[i].v);
if (buf[i].e>late)
late=buf[i].e;
}
sort(buf+1,buf+n+1);
int dp[n+1];
memset(dp,0,sizeof dp);
for (int i=1;i<=n;++i)
{
int last=0;
for (int j=i-1;j>0;--j)
if (buf[j].e<=buf[i].s)
{
last=j;break;
}
for (int j=n;j>=i;--j)
if (dp[last]+buf[i].v>dp[j])
dp[j]=dp[last]+buf[i].v;
}
printf("%d\n",dp[n]);
}
return 0;
}