2022.3.28

Codeforces Round #779 (Div. 2)

A. Marin and Photoshoot

把所有0的位置找出来放到数组里,如果相邻的0距离为1的话,说明要往中间插2个1,如果距离为2的话只需要插1个1。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        int cnt = 0,ans=0;
        for (int i = 0; i < s.size();i++)
        {
            if(s[i]=='0')
                a[cnt++] = i;
        }
        for (int i = 0; i < cnt-1;i++)
        {
            if(abs(a[i]-a[i+1])==1)
            {
                ans+=2;
            }
            else if(abs(a[i]-a[i+1])==2)
            {
                ans ++;
            }
        }
            cout << ans << '\n';
    }
    return 0;
}

B. Marin and Anti-coprime Permutation

为了满足条件,需要把奇数的位置放偶数,偶数的位置放奇数,这样乘起来每个位置一定是偶数,那么最大公约数必不可能为1。放位置的就是排列组合,把n/2,即偶数和奇数的位置的个数,然后分别全排列最后在相乘。当n为奇数一定不满足,因为奇数和偶数的位置个数不一样。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8,mod=998244353;
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin >> n;
        if(n&1)
            cout << 0 << '\n';
        else
        {
            int k = n / 2;
            ll ans = 1;
            for (int i = 1; i <= k;i++)
            {
                ans = ans * i % mod;
            }
            ans = ans * ans % mod;
            cout << ans << '\n';
        }
    }
    return 0;
}

C. Shinju and the Lost Permutation

举了几个输入发现要连成一条的样子,其他都还好想但是最重要的就是结果里面只能有1个1,因为1就表示最大的数排在前面,不可能一次循环有两次最大的数都在前面的情况,当时推的差不多了就是把a[1]推成了a[n]导致错误

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int a[N];
int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        a[n + 1] = a[1];
        int f = 0,cnt=0;
        for (int i = 1; i <= n;i++)
        {
            if(a[i+1]>a[i]+1)
            {
                f = 1;
                break;
            }
            if(a[i]==1)
            {
                cnt++;
            }
        }
        if (!f&&cnt==1)
            cout << "yes" << '\n';
        else
            cout << "no" << '\n';
    }
    return 0;
}
posted @ 2022-03-28 01:51  menitrust  阅读(46)  评论(0编辑  收藏  举报