Codeforces Round #658 (Div. 2)部分题解

A - Common Subsequence

题解:
因为a数组和b数组里面的元素值都小于1000,所以我们可以开一个数组来存对于每个元素出现过的情况,最后遍历一遍1-1000,检查是否有同时出现的值即可

/*Keep on going Never give up*/
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
const int maxn = 1010;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 1e9+7;
using namespace std;

bool a[maxn];
bool b[maxn];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        int x;
        memset(a,false,sizeof a);
        memset(b,false,sizeof b);
        for(int i=0;i<n;i++){
            cin>>x;
            a[x]=true;
        }
        for(int i=0;i<m;i++){
            cin>>x;
            b[x]=true;
        }
        bool flag=false;
        for(int i=0;i<1001;i++){
            if(a[i]&&b[i]){
                flag=true;
                cout<<"YES"<<endl;
                cout<<"1 "<<i<<endl;
                break;
            }
        }
        if(!flag) cout<<"NO"<<endl;
    }
    return 0;
}

B - Sequential Nim

题意:每次只能取序列数最低的那一堆石子的某些,看谁先取完。
题解:
我们可以发现,如果序列数为1 1 1 1 1 因为先后取得顺序已经决定,所以这种得胜负并不是一个人可以左右得了得。
但是如果我们看这串序列:3 1 1和3 1,
作为第一个人取数如果是第一种情况 那么这个人可以直接取完第一堆所有的石子,
如果是第二种情况,那么他在第一堆就取2个,还是他赢。
结论:对于序列不全为1的序列,谁先取到个数不为1的那堆石子,谁就获胜

/*Keep on going Never give up*/
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
const int maxn = 1e5+10;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 1e9+7;
using namespace std;
int a[maxn];
 
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        bool flag=false;
        int cnt=0;
        for(int i=0;i<n;i++){
            if(a[i]!=1){
                flag=true;
                break;
            }
            else cnt++;
        }
        if(!flag){
            if(cnt%2==1) cout<<"First"<<endl;
            else cout<<"Second"<<endl;
        }
        else{
            if(cnt%2==0) cout<<"First"<<endl;
            else cout<<"Second"<<endl;
        }
    }
    return 0;
}

C1 - Prefix Flip (Easy Version)

题解:因为我比较菜嘛,所以一开始就先做的C1,字符串长度只有1000,那就直接暴力模拟呗,从后往前进行反转。

/*Keep on going Never give up*/
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
const int maxn = 1e5+10;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 1e9+7;
using namespace std;
string s,s1;
string temp;
 
 
 
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        vector<int> v;
        cin>>n;
        cin>>s>>s1;
        for(int i=n-1;i>=0;i--){
            if(s[i]!=s1[i]){
                if(s[0]==s1[i]){
                    v.push_back(1);
                    s[0]=s[i];
                }
                v.push_back(i+1);
                temp=s.substr(0,i+1);
                for(int j=i;j>=0;j--){
                    if(temp[i-j]=='0') s[j]='1';
                    else s[j]='0';
                }
            }
        }
        //cout<<s<<endl;
        cout<<v.size()<<" ";
        for(auto it : v){
            printf("%d ",it);
        }
        printf("\n");
    }
    return 0;
}

C2 - Prefix Flip (Hard Version)

昨天晚上转了转去,实在是把自己给转晕了,今天在网上看了一个很好的题解。
题解:
1.首先,你用n以内的操作数,把s全转成相同数字的字符串
2.第二,你再用n以内的操作数,从后往前遍历字符串,对每个s与s1不同的地方,进行一次旋转。

因为全是1111111111或者全是000000000,所以不需要考虑特别多。
这个方法真的是很给力了。。。

const int maxn = 1e5+10;
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 1e9+7;
using namespace std;
string s,s1;
char temp;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        cin>>s>>s1;
        vector<int> v;
        for(int i=1;i<n;i++){
            if(s[i]!=s[i-1]) v.push_back(i);
        }
        temp=s[n-1];
        for(int i=n-1;i>=0;i--){
            if(temp!=s1[i]){
                v.push_back(i+1);
                if(temp=='1') temp='0';
                else temp='1';
            }
        }
        cout<<v.size()<<" ";
        for(auto it:v) printf("%d ",it);
        printf("\n");
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/xxxxxiao123/article/details/107505713