这是一道贪心的题,具体思路是先对有序对进行排序,规则是长度小的优先,若长度l相等,则以宽度小的优先,选择的时候,从小到大,先找最小的,然后依次排序,标记(弄个解向量,然后找第二小的。。。。不过我最开始还是参考了别人的代码,虽然思路是我自己想的。。。。
从别人的代码我学到的东西
1.qsort好久没用过了。。。。不过他的cmp函数功能真强大,可以按照你自己的定义的规则进行排序。还有就是对结构体的元素进行操作,注意声明他是结构体里的变量。
2.memset函数,第三个参数注意写清几个元素。
3.还是细节决定成败啊。。。
一次a的有进步
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<mem.h>
using namespace std;
const int maxn=5000+1000;
typedef struct Node
{
int w;
int l;
}node;
node woods[maxn];
int cmp(const void*a,const void*b)
{
if((*(node*)a).w<(*(node*)b).w) return -1;
else if((*(node*)a).w>(*(node*)b).w) return 1;
else if((*(node*)a).w==(*(node*)b).w)
{
if((*(node*)a).l<=(*(node*)b).l) return -1;
else return 1;
}
}
int select[maxn];//解向量
int main()
{
int t;
// freopen("1.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
int p;
scanf("%d",&p);
for(int i=0;i<p;i++)
{
scanf("%d%d",&woods[i].w,&woods[i].l);
}
qsort(woods,p,sizeof(woods[0]),cmp);//从小到大排序
//for(int i=0;i<p;i++)
//select[i]=0;
memset(select,0,sizeof(int)*p);
int times=0;
while(1)
{
int i;
for( i=0;i<p;i++)
{
if(select[i]==0) break;
}
if(i==p) break;
times++;
for(int j=i;j<p;j++)
{
if(!select[j])
{
if(woods[i].w<=woods[j].w&&woods[i].l<=woods[j].l)
{
i=j;
select[j]=1;
}
}
}
}
printf("%d\n",times);
}
return 0;
}