AtCoder Beginner Contest 240 (A - D)

本文介绍了三个编程问题:A-EdgeChecker通过环形结构判断两点间是否存在边,B-CountDistinctIntegers计算不重复整数数量,C-JumpingTakahashi探讨能否完成特定步长跳跃任务。这些题目涉及数据结构、算法基础和逻辑判断。
摘要由CSDN通过智能技术生成

A - Edge Checker

题意:已知一个环,给你两个点 a a a b b b ,问你这两个点之间有没有边

做法:随便写,意思到位就行

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a, b;
    cin >> a >> b;
    if ((a == 10 && b == 1) || (a == 1 && b == 10)) puts("Yes");
    else if (abs(a - b) == 1) puts("Yes");
    else puts("No");
    return 0;
}

B - Count Distinct Integers

题意:给你一些数,问不重复数字的个数

做法,用集合直接去重输出大小就行

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    set<int> s;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        s.insert(x);
    }
    cout << s.size() << endl ;
    return 0;
}

C - Jumping Takahashi

题意:初始在0,可以走 n n n 步,每一次要么走 a a a 要么走 b b b ,问能否走完 n n n 步后到达 x x x

做法:暴力剪枝,同一步走到相同位置的删掉,超过 x x x 的删掉即可

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 110;
int t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    vector<int> A, B;
    A.push_back(0);
    B.push_back(0);
    for (int i = 0; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        unordered_set<int> S1, S2;
        vector<int> C, D;
        for (int i = 0; i < A.size(); i++)
        {
            int na = A[i] + a;
            int nb = A[i] + b;
            if (na <= m && !S1.count(na)) {
                C.push_back(na);
                S1.insert(na);
            }
            if (nb <= m && !S1.count(nb)) {
                C.push_back(nb);
                S1.insert(nb);
            }
        }
        for (int i = 0; i < B.size(); i++)
        {
            int na = B[i] + a;
            int nb = B[i] + b;
            if (na <= m && !S2.count(na)) {
                D.push_back(na);
                S2.insert(na);
            }
            if (nb <= m && !S2.count(nb)) {
                D.push_back(nb);
                S2.insert(nb);
            }
        }
        A = C;
        B = D;
    }
    if (count(A.begin(), A.end(), m) || count(B.begin(), B.end(), m)) puts("Yes");
    else puts("No");
    return 0;
}

D - Strange Balls

题意:给定 n n n 个球,每个球都有一个权值,每次往柱子里放小球,如果连续 i i i 个权值为 i i i 的球就会抵消掉,让你输出放入第 i i i 个球后的体积

做法:模拟,开一个 p a i r pair pair 数组用来记录放入第 i i i 个球时,前面有多少个和它权值相同的球

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
int t, n, m;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    vector<pair<int, int>> a;
    for (int i = 0; i < n; i++)
    {
        cin >> m;
        int cnt;
        if (a.empty() || a.back().first != m) cnt = 1;
        else cnt = a.back().second + 1;
        a.push_back({m, cnt});
        if (a.back().second == m)
        {
            while (m--)
            {
                a.pop_back();
            }
        }
        cout << a.size() << endl ;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值