hdu 1051

来源:互联网 发布:windows怎么开发ios 编辑:程序博客网 时间:2024/06/06 21:38
一直以为是右边的和第一个比,测试数据也正确却一直WA,无语。

贪心,扫描标记下。

import java.util.Arrays;import java.util.Scanner;class Stick implements Comparable{     public int length;    public int weight;    public Stick(int length,int weight)    {         this.length=length;        this.weight=weight;    }     public int compareTo(Object o)     {         int t = length -((Stick)o).length;         if(t!=0)return t;        return weight -((Stick)o).weight;    }     }  public class Main{     public static void main(String[] args)     {          Scanner cin=new Scanner(System.in);        int n=cin.nextInt();        while(n>0)        {            int m=cin.nextInt();                        boolean flag[]=new boolean[m];             int sum=0;            Stick s[]=new Stick[m];            for(int i=0;i<m;i++)            {                int len=cin.nextInt();                int wei=cin.nextInt();                s[i]=new Stick(len,wei);            }           Arrays.sort(s);           for(int i=0;i<m;i++)           {               if(flag[i]) continue;               int temp=s[i].weight;               for(int j=i+1;j<m;j++)               {                   if(!flag[j]  && temp<=s[j].weight)                   {                       flag[j]=true;                       temp=s[j].weight;                   }               }               sum++;           }           System.out.println(sum);           n--;        }    }  }