Codeforces Round #604 (Div. 2) 练习A,B题解

A题

链接

  • 思路分析:
    因为只需要做到相邻的不相同,利用三个不同的字母是肯定可以实现的,
    所以直接先将所有的问号进行替换,比如比前一个大1,如果与后面的冲突,则再加一
  • 代码(写的很烂):
#include <bits/stdc++.h>
using namespace std;
int check( string a)
{
    for (int i = 0; i < a.length(); ++i)
    {
        if (i==0)
        {
            if (a[i]==a[i+1])
            {
                return 0;
            }
        }
        else if (a[i]==a[i-1])
        {
            return 0;
        }
        else{
            if (a[i-1]==a[i]||a[i+1]==a[i])
            {
                return 0;
            }
        }
    }
    return 1;
}

int main(int argc, char const *argv[])
{
    int t;
    cin>>t;
    string a;

    while(t--)
        {
            cin>>a;
            for (int i = 0; i < a.length(); ++i)
            {
                if (i==0)
                {
                    if (a[i]=='?')
                    {
                        a[i]=((a[i+1]+1)%97)%3 + 97;
                        if (a[i]==a[i+1])
                        {
                            a[i] = ((a[i]+1)%97)%3+97;
                        }
                    }
                }
                else if (i==a.length()-1 )
                {
                    if (a[i]=='?')
                    {
                        a[i]=((a[i-1]+1)%97)%3 + 97;
                    }

                }
                else
                {
                    if (a[i]=='?')
                    {
                        a[i]=((a[i-1]+1)%97)%3 +97;
                        if (a[i] == a[i+1])
                        {
                            a[i]=((a[i]+1)%97)%3 +97;
                        }
                    }
            }
        }
        if (check(a))
        {
            cout<<a<<endl;
        }
        else
            cout<<"-1"<<endl;
    }
    return 0;
}

B题


链接

  • 思路分析:
    如果有1到m的一个排列,那么肯定在1带m的位置的最大值和最小值的差是m-1
    所以可以利用一个数组将pos信息存起来,从小到大遍历就可以
  • 代码:
#include <bits/stdc++.h>

using namespace std;

int  pos[200001];

int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    int t;
    int n;
    cin>>t;
    int k;
    while(t--)
    {
        cin>>n;
        for (int i = 0; i < n; ++i)
        {
            cin>>k;
            pos[k-1] = i;
        }
        int maxpos = 0;
        int minpos = n-1;
        for (int i = 0; i < n; ++i)
        {
            minpos = min(minpos, pos[i]);
            maxpos = max(maxpos, pos[i]);
            cout<<(maxpos-minpos==i?1:0);
        }
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Crossea/p/11997686.html

时间: 2024-08-29 05:36:08

Codeforces Round #604 (Div. 2) 练习A,B题解的相关文章

Codeforces Round #604 (Div. 2)

https://codeforces.com/contest/1265 这场的2E是1C的不带checkpoints的版本. B - Beautiful Numbers 题意:给一个数组是一个[1,n]的permutation.对每个m∈[1,n]问[1,m]是否连续存在在这个数组中. 题解: 首先,[1,1]一定存在. 然后向指定方向扩展到2,若经过2以外的数,把2标记为不存在. 3已经被扩展,或3在区间左右?是:否. 所以每次就问新的数是不是在已有区间中或者左右,是则包含,否则扩展到有为止.

Codeforces Round #604 (Div. 2) A. Beautiful String

链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa&

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s1,s2,-,sn is beautiful if |si?si+1|=1 for all 1≤i≤n?1. Trans

Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest

链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved pi problems. Since

Codeforces Round #604 (Div. 2) D、E、F题解

Beautiful Sequence \[ Time Limit: 1000 ms\quad Memory Limit: 256 MB \] 首先我们可以考虑到 \(0\) 只能 和 \(1\) 放在一起.\(3\) 只能和 \(2\) 放在一起,那么我们想办法先把 \(0\) 和 \(3\) 凑出来,最后就剩下 \(1\) 和 \(2\) 了,我们只要把他们放在一起就可以了. 所以我们可以贪心考虑三个 \(string\),分别长成 \(0101...0101\).\(2323...2323\

Codeforces Round #604 (Div. 2)D(构造)

构造,枚举起点,如果一个序列成立,那么将它reverse依然成立,所以两个方向(从小到大或从大到小)没有区别,选定一个方向进行探测,直到探测不到以后回头,只要所给数据能成立,那么能探测进去就能探测出来,否则就不能构造. 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int num[7]; 5 int sum; 6 int main(){ 7 ios::sync_with_std

Codeforces Round #604 (Div. 2)(A-E)

A. Beautiful String 题意:把'?'换成'a' or 'b' or 'c'使得相邻的两个字符不相同. 暴力枚举每个'?'前后. #include <bits/stdc++.h> using namespace std; const int MAXN=1e5+10; string s; int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ cin>>s; bool ok=t

Codeforces Round #501 (Div. 3) F. Bracket Substring

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #536 (Div. 2)

目录 Codeforces Round #536 (Div. 2) A 题目大意 题解 卡点 C++ Code: B 题目大意 题解 卡点 C++ Code: C 题目大意 题解 卡点 C++ Code: D 题目大意 题解 卡点 C++ Code: E 题目大意 题解 卡点 C++ Code: F 题目大意 题解 卡点 C++ Code: Codeforces Round #536 (Div. 2) A 题目大意 给你一个\(n\times n(n\leqslant500)\)的矩阵,只包含.