Codeforces C. Even Number Addicts

题意:给定一个数组,要求Alice和Bob两个人一前一后的选择数并删除,Alice能否选出偶数总和的数以获得胜利。

思路:Alice选偶数时,Bob总是也选偶数留下奇数使得Alice的数字改变奇偶性,Alice选奇数时,Bob总是也选奇数使得Alice选偶数不改变她的奇偶性。

于是我们把奇偶分开来看,对于奇数的数量,做分类讨论

cnt1%4==0,Alice胜

cnt1%4==1,此时cnt2&1,Bob胜,因为剩下一个奇数Alice选,反之Alice胜

cnt1%4==2,此时Bob必胜,因为最后两个奇数一人一个

cnt1%4==3,Alice必胜,因为Alice最后拿两个奇数

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n;
int a[N];
bool dp[110][110];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

inline void init() {}

void slove()
{
    cin >> n;
    pre(i, 1, n) cin >> a[i];
    int cnt1 = 0, cnt2 = 0;
    pre(i, 1, n)
    {
        if (a[i] & 1) cnt1++;
        else cnt2++;
    }

    if (cnt1 % 4 == 0)puts("Alice");
    else if (cnt1 % 4 == 1)
    {
        if (n & 1)puts("Bob");
        else puts("Alice");
    }
    else if (cnt1 % 4 == 2)
        puts("Bob");
    else if (cnt1 % 4 == 3)
        puts("Alice");

    return;
}

signed main()
{
    //IOS;
    int _ = 1;
    si(_);
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}
/*
1
3 15
1 9
1 9
1 8

*/

dp做法

显然,该题的胜负只与奇数和偶数的数量有关,因此,我们做关于奇数和偶数的dp

用odd,even两个二维数组保存当奇数偶数分别为i,j时,先手能否一定取到奇数或者偶数。

哎,这时候就有人要问了,不是只要求能否取到偶数吗,为什么还要有奇数呢?

我们来看看推导过程,首先,当我们从ij向之前的状态转移时,于是我们要的答案变成cntodd&1==1时,even[i][j-1],even[i-1][j]均为false,cntodd&1==0时odd[i-1][j],odd[i][j-1]均为false,所以需要odd辅助,同时,我们也需要维护odd数组

还有需要注意的边界问题(i==0||j==0)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 1000010, M = 2 * N, B = N, MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n;
int a[N];
bool even[110][110];
bool odd[110][110];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

inline void init() {
    /*for (int i = 1; i <= 100; i++)
    {
        even[i][0] = (i + 1) / 2 & 1 ^ 1;
        even[0][i] = 1;
        odd[i][0] = (i + 1) / 2 & 1;
        odd[0][i] = 0;
    }*/
    even[0][1] = true, even[1][0] = false;
    odd[1][0] = true, odd[0][1] = false;

    for(int cnt=2;cnt<=100;cnt++)
        for (int i = 0; i <= cnt; i++) {
            int j = cnt - i;
            if (i & 1)
            {
                if(i) even[i][j] |= !even[i - 1][j];
                if(j) even[i][j] |= !even[i][j - 1];
                if(i) odd[i][j] |= !odd[i - 1][j];
                if(j) odd[i][j] |= !odd[i][j - 1];
            }
            else
            {
                if(i) even[i][j] |= !odd[i - 1][j];
                if(j) even[i][j] |= !odd[i][j - 1];
                if(i) odd[i][j] |= !even[i - 1][j];
                if(j) odd[i][j] |= !even[i][j - 1];
            }
        }
}

void slove()
{
    cin >> n;
    pre(i, 1, n) cin >> a[i];
    int cnt1 = 0, cnt2 = 0;
    pre(i, 1, n)
    {
        if (a[i] & 1) cnt1++;
        else cnt2++;
    }

    if (even[cnt1][cnt2])puts("Alice");
    else puts("Bob");
}

signed main()
{
    //IOS;
    int _ = 1;
    si(_);
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}
/*
1
3 15
1 9
1 9
1 8

*/

这是1500的博弈题?,这就叫惊喜!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 并发 C++11 C++17 I encountered the concept of multithreaded code while working at my first job after I left college. We were writing a data processing application that had to populate a database with incoming data records. There was a lot of data, but each record was independent and required a reasonable amount of processing before it could be inserted into the database. To take full advantage of the power of our 10-CPU UltraSPARC, we ran the code in multiple threads, each thread processing its own set of incoming records. We wrote the code in C++, using POSIX threads, and made a fair number of mistakes—multithreading was new to all of us—but we got there in the end. It was also while working on this project that I first became aware of the C++ Standards Committee and the freshly published C++ Standard. I have had a keen interest in multithreading and concurrency ever since. Where others saw it as difficult, complex, and a source of problems, I saw it as a powerful tool that could enable your code to take advantage of the available hardware to run faster. Later on I would learn how it could be used to improve the responsiveness and performance of applications even on single-core hardware, by using multiple threads to hide the latency of time-consuming operations such as I/O. I also learned how it worked at the OS level and how Intel CPUs handled task switching. Meanwhile, my interest in C++ brought me in contact with the ACCU and then the C++ Standards panel at BSI, as well as Boost. I followed the initial development of the Boost Thread Library with interest, and when it was abandoned by the original developer, I jumped at the chance to get involved. I have been the primary developer and maintainer of the Boost Thread Library ever since.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值