A group of people sit together, each guess a number within 100, and the one whose number is closest to half of everyone's average wins. This question requires you to find the winner among them.
Input format:
Enter a positive integer N (≤104) in the first line. Next N lines, each line gives a player's name (a string consisting of no more than 8 English letters) and the guessed positive integer (≤ 100).
Output format:
Sequential output on a line: half of the average of everyone (only the integer part is output), the name of the winner, separated by a space. The title guarantees that the winner is unique.
Input sample:
7
Bob 35
Amy 28
James 98
Alice 11
Jack 45
Smith 33
Chris 62
Sample output:
22 Amy
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f;
priority_queue<int,vector<int>,greater<int>>q;
const int mod=10e9+7;
int main()
{
int N;
cin>>N;
pair<string,int>p[10100];
ll sum=0;
for(int i=0;i<N;i++)
{
cin>>p[i].first>>p[i].second;
sum+=p[i].second;
}
ll mid_aver=(sum/N)/2;
ll gap=INF;
string winner="";
for(int i=0;i<N;i++)
{
if(abs(p[i].second-mid_aver)<gap)
{
gap=abs(p[i].second-mid_aver);
winner=p[i].first;
}
}
cout<<mid_aver<<" "<<winner;
return 0;
}