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;
}
上一篇:Background Matting V2算法讲解


下一篇:巧克力王国