题目
分析
两个关键字都是从大到小排序,可以保证左边的第一关键字比右边大,在此基础上从左到右维护一个最大第二关键字判断当前笔记本是否被完虐,可以求出被碾压的笔记本数 。
做法很简单。假设内存为a,速度为b,题目要求我们求ai<aj,bi<bj的j有多少 。
根据上面的排序方法,从左到右a是单减的,然后只要从左到右枚举,只要考虑当前元素的b小于前面的b就会被完爆。
代码
import java.util.Arrays;
import java.util.Scanner;
public class FST {
static class off implements Comparable<off>{
private int me,sp;
public off(int me,int sp) {
this.me=me;this.sp=sp;
}
@Override
public int compareTo(off o) {
if(me>o.me) {
return -1;
}else if(me<o.me) {
return 1;
}else {
if(sp>o.sp) {
return -1;
}else if(sp<o.sp) {
return 1;
}
return 0;
}
}
}
static off a[];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
a=new off[n+1];int max;int ans=0;
for (int i =1;i<a.length; i++) {
a[i]=new off(sc.nextInt(),sc.nextInt());
}
Arrays.sort(a,1,a.length); //排序
max=a[1].sp;
for (int i =2; i<a.length; i++) { //统计答案
if(max>=a[i].sp) {
ans++;
}else {
max=a[i].sp;
}
}
System.out.println(ans);
}
}