AtCoder Beginner Contest 240

2022/2/20 23:56:43

本文主要是介绍AtCoder Beginner Contest 240,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

A

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a > b)  swap(a, b);
    if (b - a == 1 || b - a == 9)   puts("Yes");
    else puts("No");
    return 0;
}

B

#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
    scanf("%d", &n);
    set<int> s;
    for (int i = 1; i <= n; i ++ ) {
        int x;
        scanf("%d", &x);
        s.insert(x);
    }
    printf("%d\n", s.size());
    return 0;
}

C

#include <bits/stdc++.h>
using namespace std;
int n, m;
int st[10010];
int main() {
    scanf("%d%d", &n, &m);
    st[0] = 1;
    for (int i = 1; i <= n; i ++ ) {
        int a, b;
        scanf("%d%d", &a, &b);
        vector<int> c(m + 10);
        for (int j = 0; j <= m; j ++ ) 
            if (st[j] == 1)  {
                if (j + a <= m) c[j + a] = 2;
                if (j + b <= m) c[j + b] = 2;
            }
        for (int j = 0; j <= m; j ++ )
            if (c[j] == 2) st[j] = 1;
            else st[j] = 0;
    }
    if (st[m] == 1) puts("Yes");
    else puts("No");
    return 0;
}

D

#include <bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
int n, len;
int main() {
    scanf("%d", &n);
    vector<PII> v;
    for (int i = 1; i <= n; i ++ )  {
        int x;
        scanf("%d", &x);
        len ++;
        if (v.size() == 0)  v.push_back({x, 1});
        else {
            if (v[v.size() - 1].first == x && v[v.size() - 1].second == x - 1) 
                len -= x, v.pop_back();
            else {
                if (v[v.size() - 1].first == x) v[v.size() - 1].second ++;
                else v.push_back({x, 1});
            }
        }
        printf("%d\n", len);
    }
    return 0;
}

E

#include <bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
const int N = 2e5 + 10, M = N * 2;
int n;
PII range[N];
int h[N], e[M], ne[M], idx, s[N];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u, int fa)
{
    s[u] = 0;
    int sum = 1;
    for (int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if (j == fa)    continue;
        dfs(j, u);
        s[u] += max(s[j], 1);
    }
}
void dfs1(int u, int fa, int l, int r)
{
    int ll = l, rr = r;
    range[u].first = l, range[u].second = r;
    for (int i = h[u]; i != -1; i = ne[i]) {
        int j = e[i];
        if (j == fa)    continue;
        dfs1(j, u, ll, ll + max(s[j], 1) - 1);
        ll += max(s[j], 1);
    }
}
int main() {
    scanf("%d", &n);
    memset(h, -1, sizeof h);
    for (int i = 1; i < n; i ++ ) {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
    }
    dfs(1, -1);
    dfs1(1, -1, 1, s[1]);
    for (int i = 1; i <= n; i ++ ) 
        printf("%d %d\n", range[i].first, range[i].second);
    return 0;
}


这篇关于AtCoder Beginner Contest 240的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程