Atcoder Beginner Contest 355 A - D

Atcoder Beginner Contest 355 A - D


A. Who Ate the Cake?

传送门
在这里插入图片描述

思路:
签到题直接模拟即可,只要a和b不相同就一定能确认犯人是c

//
// Created by Zhuang on 2024/5/25.
//
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int a, b, c;
    cin >> a >> b;
    if (a == b)
        cout << "-1\n";
    else
        cout << 6 - a - b << "\n";

    return 0;
}

B. Piano 2

传送门
在这里插入图片描述
在这里插入图片描述

思路:

  • 开一个bool数组记录原数组中出现的数
  • 对原数组排序后,枚举判断相邻的数是否被bool数组记录过即可
//
// Created by Zhuang on 2024/5/25.
//

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 110;
int b[N];
bool ba[300];

int main()
{

    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n, m;
    cin >> n >> m;
    vector<int> c;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        ba[a[i]] = true;
        c.emplace_back(a[i]);
    }
    for (int i = 0; i < m; i++)
        cin >> b[i], c.emplace_back(b[i]);

    sort(c.begin(), c.end());

    int len = c.size();
    //    for (int i = 0; i < len; i++)
    //    {
    //        d2[c[i]] = i + 1;
    //    }

    //    int idx1 = 0, idx2 = 0;
    /*for (int i = 0; i < len - 1; i++)
    {
        if (find(a.begin(), a.end(), c[i]) == a.end()) continue;
        if(abs(d1[c[i]] - d1[c[i + 1]]) == 1) {
            cout << "Yes\n";
            return 0;
        }
    }*/
    for (int i = 0; i < len; i++)
    {
        if (i == 0)
        {
            if (ba[c[i]] && ba[c[i + 1]])
            {
                cout << "Yes";
                return 0;
            }
        }
        else if (ba[c[i]] && ba[c[i - 1]])
        {
            cout << "Yes";
            return 0;
        }
    }
    cout << "No\n";

    return 0;
}

C. Bingo 2

传送门
在这里插入图片描述

思路:
类似n皇后问题的处理方式,当需要记录一个数时,如果这个数没有被记录,则在一个 n * n 上的图中行、列、正对角线、副对角线都记录一边。最后判断有其中一个满足条件即可。

//
// Created by Zhuang on 2024/5/25.
//

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int mod = 200001;
const int N = 1e4;
int han[N], ug[N], dg[N], lie[N];
set<int> vis;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n, t, v;
    cin >> n >> t;
    for (int i = 1; i <= t; i++)
    {
        cin >> v;
        int x = (v - 1) / n;
        int y = (v - 1) % n;

        if (!vis.count(v))
        {
            han[x]++;
            lie[y]++;
            ug[x + y]++;
            dg[x - y + n]++;

            if (han[x] == n || lie[y] == n || ug[x + y] == n || dg[x - y + n] == n)
            {
                cout << i << "\n";
                return 0;
            }
            vis.insert(v);
        }
    }

    cout << "-1\n";

    return 0;
}

D. Intersecting Intervals

传送门
在这里插入图片描述

思路:
只需要分开维护左右两边端点,然后算出全部相交后区间,减去不相交的区间即可。

//
// Created by Zhuang on 2024/5/25.
//

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n;
    cin >> n;
    vector<int> l(n), r(n);

    for (int i = 0; i < n; i++)
        cin >> l[i] >> r[i];

    sort(l.begin(), l.end());
    sort(r.begin(), r.end());

    int t = 0;
    ll res = n * (n - 1ll) / 2;
    for (auto &xx : l)
    {
        while (t < n && r[t] < xx) t++;
        res -= t;
    }
    cout << res << "\n";
    return 0;
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值