第十七届中国计量大学程序设计竞赛 D Dessert Time

第十七届中国计量大学程序设计竞赛 D Dessert Time

题目描述
The party began, the greasy uncle was playing cards, the fat otaku was eating, and the little beauty was drawing.

Because there were too many steaks just grilled, ZJH found that he could not finish it, so he gave some of it to others. After LZT felt too greasy, he asked the kitchen to make some cakes for dessert. The kitchen put the finished cakes into many plates, each with several cakes.

At this time, LZT was talking with a friend. To show humility, people do not like to take the last plate and persuade each other to take more. So LZT and his friends always took a plate which has no fewer cakes than the other take last time. When a person found that there is no plate on the table that has no fewer cakes than the other take last time, he must take all the remaining cakes (if had), we usually called it “dōu dǐ” in Chinese.

Neither of them wanted to take the last plate, and Chairman Luo could not personally play due to his status, so the task was handed over to his assistant, that is, you.

Now given a number N, indicating the number of plates, and an array a, representing the number of cakes on each plate. During one move, the player chooses an existing number x and takes away a plate with cake number x. The next player’s choice of x cannot be less than this time. Of course, the first choice is unlimited. The person who takes the last plate loses, and the person who cannot choose x will take all the plates left on the table. You are asked to tell Chairman Luo the first x to win, or -1 if he cannot win. Because of his status, Chairman Luo will always take the first move.
输入描述:
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:

The first line contains an integer N (3 ≤ N ≤ 10^5 ), indicating the number of plates.
The second line contains N integers, a1, a2 ,……, an
(1 ≤ ai≤ 10^9), indicating the number of cakes on i-th plate. It is guaranteed every number appears more than once.
输出描述:
For each test case, each line contains an integer, indicating the first x Chairman Luo should choose to win or -1 if he can’t win.
示例1
输入

3
2
1 1
4
1 2 1 2
5
2 2 2 6 6

输出

1
1
-1

题意
现在给定一个数字N(表示盘子的数量)和一个数组a(代表每个盘子上的蛋糕的数量)。 在一次移动中,玩家选择一个现有的数字x并拿走一个蛋糕数字x的盘子。 下一位玩家选择的x不能少于这次。 当然,第一选择是无限的。 拿最后一个牌的人输了,不能选择x的人将把桌上剩下的所有牌都拿走。 您将被要求告诉罗董事长第一个获胜的x,如果不能获胜则为-1。 由于地位,罗主席将始终采取第一步。
解题思路
这题乍一看像是一道博弈论nim游戏。但其实是要我们输出一个先手必胜的一个x。这里蛋糕是一盘盘拿的,所以不能用异或这样的结论。我们可以进行一个简单的分析。因为我们拿的不能比上一次小。
所以我们先讲其排序从大的一堆开始想。如果我们先拿大堆里的一份。如果这堆有偶数盘。那我们就赢了。输出这一堆值。如果是奇数那我们就看下面一个大堆。直到最小堆。如果最小堆是偶数那么是输的。要输出-1.如果是奇数。那么输出最小堆。

代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N=1000010;
int a[N];
int b[N];
void s(){
     int n;
        scanf("%d", &n);
  for (int i = 1; i <=n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a+1, a + n+1);
  int cnt = 1;
  for (int i = n-1; i >= 0; i--) {
    if (a[i] == a[i + 1])
      cnt++;
    else {
      if (cnt % 2 == 1) {
        if (i != 0)
          printf("%d\n", a[i + 1]);
        else
          printf("-1\n");
       return;
      }
      cnt = 1;
    }
  }
  printf("%d\n", a[1]);
}
int main(){
    
    int T;
    scanf("%d",&T);
    while(T--){
      s();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值