Codeforces Round 945(Div.2)题解(A , B)

A. Chess For Three

暴力枚举+动态规划+模拟+数学 

有两种情况:①+2  +0   ②+1  +1  ,但不管怎么样比赛次数一定是 +2 ,所以比赛次数总和一定是偶数

p1+p2<=p3   \rightarrow   输出 p1+p2

p1+p2>p3     \rightarrow   输出 (p1+p2+p3)/2

#include <bits/stdc++.h>
 
using namespace std;
 
void solve()
{
    int p1, p2, p3;
    cin >> p1 >> p2 >> p3;
    int x = p1 + p2 + p3;
    if (x % 2 == 1)
        cout << "-1\n";
    else
        cout << min(x / 2, p1 + p2) << '\n';
}
 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

B. Cat, Fox and the Lonely Array

二分+状态压缩+数据结构+贪心+数学+双指针

拆位转换成 0 1,那这道题就转换成为了两个 1 之间的最大距离(因为只有最大才能满足所有的要求)

小注意:1 0 0 0 0 是 5

but 这道题的思路感觉自己还得再理一下,maybe 晚点还会进行补充,更加充足

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int a[N];
 
void solve()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int ans = 1;
    for (int bit = 0; bit < 20; bit++)
    {
        int l = -1, mx = 0;
        for (int i = 0; i < n; i++)
        {
            if (a[i] >> bit & 1)
            {
                mx = max(mx, i - l - 1);
                l = i;
            }
        }
        mx = max(mx, n - l - 1);
        if (mx == n)
            continue;
        ans = max(ans, mx + 1);
    }
    cout << ans << '\n';
}
 
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

July.19th

感谢各位对我的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值