【CH0805】防线

AcWing
(我写这道题的时候CH已经萎掉了,所以只有AcWing的链接)

分析

由于这题数据范围很大,我们无法直接统计每个位置的防具数量,于是考虑二分,在每次二分的过程中统计二分范围内的防具数量,记为 \(cnt\) 。然后因为偶数+偶数=偶数,而奇数+偶数=奇数,我们可以利用这个性质来判断是否符合条件,于是 \(check\) 函数直接返回 \(cnt \ \text{&} \ 1\) 即可。

注意要用 \(unsigned\)

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define maxn 200005
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;

template <typename T> inline void read(T &x) {
    T f = 1; x = 0; char c;
    for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    x *= f;
}

int n;
unsigned l, r;
unsigned sum[maxn], s[maxn], e[maxn], d[maxn];

bool check(unsigned lim) {
    unsigned cnt = 0;
    for (int i = 1; i <= n; ++i)
        if (s[i] <= lim)
            cnt += (min(lim, e[i]) - s[i]) / d[i] + 1;
    return cnt & 1;
}

void solve() {
    read(n); l = 0, r = 1U << 31;
    for (int i = 1; i <= n; ++i) read(s[i]), read(e[i]), read(d[i]);
    while(l < r) {
        unsigned mid = l + r >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    if (l == (1U << 31)) puts("There's no weakness.");
    else {
        unsigned cnt = 0;
        for (int i = 1; i <= n; ++i)
            if (s[i] <= l && e[i] >= l && (l - s[i]) % d[i] == 0)
                cnt++;
        printf("%u %u\n", l, cnt);
    }
}

int main() {
    int t;
    read(t);
    while(t--) solve();
    return 0;
}

转载于:https://www.cnblogs.com/hlw1/p/11321749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值