列表

详情


NC24946. [USACO 2008 Jan B]Election Time

描述

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 <= N <= 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.
The election consists of two rounds. In the first round, the K cows (1 <= K <= N) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.
Given that cow i expects to get Ai votes (1 <= Ai <= 1,000,000,000) in the first round and Bi votes (1 <= Bi <= 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.

输入描述

* Line 1: Two space-separated integers: N and K
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi

输出描述

* Line 1: The index of the cow that is expected to win the election.

示例1

输入:

5 3
3 10
9 2
5 6
8 4
6 5

输出:

5

说明:

There are 5 cows, 3 of which will advance to the second round. The cows expect to get 3, 9, 5, 8, and 6 votes, respectively, in the first round and 10, 2, 6, 4, and 5 votes, respectively, in the second.
Cows 2, 4, and 5 advance to the second round; cow 5 gets 5 votes in the second round, winning the election.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python(2.7.3) 解法, 执行用时: 219ms, 内存消耗: 15220K, 提交时间: 2019-07-30 23:43:29

import sys
from collections import Counter

line = sys.stdin.readline().strip().split()
n, k = int(line[0]), int(line[1])
v = []
for i in range(1, n + 1):
    line = sys.stdin.readline().strip().split()
    v.append([i, int(line[0]), int(line[1])])

a1 = sorted(v, key=lambda x: x[1], reverse=True)[:k]
b1 = max(a1, key=lambda x: x[2])
print(b1[0])



C++ 解法, 执行用时: 22ms, 内存消耗: 960K, 提交时间: 2021-06-08 09:26:37

#include <bits/stdc++.h>
using namespace std;
struct c
{
    int a,b,x;
};
c  n[50001];
int b(c a,c b)
{return a.a>b.a;
}
int b1(c a,c b)
{return a.b>b.b;
}
int main()
{
    int m,k,i;
    cin>>m>>k;
    for(i=0;i<m;i++)
        scanf("%d%d",&n[i].a,&n[i].b),n[i].x=i;
    sort(n,n+m,b);
     sort(n,n+k,b1);
    cout<<n[0].x+1;
 
}

Python3(3.5.2) 解法, 执行用时: 338ms, 内存消耗: 16284K, 提交时间: 2019-07-29 11:35:02

N,K=map(int,input().split())
arr=[list(map(int,input().split()))+[i] for i in range(N)]
arr.sort(reverse=True)
arrk=arr[:K]
arrk.sort(key=lambda x:(-x[1],x[2]))
print(arrk[0][2]+1)

上一题