hdu1051

#include<iostream>  

#include<algorithm>  

using namespace std;  

  

struct SIZE  

{  

    int l;  

    int w;  

}sticks[5005];  

int flag[5005];  

  

bool cmp(const SIZE &a,const SIZE &b)//这里是排序!  

{//写排序函数的时候要特别的小心!  

    //if(a.w!=b.w)//这里写错了,这里表示如果重量不等,按照长度排,如果重量相等,则按照重量排!(没意义!)  

    if(a.l!=b.l)  

        return a.l>b.l;//长度不等时按照长度排,从大到小排  

    else  

        return a.w>b.w;//长度相等时,再按照重量从大到小排列  

}  

  

int main()  

{  

    int n,min,cases;  

    int i,j,s;  

      

  

    cin>>cases;   

    for(j=0;j<cases;j++)  

    {  

        cin>>n;  

        for(i=0;i<n;i++)  

        {  

            cin>>sticks[i].l>>sticks[i].w;  

            flag[i]=0;  

        }  

        sort(sticks,sticks+n,cmp);  

  

        s=0;  

        for(i=0;i<n;i++)  

        {  

            if(flag[i]) continue;  

            min=sticks[i].w;  

  

            for(int j=i+1;j<n;j++)  

            {  

                if(min>=sticks[j].w && !flag[j])  

                {  

                    min=sticks[j].w;  

                    flag[j]=1;  

                }  

            }  

            s++;  

        }  

        cout<<s<<endl;  

    }  

    //system("pause");  

    return 0;  

}  

  1. #include<iostream>  
  2. #include<algorithm>  
  3. using namespace std;  
  4.   
  5. struct SIZE  
  6. {  
  7.     int l;  
  8.     int w;  
  9. }sticks[5005];  
  10. int flag[5005];  
  11.   
  12. bool cmp(const SIZE &a,const SIZE &b)//这里是排序!  
  13. {//写排序函数的时候要特别的小心!  
  14.     //if(a.w!=b.w)//这里写错了,这里表示如果重量不等,按照长度排,如果重量相等,则按照重量排!(没意义!)  
  15.     if(a.l!=b.l)  
  16.         return a.l>b.l;//长度不等时按照长度排,从大到小排  
  17.     else  
  18.         return a.w>b.w;//长度相等时,再按照重量从大到小排列  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     int n,min,cases;  
  24.     int i,j,s;  
  25.       
  26.   
  27.     cin>>cases;   
  28.     for(j=0;j<cases;j++)  
  29.     {  
  30.         cin>>n;  
  31.         for(i=0;i<n;i++)  
  32.         {  
  33.             cin>>sticks[i].l>>sticks[i].w;  
  34.             flag[i]=0;  
  35.         }  
  36.         sort(sticks,sticks+n,cmp);  
  37.   
  38.         s=0;  
  39.         for(i=0;i<n;i++)  
  40.         {  
  41.             if(flag[i]) continue;  
  42.             min=sticks[i].w;  
  43.   
  44.             for(int j=i+1;j<n;j++)  
  45.             {  
  46.                 if(min>=sticks[j].w && !flag[j])  
  47.                 {  
  48.                     min=sticks[j].w;  
  49.                     flag[j]=1;  
  50.                 }  
  51.             }  
  52.             s++;  
  53.         }  
  54.         cout<<s<<endl;  
  55.     }  
  56.     //system("pause");  
  57.     return 0;  
  58. }  
posted @ 2017-01-18 00:14  王坤1993  阅读(128)  评论(0编辑  收藏  举报