IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> Codeforces C. Even Number Addicts -> 正文阅读

[C++知识库]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的博弈题?,这就叫惊喜!

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:22:12  更:2022-10-08 20:25:01 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 4:47:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码