Two players play a game.
Initially there are nn integers a1,a2,…,ana1,a2,…,an written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i. e. n−1n−1 turns are made. The first player makes the first move, then players alternate turns.
The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.
You want to know what number will be left on the board after n−1n−1 turns if both players make optimal moves.
The first line contains one integer nn (1≤n≤10001≤n≤1000) — the number of numbers on the board.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106).
Print one number that will be left on the board.
3 2 1 3
2
3 2 2 2
2
题意:有n个数字,两个人轮流删数字,最后剩一个数,先手那个人想保留最小值,后手那个人想保留最大值,问你最后会留下的数字什么。
思路:第一个人肯定删最大的,第二个人肯定删最小的,sort排个序,推一下规律可以发现n是偶数留下a[n/2],n是奇数留下a[n/2+1]。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define exp 1e-8
#define mst(a,k) memset(a,k,sizeof(a))
#define pi acos(-1.0)
using namespace std;
ll a[1010],n;
int main()
{
while(~scanf("%lld",&n))
{
for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
sort(a+1,a+n+1);
if(n&1)printf("%lld\n",a[n/2+1]);
else printf("%lld\n",a[n/2]);
}
return 0;
}