题解 CH0805 【防线】

Problem

@CH0805.png

Solution

由于题目中提到了奇数位置要么没有,要么有1个,所以若区间[L,R]中有奇数位置,等价于这个区间里的防具数量为奇数个。
考虑到n只有200000,可以考虑二分奇数点的位置p,然后判断区间[0,p]中的防具的个数。
显然对于一组防具\((s_i,e_i,d_i)\),它对于个数的贡献是$\Large \frac{min(e_i,p)-s_i}{d_i} +1 $
但要注意:\(min(e_i,p)\)有可能小于\(s_i\),需要特判。

Code

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=200005;
int T,n;
LL s[maxn],e[maxn],d[maxn],maxe;
inline LL sum(LL p)
{
    LL tot=0;
    for(int i=0;i<n;i++) if(min(e[i],p)>=s[i]) tot+=(min(e[i],p)-s[i])/d[i]+1;
    return tot;
}
int main()
{
#ifdef local
    freopen("pro.in","r",stdin);
#endif
    scanf("%d",&T);
    while(T-->0)
    {
        scanf("%d",&n);
        maxe=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld%lld\n",&s[i],&e[i],&d[i]);
            maxe=max(maxe,e[i]);
        }
        LL L=0,R=maxe+1,M,res=maxe+1;
        while(L<=R)
        {
            M=(L+R)>>1;
            if(sum(M)&1) res=M,R=M-1;
            else L=M+1;
        }
        if(res==maxe+1) puts("There's no weakness.");
        else printf("%lld %lld\n",res,sum(res)-sum(res-1));
    }
    return 0;
}

转载于:https://www.cnblogs.com/happyZYM/p/11379039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值