java 自定义数组排序函数_Java数组排序中Comparator接口实现自定义排序

要求:叠罗汉过程中,站在某个人肩上的人应该既比自己矮又比自己瘦,或相等。

*/

public class CircusTroup {

static class People{

int num;

int weight;

int height;

public People(int num,int weight,int height){

this.num=num;

this.weight=weight;

this.height=height;

}

}

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

int N;

while (scanner.hasNext()){

N=scanner.nextInt();

String inValid=scanner.nextLine();//无效

People[] peoples=new People[N];

for(int i=0;i

String[] str=scanner.nextLine().split(" ");

peoples[i]=new People(Integer.valueOf(str[0]),Integer.valueOf(str[1]),Integer.valueOf(str[2]));

}

//第一处:注意地方

//先按照体重升序,再按照身高降序

//为什么身高要降序?因为:体重相同时,只有身高也相同才可以站在自己肩上,比自己矮是不能站在自己肩上的。

//而身高降序就保证了:体重相同时,身高不同为降序,不是升序序列,则不能站在自己肩上.

Arrays.sort(peoples,new Comparator() {

@Override

public int compare(People o1,People o2) {

if(o1.weight==o2.weight){

return o2.height-o1.height;

}else{

return o1.weight-o2.weight;

}

}

});

//第二处:注意地方 db部分开始

//按身高求最大升序子序列,不是太会

int[] dp=new int[N];

dp[0]=1;

int level=0;

for(int i=1;i

dp[i]=1;

for(int j=0;j代码,可以知道:子序列并不需要连续,只要满足if条件就可以

if(peoples[j].height<=peoples[i].height && dp[j]+1>dp[i])

dp[i]=dp[j]+1;

}

}

Arrays.sort(dp);

level=dp[N-1];

System.out.println(level);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值