Codeforces Round #738 (Div. 2)

Posted Jozky86

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #738 (Div. 2)相关的知识,希望对你有一定的参考价值。

Codeforces Round #738 (Div. 2)

题号题目知识点
AMocha and Math
BMocha and Red and Blue
CMocha and Hiking
D1Mocha and Diana (Easy Version)
D2Mocha and Diana (Hard Version)
EMocha and Stars

A

题解:

可以任意选区间,可以操作多次,也就是任何数都可以进行&操作,所以答案就是所有&的结果

代码:

// Problem: A. Mocha and Math
// Contest: Codeforces - Codeforces Round #738 (Div. 2)
// URL: https://codeforces.com/contest/1559/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-16 19:15:42
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename T> inline void read(T& x)
{
    T f= 1;
    x= 0;
    char ch= getchar();
    while (0 == isdigit(ch)) {
        if (ch == '-')
            f= -1;
        ch= getchar();
    }
    while (0 != isdigit(ch))
        x= (x << 1) + (x << 3) + ch - '0', ch= getchar();
    x*= f;
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{
    //rd_test();
    int t;
    read(t);
    while (t--) {
        int n;
        read(n);
        ll ans= 0;
        for (int i= 1; i <= n; i++) {
            int x;
            read(x);
            if (i == 1)
                ans= x;
            else
                ans= ans & x;
        }
        printf("%d\\n", ans);
    }
    return 0;
    //Time_test();
}

B

题意:

长度为n的字符串,由BR?三种符号组成,现在要求你将?填上B/R,使得相同符号相邻的情况最少

题解:

找到第一个非?的字符,然后从该点开始向前向后一次填充,每次填充?时填与其相邻的相反元素
这样保证最优

代码:

// Problem: B. Mocha and Red and Blue
// Contest: Codeforces - Codeforces Round #738 (Div. 2)
// URL: https://codeforces.com/contest/1559/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-16 19:24:33
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename T> inline void read(T& x)
{
    T f= 1;
    x= 0;
    char ch= getchar();
    while (0 == isdigit(ch)) {
        if (ch == '-')
            f= -1;
        ch= getchar();
    }
    while (0 != isdigit(ch))
        x= (x << 1) + (x << 3) + ch - '0', ch= getchar();
    x*= f;
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{
    //rd_test();
    int t;
    read(t);
    while (t--) {
        int n;
        read(n);
        string s;
        cin >> s;
        int pos= n + 1;
        for (int i= 0; i < s.length(); i++) {
            if (s[i] != '?') {
                pos= i;
                break;
            }
        }
        for (int i= pos - 1; i >= 0; i--) {
            if (s[i] == '?') {
                if (s[i + 1] == 'R')
                    s[i]= 'B';
                else
                    s[i]= 'R';
            }
        }
        for (int i= pos + 1; i < s.length(); i++) {
            if (s[i] == '?') {
                if (s[i - 1] == 'R')
                    s[i]= 'B';
                else
                    s[i]= 'R';
            }
        }
        cout << s << endl;
    }

    //Time_test();
}

C

题意:

有n+1个点,其中1~n个点,1有条到2的边,2有条到3的边…n-1有条到n的边
1到n这些点与n的连边关系题目给出,问能否将所有点全走一遍(每个点最多只能走一次)

题解:

不难发现,从1出发,是可以到n的(经过了1到n所有点),现在还剩n+1
如果点n+1能到1或者n能到n+1,那就可以顺利到点n+1
或者从 a i a_{i} ai可以到n+1,从n+1也可以到 a i + 1 a_{i+1} ai+1,这样也行

代码:

// Problem: C. Mocha and Hiking
// Contest: Codeforces - Codeforces Round #738 (Div. 2)
// URL: https://codeforces.com/contest/1559/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-16 19:47:58
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename T> inline void read(T& x)
{
    T f= 1;
    x= 0;
    char ch= getchar();
    while (0 == isdigit(ch)) {
        if (ch == '-')
            f= -1;
        ch= getchar();
    }
    while (0 != isdigit(ch))
        x= (x << 1) + (x << 3) + ch - '0', ch= getchar();
    x*= f;
}
template <typename T> inline void write(T x)
{
    if (x < 0) {
        x= ~(x - 1);
        putchar('-');
    }
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCAL
    startTime= clock();
    freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCAL
    endTime= clock();
    printf("\\nRun Time:%lfs\\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn= 2e4 + 9;
vector<int> vec[maxn];
int a[maxn];
int main()
{
    //rd_test();
    int t;
    read(t);
    while (t--) {
        int n;
        read(n);
        for (int i= 1; i < n; i++)
            vec[i].push_back(i + 1);
        for (int i= 1; i <= n; i++) {
            cin >> a[i];
        }
        if (a[1] == 1) {
            printf("%d", n + 1);
            for (int i= 1; i <= n; i++) {
                printf(" %d", i);
            }
            printf("\\n");
            continue;
        }
        else if (a[n] == 0) {
            printf("%d", 1);
            for (int i= 2; i <= n + 1; i++) {
                printf(" %d", i);
            }
            printf("\\n");
            continue;
        }
        bool f= 0;
        for (int i= 1; i < n; i++) {
            if (a[i] == 0 && a[i + 1] == 1) {
                for (int j= 1; j <= n; j++) {
                    printf("%d ", j);
                    if (j == i)
                        printf("%d ", n + 1);
                }
                printf("\\n");
                break;
            }
        }
        if (f == 0)
            continue;
        else
            printf("-1\\n");
    }

    //Time_test();
}

D1

题意:

有两个森林,现在要求你在第一个森林加边,第二个森林会自动进行一样的操作,两个森林都不允许出现环,问最多能加多少边?

题解:

利用并查集分开维护两个森林,对于边u和v,如果uv在第一个森林不在一个集合内,在第二个森林也不在一个集合内,才可以加入
O( n 2 n^2 n2)

代码:

// Problem: D1. Mocha and Diana (Easy Version)
// Contest: Codeforces - Codeforces Round #738 (Div. 2)
// URL: https://codeforces.com/contest/1559/problem/D1
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-16 20:01:37
// By Jozky

#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
template <typename T> inline void read(T& x)
{
    T f= 1;
    x= 0;
    char ch= getchar();
    while (0 == isdigit(ch)) {
        if (ch == '-')
            f= -1;
        ch= getchar();
    }
    while (0 != isdigit(ch))
        x= (x << 1) + (x << 3) + ch - '0', ch= Codeforces Round #738 (Div. 2) 题解

Codeforces Round #738 (Div. 2) A - D1 题解

Codeforces Round #738 (Div. 2) A - D1 题解

Codeforces Round #738 (Div. 2) A - D1 题解

Codeforces Round #738 (Div. 2)

Codeforces Round #738 (Div. 2) 题解