HDU2881 Jack’s struggle (LIS)
Description
给定一个n*n的场地,与m个任务,每个任务要求在第t秒时到达(r,c)位置,每一秒你可以向上下左右移动一个单位。第0秒你可以在任意未知,求最多可以完成多少任务。
题解
很容易看出来,我们把任务按照时间排序之后就是一个LIS问题了。
判断是否可以转移就是看时间差是否大于欧几里德距离。
这题可以
O(n2)
卡过。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define MAXN 10000+10
#define max(a,b) (a>b)?a:b
using namespace std;
int n,m,d[MAXN],ans;
struct Date{
int x,y,t;
}a[MAXN];
bool cmp(const Date a,const Date b) { return a.t < b.t; }
bool check(int i,int j)
{
if(abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y)>abs(a[i].t-a[j].t)) return 0;
else return 1;
}
int main()
{
while(1)
{
scanf("%d%d",&n,&m);
if(n==0&&m==0) break;
memset(a,0,sizeof(a));
memset(d,0,sizeof(d));
ans=0;
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].t,&a[i].x,&a[i].y),d[i]=1;
sort(a+1,a+m+1,cmp);
for(int i=2;i<=m;i++)
{
for(int j=i-1;j>=1;j--)
if(check(i,j))
d[i]=max(d[i],d[j]+1);
ans=max(ans,d[i]);
}
printf("%d\n",ans);
}
return 0;
}