Codeforces Round 908 (Div. 2)

本文介绍了三道Codeforces竞赛题目,涉及A-SecretSport中的字符串操作,B-TwoOutofThree中出现次数判断和分配,以及C-AnonymousInformant中的数组反转规律。展示了C++代码实现和解题思路。
摘要由CSDN通过智能技术生成

Codeforces Round 908 (Div. 2)

请添加图片描述

A - Secret Sport

赢下最后一局才会胜利结束游戏,最后一局的胜者是赢家

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+5;
void solve(){
    int n;cin>>n;
    string s;cin>>s;
    cout<<s[n-1]<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
    while(t--)solve();
}

B - Two Out of Three

我们只关心出现次数大于等于2的种类
当种类小于2时必定没有解
超过2时一个种类赋予1,2类型,一个种类赋予1,3类型,其他值随便赋1,2,3即可

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+5;
void solve(){
    int n;cin>>n;
    map<int,int>m;
    vector<int>v(n);
    set<int>s;
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        m[v[i]]++;
        if(m[v[i]]>=2)s.insert(v[i]);
    }
    if(s.size()<2)cout<<-1<<endl;
    else
    {
        int x=0,y=0;
        for(auto k : s)
        {
            if(!x)x=k;
            else
            {
                y=k;
                break;
            }
        }
        int f=0;
        int ff=0;
        for(int i=0;i<n;i++)
        {
            if(x==v[i]) { if(!f)cout << 1 << ' ';else cout<<2<<' '; f++; }
            else if(y==v[i]) { if(!ff)cout << 1 << ' ';else cout<<3<<' '; ff++; }
            else cout<<1<<' ';
        }
        cout<<endl;
    }

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

C - Anonymous Informant

找规律。 固定点ax=x左移x位后一定在第ax位, 那么我们可以猜测论只有第n位才能反向操作
ai要回到第个位置,必须满足(ai+i)%n=ai,那么只有当i==n时满足。
我们对最后一位进行操作,要回到原来的位置,那么对数组的影响是,数组会变成[a-an+1]+[1+n-an]
但是,当遇到最后一位an>n时,一定不合法,因为,不满足ax=x固定点。
所以我们一直操作最后一位,持续至多次,直到碰到循环或者an>n停止。我们设cnt为我们现在反向操作的最后一个位置,我们可以一直处理cnt=(cnt-a[cnt]+n)%n就能做到最后一位的转移。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+5;
int a[N],b[N];
void solve(){
    int n,k;cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i], b[i]=0;
    int cnt = n;
    while(k--)
    {
        if(a[cnt]>n){ cout<<"NO"<<endl;return ; }
        cnt=(cnt-a[cnt]+n)%n;
        if(!cnt)cnt = n;
        if(b[cnt]){ cout<<"YES"<<endl;return ; }
        b[cnt]=1;
    }
    cout<<"YES"<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    cin>>t;
    while(t--)solve();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值