Playing games —— 基本dp+滚动数组

链接:https://www.nowcoder.com/acm/contest/146/H
来源:牛客网

题目描述
Niuniu likes playing games. He has n piles of stones. The i-th pile has ai stones. He wants to play with his good friend, UinUin. Niuniu can choose some piles out of the n piles. They will play with the chosen piles of stones. UinUin takes the first move. They take turns removing at least one stone from one chosen pile. The player who removes the last stone from the chosen piles wins the game. Niuniu wants to choose the maximum number of piles so that he can make sure he wins the game. Can you help Niuniu choose the piles?

输入描述:
The first line contains one integer n (1 ≤ n ≤ 500000), which means the number of piles.
The second line describes the piles, containing n non-negative integers, a1 a2 … an, separated by a space. The integers are less than or equal to 500000.

输出描述:
Print a single line with one number, which is the maximum number of piles Niuniu can choose to make sure he wins. If Niuniu cannot always win whatever piles he chooses, print 0.
示例1
输入
复制
8
1 9 2 6 0 8 1 7
输出
复制
7
说明
Niuniu can choose the piles {1,9,6,0,8,1,7} to make sure he wins the game.

赛后看了别人的题解才会,刚开始不知道被谁榜带偏了,所有人都在做这道题,呵呵,没多少做出来的。
我们可以知道,若一堆数的异或和为0,先手一定会打破这种平衡,后手则可以修补这种平衡,所以在异或为0的情况下,后手胜,之后就是通过dp来找出最少需要去掉几个数才能使异或和为0;

#include<bits/stdc++.h>
using namespace std;
#define maxn 600005
#define inf 0x3f3f3f3f
int dp[2][maxn],vis[maxn];
vector<int>vec;
int main()
{
    int n;
    scanf("%d",&n);
    int a;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        if(!vis[a])
            vec.push_back(a);
        vis[a]=1;
        dp[0][a]=1;
        sum^=a;
    }
    if(sum==0)
        printf("%d\n",n);
    else if(vis[sum])
        printf("%d\n",n-1);
    else
    {
        int flag=0;
        for(int i=0;i<vec.size();i++)
            if(vis[sum^vec[i]])
            {
                flag=1;
                break;
            }
        if(flag)
            printf("%d\n",n-2);
        else
        {
            int pos=0;
            memset(dp,inf,sizeof(dp));
            dp[0][0]=0;
            for(int i=0;i<vec.size();i++)
            {
                pos^=1;
                for(int j=0;j<=maxn;j++)
                {
                    if(dp[pos^1][j]>=inf)
                        continue;
                    dp[pos][j]=min(dp[pos][j],dp[pos^1][j]);
                    dp[pos][j^vec[i]]=min(dp[pos^1][j]+1,dp[pos][j^vec[i]]);
                }
            }
            printf("%d\n",max(n-dp[pos][sum],0));
        }
    }
    return 0;
}
阅读更多

Roll Playing Games

10-17

DescriptionnnPhil Kropotnik is a game maker, and one common problem he runs into is determining the set of dice to use in a game. In many current games, non-traditional dice are often required, that is, dice with more or fewer sides than the traditional 6-sided cube. Typically, Phil will pick random values for all but the last die, then try to determine specific values to put on the last die so that certain sums can be rolled with certain probabilities (actually, instead of dealing with probabilities, Phil just deals with the total number of different ways a given sum can be obtained by rolling all the dice). Currently he makes this determination by hand, but needless to say he would love to see this process automated. That is your task. nnFor example, suppose Phil starts with a 4-sided die with face values 1, 10, 15, and 20 and he wishes to determine how to label a 5-sided die so that there are a) 3 ways to obtain a sum of 2, b) 1 way to obtain a sum of 3, c) 3 ways to obtain 11, d) 4 ways to obtain 16, and e)1 way to obtain 26. To get these results he should label the faces of his 5-sided die with the values 1, 1, 1, 2, and 6. (For instance, the sum 16 may be obtained as 10 +6 or as 15 +1, with three different "1" faces to choose from on the second die, for a total of 4 different ways.) nInputnnInput will consist of multiple input sets. Each input set will start with a single line containing an integer n indicating the number of dice that are already specified. Each of the next n lines describes one of these dice. Each of these lines will start with an integer f (indicating the number of faces on the die) followed by f integers indicating the value of each face. The last line of each problem instance will have the form nnr m v1 c1 v2 c2 v3 c3 ... vm cm nnwhere r is the number of faces required on the unspecified die, m is the number of sums of interest, v1, ... ,vm are these sums, and c1, ... ,cm are the counts of the desired number of different ways in which to achieve each of the respective sums. nnInput values will satisfy the following constraints: 1 <= n <= 20, 3 <= f <= 20, 1 <= m <= 10, and 4 <= r <= 6. Values on the faces of all dice, both the specified ones and the unknown die, will be integers in the range 1 ... 50, and values for the vi's and ci’s are all non-negative and are strictly less than the maximum value of a 32-bit signed integer. nnThe last input set is followed by a line containing a single 0; it should not be processed. nOutputnnFor each input set, output a single line containing either the phrase "Final die face values are" followed by the r face values in non-descending order, or the phrase "Impossible" if no die can be found meeting the specifications of the problem. If there are multiple dice which will solve the problem, choose the one whose lowest face value is the smallest; if there is still a tie, choose the one whose second-lowest face value is smallest, etc.nSample Inputnn1n4 1 10 15 20n5 5 2 3 3 1 11 3 16 4 26 1n1n6 1 2 3 4 5 6n6 3 7 6 2 1 13 1n4n6 1 2 3 4 5 6n4 1 2 2 3n3 3 7 9n8 1 4 5 9 23 24 30 38n4 4 48 57 51 37 56 31 63 11n0nSample OutputnnFinal die face values are 1 1 1 2 6nImpossiblenFinal die face values are 3 7 9 9

没有更多推荐了,返回首页