Codeforces Round #662 (Div. 2)题解

本文详细解析了Codeforces Round #662 (Div. 2)比赛中的A到E五道题目,包括解题思路、示例代码和难点讨论。A题采用奇数开始的方法,B题通过构造解决,C题使用贪心算法,D题尝试暴力搜索和DP但未成功,E题因题目理解难度大而未给出解法。作者对本次表现评价一般,表示将努力提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题

解题思路:
从n为奇数开始写。
示例代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        long long n;
        cin >> n;
        cout << (n + 2) / 2 << endl;
    }
}

B题

解题思路:
构造。不过必须要注意,构造的时候只需要用这些木棒构成长方形和正方形的周长部分即可,并且每条边只能放一根木棒。
示例代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long num[100005];
int main()
{
    int n;
    cin >> n;
    for(ll i = 0;i < n;i++)
    {
        ll x;
        cin >> x;
        num[x]++;
    }
    ll cas2 = 0,cas4 = 0;
    for(ll i = 1;i <= 100000;i++)
    {
        cas2 += num[i] / 2;
        if(num[i] > 3) cas4++;
    }
    ll q;
    cin >> q;
    while(q--)
    {
        char c;
        ll x;
        cin >> c >> x;
        if(c == '-')
        {
            if(num[x] == 4) cas4--;
            if(num[x] % 2 == 0) cas2--;
            num[x]--;
        }
        else
        {
            if(num[x] == 3) cas4++;
            if(num[x] % 2) cas2++;
            num[x]++;
        }
        if(cas4 && cas2 > 3) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

C题

解题思路:
贪心。这题不太难,就正常贪心然后再模拟就ok了。
示例代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        ll n;
        cin >> n;
        vector<ll>num(n + 1,0);
        ll Max = 0;
        for(ll i = 0;i < n;i++)
        {
            ll x;
            cin >> x;
            num[x]++;
            Max = max(Max,num[x]);
        }
        ll  = cnt0;
        for(ll i = 1;i <= n;i++)
            if(num[i] == Max) cnt++;
        cnt += (n - Max * cnt) / (Max - 1);
        cout << cnt - 1 << endl;
    }
}

D题

解题思路:
这道题原本我是想弄一个暴力搜索,结果搜着搜着发现好像会超时。于是又想弄dp但最后还是没弄出来。最后翻了一些大佬的题解,懂了一些但是还是没有完全懂。
示例代码:

E题

解题思路:
这道题有两个版本。但是我连题目也没看懂。不知道该怎么做。也没有什么大佬写这题题解。好难。
示例代码:

总结

这回做的很一般。个人认为不太好。下次加油吧。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值