Codeforces Round #658 (Div. 2) A - C 超详解

A. Common Subsequence

题意:找出一个最短序列,里面的元素在AB数组里都出现过,若没有则输出NO

思路:Map一下A数组里的东西,B遇到一个就输出就行。B搞完都没有的话就说明没有了

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e4+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll Map[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        mem(Map,0);
        ll n = read(), m = read();
       // cout<<n<<' '<<m<<"wa"<<endl;
        rep(i,1,n)
        {
            ll x = read();
            Map[x] = 1;
        }
        int flag = 0;
        rep(i,1,m)
        {
            ll x = read();
            if(Map[x]&&!flag)
            {
                flag = 1;
                cout<<"YES"<<endl;
                cout<<1<<' '<<x<<'\n';
            }
        }
        if(!flag) cout<<"NO"<<endl;
    }
    return 0;
}

B. Sequential Nim

题意:博弈,每次可以在序列第一个不为零的元素中挑出若干数,最后没数取的那个人输。判断谁赢

写着NIM博弈,其实是个规律题。我们先看5 4这个样例。毫无疑问,最后谁先拿到4,谁就肯定赢了(直接拿完4)。那么,我既然是先手只能从5开始拿,我肯定不会一下子拿完,因为拿完的话就把4拱手让给对面了嘛(前面说了拿到4必赢)。那我5就拿的只剩下1,恶心一下对面让对面不得不只能拿完这最后一个,这样4的掌控权又到了我手里了。同理3 5 4这个样例,如果我先手,我总是可以掌控每个位置——3拿走2剩下1,对面没办法只能拿1。5又是我拿走4,剩下1恶心对面,最后一波拿走4 。 所以我们发现,如果全部都是大于1的数,我先手可以有所有数的掌控权。那我们看看有1的情况, 1 1 1,这个情况下大家都不能制衡对手,只能轮着来,奇数个1就掌控权回来先手手里,偶数就白给对面(参考题目里的第二个样例)。一旦拿到大于1的掌控权的一方,不管后面有多少1,我都可以使得最后掌控权在自己手里(这一点其实很明显自己想想)

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read();
        ll win = 1;
        int flag = 0;
        rep(i,1,n)
        {
            ll x = read();
            if(x>1) flag = 1;
            else if(flag) continue;
            else if(i!=n) win = !win , flag = 0;
        }
        if(win) cout<<"First"<<endl;
        else cout<<"Second"<<endl;
    }
    return 0;
}

C1. Prefix Flip (Easy Version)

题意:给一个操作,调转前k位的二进制位,然后翻转,得到新串。问3n步内怎么把字符串a转向b

破题口:3n步以内。说明总能知道一种走法可以无脑解的。我们看每次都会调转字符串,那就每次都看开头第一个和当前要处理的尾部的倒数第i个。拿s当原串,s1当目标串,若s[1] != s1[n](相异),说明1这个位置取反再颠倒后就是我的目标串第n位。如果相同的话就给第一位取反调转(一次操作)再执行上述。所以只需要每次对开头和结尾对比操作就行了 。这样撑破天都不会超过2n次。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll ans[maxn];
char s[maxn];
char s1[maxn];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n = read();
        scanf("%s",s +1);
        scanf("%s",s1+1);
        ll k = 0;
        ll cur = n;
        while(cur>1)
        {
            char to = s1[cur];
            if(s[1] - 48 == !(to-48) )
            {
                ans[++k] = cur;
                for(int i=1; i<=cur;i++)
                s[i] = !(s[i] - 48) + 48;
                for(int i=1, j=cur; i<=j; i++, j--)
                swap(s[i],s[j]);
                cur--;
                continue;
            }
            ans[++k] = 1;
            s[1] = !(s[1] - 48) + 48;
            ans[++k] = cur;
            for(int i=1; i<=cur;i++)
            s[i] = !(s[i] - 48) + 48;
            for(int i=1, j=cur; i<=j; i++, j--)
            swap(s[i],s[j]);
            cur--;
        }
        if(s[1]!=s1[1]) ans[++k] = 1, s[1] = !(s[1]-48) + 48;
        cout<<k<<' ';
        rep(i,1,k) cout<<ans[i]<<' ';
        cout<<'\n';
    }
    return 0;
}

写题太慢后面题没看了。

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107502212