Laptop 题解
Laptop
https://ac.nowcoder.com/acm/problem/14266
首先我们先对笔记本的内存进行排序,记得同时交换速度的位置。
然后我们从后往前遍历内存,同时记录后k项的最大值max,如果当前速度小于max,就说明完败与一台笔记本。
最后记录个数即可.
import java.math.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.*; public class Main { public static void main(String args[])throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); in.nextToken(); int n = (int)in.nval; int m[] = new int[n]; int s[] = new int[n]; int max[] = new int[n]; for(int i=0;i<n;i++) { in.nextToken(); m[i] = (int)in.nval; in.nextToken(); s[i] = (int)in.nval; } quickSort(0,n-1,m,s); int maxs=0,sum=0; for(int i=n-1;i>=0;i--) { if(s[i]>maxs) { maxs = s[i]; } else { sum++; } } out.print(sum); out.flush(); } public static void quickSort(int l,int r,int m[],int s[]) { int i=l,j=r,key=m[(l+r)>>1],temp,temps; do{ while(m[i]<key) i++; while(m[j]>key) j--; if(i<=j) { temp = m[i]; m[i] = m[j]; m[j] = temp; temps = s[i]; s[i] = s[j]; s[j] = temps; i++; j--; } }while(i<=j); if(i<r) quickSort(i,r,m,s); if(l<j) quickSort(l,j,m,s); } }