NC205095. ASimpleProblemaboutelection
描述
There are candidates and the voluntary team only needs several volunteers in total. To ensure the quality of the team, they decided to hold an election to pick out the volunteers. Every resident has to nominate exactly candidates and mustn't give more than one nomination to each candidate. (of course one can save one of the nominations for himself/herself). The candidates then will be ranked by the nominations they get in descending order, if there are candidates have the same number of nominations, they will be ranked by their names in lexicographically increasing order.
输入描述
The input consists of several test cases. The first line of the input is an integer , the number of the test cases.
For each test case, the first line will be two integers . The number of candidates and the number of nominations that each resident can make.
The next line will be integers separated by spaces, which are the current number nominations of each candidate. The first integer always refers to the nominations of ZZZZSGW.
For each test case, , . And there is for all test cases.
输出描述
For each test case, output a single integer in a line, which is the best place ZZZZSGW can get after his turn.
示例1
输入:
2 5 3 5 1 2 6 7 5 3 5 1 2 5 7
输出:
3 2
C++14(g++5.4) 解法, 执行用时: 316ms, 内存消耗: 992K, 提交时间: 2020-04-26 13:50:21
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int a[100010]; int main(){ int t,n,m; cin>>t; while(t--){ int sum1=0,sum2=0; cin>>n>>m; scanf("%d",&a[1]); for(int i=2;i<=n;++i){ scanf("%d",&a[i]); if(a[i]>a[1]) sum1++; else if(a[i]<a[1]) sum2++; } if(m-1<=sum1+sum2) cout<<sum1+1<<endl; else cout<<m-sum2<<endl; } }
C++(clang++11) 解法, 执行用时: 72ms, 内存消耗: 3252K, 提交时间: 2021-04-26 19:54:14
#include<bits/stdc++.h> using namespace std; int a[100005],n,m;; void work(){ scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%d",a+i); int x=a[0]; sort(a,a+n),a[n]=1e9+5; int y=upper_bound(a,a+n+1,x-1)-a,z=upper_bound(a,a+n+1,x)-a,num=n-z+y; if(num>=m-1)printf("%d\n",n-z+1); else printf("%d\n",n-z+m-num); } int main(){ int T;scanf("%d",&T); while(T--)work(); }