首页 > 其他分享 >AtCoder abc354E

AtCoder abc354E

时间:2024-05-20 10:43:18浏览次数:25  
标签:AtCoder int long 玩家 leq abc354E mp cards

原题链接

Problem Statement

Takahashi and Aoki are playing a game using \(N\) cards. The front side of the \(i\)-th card has \(A_i\) written on it, and the back side has \(B_i\) written on it. Initially, the \(N\) cards are laid out on the table. With Takahashi going first, the two players take turns performing the following operation:

  • Choose a pair of cards from the table such that either the numbers on their front sides are the same or the numbers on their back sides are the same, and remove these two cards from the table. If no such pair of cards exists, the player cannot perform the operation.

The player who is first to be unable to perform the operation loses, and the other player wins. Determine who wins if both players play optimally.

Constraints

  • \(1 \leq N \leq 18\)
  • \(1 \leq A_i, B_i \leq 10^9\)
  • All input values are integers.

问题陈述

高桥和青木正在玩一个使用 \(N\) 张卡片的游戏。 \(i\) 这张牌的正面写着 \(A_i\) ,背面写着 \(B_i\) 。最初, \(N\) 这张牌摆在桌子上。高桥先出,两位玩家轮流进行以下操作:

  • 从桌上选择一对正面数字相同或背面数字相同的牌,然后从桌上拿走这两张牌。如果没有这样的一对牌,玩家就不能进行操作。

最先无法进行操作的玩家输,另一名玩家赢。如果双方都以最佳方式出牌,谁会赢?


需要用到状态压缩技巧的记忆化搜索
看来真比同场 D 题简单多了

点击查看代码
// Created by qyy on 2024/5/18.

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define PII pair<int, int>
#define endl "\n"

const long long inf = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
string st = "Takahashi";
string sa = "Aoki";
int n;
ll a[N], b[N];
int mp[20][20];
bool dp[1<<19];

bool dfs(ll x){
    // 判断先手会不会赢
    // flag = 1 --> Tak, 0 --> Aoki;
    bool ans = false; // 只要后手必输,那么先手就必赢,否则先手必输
    for(int i = 0; i <= 17; i++){
        for(int j = 0; j < i; j++){
            if(mp[i][j])
                if(x&(1LL<<i) && x&(1LL<<j)){
                    ll new_x = x - (1LL << i) - (1LL << j);
                    bool tmp;
                    if(!dp[new_x]){
                        tmp = dfs(new_x);
                        dp[new_x] = tmp;
                    }else tmp = dp[new_x];
                    if(!tmp) ans = true;
                }
        }
    }
    dp[x] = ans;
    return ans;
}
void solve() {
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < i; j++){
            if(a[i] == a[j]) mp[i][j] = mp[j][i] = 1;
            if(b[i] == b[j]) mp[i][j] = mp[j][i] = 1;
        }
    }

    ll t = (1LL << 18) - 1;
    if(dfs(t)) cout << "Takahashi\n";
    else cout << "Aoki\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

标签:AtCoder,int,long,玩家,leq,abc354E,mp,cards
From: https://www.cnblogs.com/9102qyy/p/18201385

相关文章

  • AtCoder Beginner Contest 354
    A-ExponentialPlant(abc354A)题目大意某星球上的植物,初始高\(0\),然后每天依次增长\(1,2,4,8,...\),问哪天就高过身高为\(h\)的高桥。解题思路因为是指数级别长高,枚举一下天数即可,由于\(h\leq10^9\),因此天数不会超过\(32\)天。神奇的代码#include<bits/stdc++.h>u......
  • Atcoder 题目选做(二)
    \(\text{ByDaiRuiChen007}\)*1.[ARC145F]ModuloSumofIncreasingSequencesProblemLink给定\(n,m,p\),对于所有\(r\in[0,p)\)求有多少长度为\(n\),值域\([0,m]\)的单调不降序列数组在\(\bmod\p\)意义下的序列和为\(r\)。数据范围:\(n,m\le10^6,p\le500\)......
  • Atcoder 题目选做(一)
    \(\text{ByDaiRuiChen007}\)1.[ARC080F]PrimeFlipProblemLink数轴上有\(n\)个点\(a_1\sima_n\)的颜色是黑色的,其余颜色为白色。每次操作可以选连续\(p\)个位置反色,其中\(p\)必须是奇素数。求全部位置染白的最小操作次数。数据范围:\(n\le100,a_i\le10^7\)......
  • AtCoder Regular Contest 177
    AtCoderRegularContest177A-Exchange问题陈述判断\(n\)个价格分别为\(x_i\)的商品,问能否通过有限数量的\(1\)元,\(5\)元,\(10\)元,\(50\)元,\(100\)元,\(500\)元,购买。思路贪心。每个商品从\(500\)元开始,能用就尽量用。如果中间某个商品无法被满足,则无解,反......
  • AtCoder Beginner Contest 351 E - Jump Distance Sum
    题目链接Hint1:只能斜着走,容易想到黑白棋盘,\((x+y)\%2==0\)位于一个团,\((x+y)\%2==1\)位于另一个团,分别求和。Hint2:\(dist=max(|x1-x2|,|y1-y2|)\),这是切比雪夫距离,将坐标系倾斜\(45^{\circ}\),改为曼哈顿距离\(dist=|x1-x2|+|y1-y2|\),即\(X=x+y,Y=x-y\),这样就可以将横纵坐标......
  • AtCoder Beginner Contest 352 E - Clique Connect
    题目链接不需要将所有边都建立出来,根据\(Kruskal\)最小生成树的贪心策略,相同权值的边不需要形成团,形成一个链就行,对结果没有影响。时间复杂度\(O(mlogm)[m=\sum_{i=1}^{n}k_{i}]\)。#pragmaGCCoptimize(2)#pragmaGCCoptimize(3)#include<bits/stdc++.h>//#defineint......
  • AtCoder Beginner Contest 353
    AtCoderBeginnerContest353abc353_c题意:定义\(F(x,y)\)为\((x+y)mod10^8\)的值,求\(\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^Nf(A_i,A_j).\)思路:对于\(\displaystyle\sum_{i=1}^{N-1}\sum_{j=i+1}^N\f(A_i,A_j).\)来说,每个\(A_i\)的次数都是\(n-1\)次,所以如果没有\(m......
  • Atcoder ABC 353 全题解
    最近看的人好少……都快不想写了……你们的支持就是我创作的最大动力!AB%你CDE题意:有一个一个一个函数,把函数两两配对式求值,求这些值最后的总和C考虑将所有的和减去$10^8$出现的次数。将整个数组排序,然后进行二分,求第一个与这个数的和$\ge10^8$的位置,然后与这个数......
  • Atcoder Beginner Contest 353
    AtcoderBeginnerContest353A问题陈述有\(N\)幢楼房排列成一排。左起的\(i\)th楼高\(H_i\)。请判断是否有比左起第一座高的建筑物。如果存在这样的建筑物,请找出从左边起最左边的建筑物的位置。思路签到题。代码#include<bits/stdc++.h>usingnamespacestd;int......
  • AtCoder Beginner Contest 353
    AtCoderBeginnerContest353A-Buildings求第一个\(h_i\)大于\(h_1\)的位置。模拟。点击查看代码#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intn,h[103];signedmain(){ cin>>n;cin>>h[1]; for(inti=2;i<=n;i++){ cin&......