Codeforces Round 947 (Div. 1 + Div. 2) A - D

 

Codeforces Round 947 (Div. 1 + Div. 2) A - D题_哔哩哔哩_bilibili

解析都在视频里哦

A - Bazoka and Mocha's Array

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++)
            cin >> a[i];
        int cnt = 0;
        for (int i = 0; i < n - 1; i++)
            if (a[i] > a[i + 1])
                cnt++;
        if (a[0] < a[n - 1])
            cnt++;
        if (cnt > 1)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
}

B - 378QAQ and Mocha's Array

#include<bits/stdc++.h>
using namespace std;
int main() {
    
    int T;
    cin >> T;
    while (T--)
    {
        int n, dig, cnt = 0;
        cin >> n;
        set<int> S;
        for (int i = 0; i < n; i++)
        {
            cin >> dig;
            S.insert(dig);
        }
        dig = *S.begin();

        for (auto i : S)
            if (i % dig != 0)
                cnt++;
        if (cnt > 1)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
    return 0;
}

C - Chamo and Mocha's Array

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        for (auto &i : a)
            cin >> i;
        int maxNum = INT_MIN;
        if (n == 2)
            cout << min(a[0], a[1]) << endl;
        else
        {
            for (int i = 0; i < n - 2; i++)
            {
                vector<int> temp{a[i], a[i + 1], a[i + 2]};
                sort(temp.begin(), temp.end());
                maxNum = max(maxNum, temp[1]);
            }
            cout << maxNum << endl;
        }
    }
}

D - Paint the Tree

#include<bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
vector<int> parTo;
vector<int> disTo;
int maxStep = INT_MIN;
void dfs(int cur, int par, int step)
{
    maxStep = max(maxStep, step);
    parTo[cur] = par;
    disTo[cur] = step;
    for (auto &i : adj[cur])
        if (i != par)
            dfs(i, cur, step + 1);
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, a, b, x, y;
        cin >> n >> a >> b;
        parTo.assign(n + 5, 0), disTo.assign(n + 5, 0);
        for (int i = 1; i <= n; i++)
            adj[i].clear();
        for (int i = 0; i < n - 1; i++)
        {
            cin >> x >> y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        dfs(a, a, 0);
        int firstBlue = b, times = (disTo[b] + 1) / 2;
        int ans = times;
        while (times--)
            firstBlue = parTo[firstBlue];
        maxStep = INT_MIN;
        dfs(firstBlue, firstBlue, 0);
        ans += (n - 1) * 2 - maxStep;
        cout << ans << endl;
    }
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值