A. Game(sort排序)

A. Game
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Two players play a game.

Initially there are
n
integers
a
1
,
a
2
,

,
a
n
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

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

1
turns if both players make optimal moves.

Input
The first line contains one integer
n
(
1

n

1000
) — the number of numbers on the board.

The second line contains
n
integers
a
1
,
a
2
,

,
a
n
(
1

a
i

10
6
).

Output
Print one number that will be left on the board.

Examples
inputCopy
3
2 1 3
outputCopy
2
inputCopy
3
2 2 2
outputCopy
2
Note
In the first sample, the first player erases
3
and the second erases
1
.
2
is left on the board.

In the second sample,
2
is left on the board regardless of the actions of the players.

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[1001];
    int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a+1,a+1+n);
    if(n%2==0)
    {
        printf("%d\n",a[n/2]);
    }
    else printf("%d\n",a[n/2+1]);
    return 0;
}

题意:给定一些数,一个人每次会删除最大的,另一个人每次会删除最小的,然后进行n-1轮,所以只要把数排好序,偶数的话就剩中间两对前面的那个,因为我是升序排列的,第一个人先是删除的最大的,所以是从后面开始删除;奇数的话就剩中间那个数了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值