73:牛的選舉
總時間限制:
1000ms
內存限制:
65536kB
描述
現在有N(1<=n<=50000)頭牛在選舉它們的總統,選舉包括兩輪:第一輪投票選舉出票數最多的k(1<=k<=n)頭牛進入第二輪;第二輪對k頭牛重新投票,票數最多的牛當選爲總統。< p="">
現在給出每頭牛i在第一輪期望獲得的票數Ai(1<=Ai<=1,000,000,000),以及在第二輪中(假設它進入第二輪)期望獲得的票數Bi(1<=Bi<=1,000,000,000),請你預測一下哪頭牛將當選總統。幸運的是,每輪投票都不會出現票數相同的情況。
<=n<=50000)頭牛在選舉它們的總統,選舉包括兩輪:第一輪投票選舉出票數最多的k(1<=k<=n)頭牛進入第二輪;第二輪對k頭牛重新投票,票數最多的牛當選爲總統。<>
輸入
第1行:N和K
第2至N+1行:第i+1行包括兩個數字:Ai和Bi
輸出
當選總統的牛的編號(牛的編號從1開始)
樣例輸入
5 3 3 10 9 2 5 6 8 4 6 5
樣例輸出
5
實現代碼如下:
import java.util.*;
/**
* @author baikunlong
* @date 2020/6/23 12:10
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
LinkedList<Cow> cows = new LinkedList<>();
for (int i = 0; i < n; i++) {
cows.add(new Cow(scanner.nextInt(),scanner.nextInt(),i+1));
}
cows.sort(Comparator.comparingInt(o->(o.first)));
Collections.reverse(cows);
while (cows.size()>k){
cows.removeLast();
}
cows.sort(Comparator.comparingInt(o->(o.second)));
Collections.reverse(cows);
System.out.println(cows.get(0).index+"");
}
static class Cow{
int first;
int second;
int index;
public Cow(int first, int second, int index) {
this.first = first;
this.second = second;
this.index = index;
}
}
}