NC24946. [USACO 2008 Jan B]Election Time
描述
输入描述
* 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.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)