您的位置:首页 > 其它

zoj1025 dp(呃。。)

2016-02-05 21:12 351 查看
这道题放在DP训练题组里,但是我用的贪心做出来的

题目大意:问最少能用多少组非递减数列覆盖所给数列。。网上看的大多数做法和我差不多,排序好以后进行搜索与标记,然后就AC了。。并不知道在哪里用DP。。

以下是代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>

using namespace std;

struct anode
{
int len,weight;
int ret;
}a[12345];

bool cmp(anode x, anode y)
{
if(x.len==y.len) return x.weight<y.weight;
return x.len<y.len;
}

int main()
{
// freopen("in.txt","r",stdin);
int n,T;
scanf("%d",&T);
while(T--){
memset(a,0,sizeof(a));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].len,&a[i].weight);

sort(a,a+n,cmp);
int ans=0;
for(int i=0;i<n;i++){
if(a[i].ret) continue;
ans++;
int tmp=a[i].weight;
for(int j=i;j<n;j++){
if(a[j].weight>=tmp && a[j].ret==0) a[j].ret=1,tmp=a[j].weight;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: