zoj1025 dp(呃。。)

来源:互联网 发布:反监听软件 编辑:程序博客网 时间:2024/06/12 01:04

这道题放在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;}


0 0
原创粉丝点击