BZOJ 1271: [BeiJingWc2008]秦腾与教学评估 二分查找

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

解法:

因为题中说了,最多只有一个位置有奇数个人。

所以我们可以先check一下,如果总人数是偶数那么显然就是

Poor Qin Teng

接下来的话我们只需要二分出那个奇数的位置,检验l到这个位置有多少个人即可。

得到位置之后直接O(n)求一下该位置的人数即可。

//BZOJ 1271 binary

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
typedef long long LL;
LL s[maxn], e[maxn], d[maxn];
int n;
LL check(LL x){
    LL re = 0;
    for(int i=1; i<=n; i++){
        if(s[i]>x) continue;
        LL tmp = min(x, e[i]);
        re += (tmp-s[i])/d[i]+1;
    }
    return re;
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        LL maxe = 0;
        for(int i=1; i<=n; i++) scanf("%lld%lld%lld", &s[i],&e[i],&d[i]), maxe = max(maxe, e[i]);
        LL tmp = check(maxe);
        if((tmp&1)==0){
            puts("Poor QIN Teng:(");
            continue;
        }
        LL l = 0, r = maxe, ans;
        while(l<=r){
            int mid=(l+r)/2;
            if(check(mid)&1) ans=mid,r=mid-1;
            else l=mid+1;
        }
        LL cnt=0;
        for(int i=1;i<=n;i++){
            if(s[i]>ans) continue;
            if(e[i]<ans) continue;
            if((ans-s[i])%d[i]==0) cnt++;
        }
        printf("%lld %lld\n", ans, cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值