NYOJ236 心急的C小加&NYOJ 16 矩形嵌套

来源:互联网 发布:淘宝男装销量店铺排行 编辑:程序博客网 时间:2024/05/22 03:19

分析题目,一求单调递增子序列个数,一求最长单调子序列。

故而,解题方法不一,贪心PK动态规划

NYOJ 236 心急的C小加  贪心

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct mb{    int len;    int weight;}w[10001];bool cmp(mb x,mb y) //排序{    if(x.len<y.len)  return true;  //升序    if(x.len==y.len&&x.weight<y.weight)  return true;    return false;}int main(){    int s,n,i,j,count,t;    scanf("%d",&s);    while(s--)    {        count=0;        scanf("%d",&n);        for(i=0;i<=n-1;i++)        {            scanf("%d %d",&w[i].len,&w[i].weight);        }        sort(w,w+n,cmp);        for(i=0;i<=n-1;i++)        {            if(w[i].weight!=0) //数未出现过            {                t=w[i].weight;                count++;  //开启一单位                for(j=i+1;j<=n-1;j++)                {                    if(w[j].weight>=t)                    {                        t=w[j].weight;                        w[j].weight=0;                    }                }             }         }         printf("%d\n",count);    }    return 0;}

NYOJ 16 矩形嵌套  DP

#include<cstdio>#include<algorithm>#include<cstring>#define M 1024using namespace std;struct rect{    int len,width;}d[M],p[M];bool cmp(rect a,rect b)//按长排降序,后再按宽排{    if(a.len==b.len) return a.width<b.width;    return a.len>b.len;}int main(){    int m;    scanf("%d",&m);    while(m--)    {        int n,i,j,t;        scanf("%d",&n);        for(i=0;i<n;i++){            scanf("%d %d",&d[i].len,&d[i].width);            if(d[i].width>d[i].len)                {                    t=d[i].width;                    d[i].width=d[i].len;                    d[i].len=t;                }           }        sort(d,d+n,cmp);        int length=1;        p[0].len=900,p[0].width=900;        //单调递减最长子序列        for(i=0;i<n;i++)//终止位置        {            for(j=length-1;j>=0;j--)//length 长度             {                 if(d[i].width<p[j].width&&d[i].len<p[j].len) { p[j+1]=d[i];  if(j+1==length) length++; break;}             }        }        printf("%d\n",length-1);        }}


0 0
原创粉丝点击