现在的位置: 首页 > 综合 > 正文

poj 1065Wooden Sticks (简单的贪心)

2017年11月16日 ⁄ 综合 ⁄ 共 1023字 ⁄ 字号 评论关闭

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct node{
int l,w;
bool flag;
}a[5100];
/*int cmp(node x,node y)
{
if(x.l!=y.l)
return x.l-y.l;
else if(x.l==y.l)
return x.w-y.w;
else 
return 0;
if(x.l==y.l)
return x.w<y.w;//x.w-y.w;
else
return x.l-y.l;//x.l-y.l;
}*/
int cmp(const void *x,const void *y)
{
if( ((struct node*)x)->l != ((struct node *)y)->l)
return ((struct node *)x)->l-((struct node*)y)->l;
else
        return ((struct node*)x)->w-((struct node *)y)->w;
}
int main()
{
  int t,i,j;
  cin>>t;
  while(t--)
  {
 int n;
 cin>>n;
      for(i=0;i<n;i++)
 {
 cin>>a[i].l>>a[i].w;
 a[i].flag=false;
 }
//sort(a,a+n,cmp);//Ϊʲô²»¶Ô£¡£¡£¡£¡£¡£¡£¡
 qsort(a,n,sizeof(a[0]),cmp);
/*for(i=0;i<n;i++)
 cout<<a[i].l<<"***"<<a[i].w<<endl;*/
 int max=0,min=-1,ans=0;
      /*for(i=1;i<n;i++)
 {
         if(a[i].w>=max)
max=a[i].w;
else if(a[i].w>=min)
{
if(min==-1)  ans++;
min=a[i].w;
}
else
{
           ans++;
  min=a[i].w;
}
 }*/
 for(i=0;i<n;i++)
 {
 if(a[i].flag==false)
 {
ans++;
max=a[i].w;
   for(j=i;j<n;j++)
     if(a[j].flag==false && a[j].w>=max)
 {
max=a[j].w;
a[j].flag=true;
 }
 }
 }

 cout<<ans<<endl;
  }
  return 0;
}

抱歉!评论已关闭.