CF 949 D Curfew —— 二分答案

题目:http://codeforces.com/contest/949/problem/D

先二分一个答案,让两边都至少满足这个答案;

由于越靠中间的房间越容易满足(被检查的时间靠后),所以策略就是优先满足中间的房间,舍弃两边边缘的;

所以就由外到内推过来就可以了,用一个指针记录现在已经使用到的房间...

具体可以看这篇博客:https://www.cnblogs.com/Narh/p/9706060.html

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((l+r)>>1)
using namespace std;
typedef long long ll;
int const xn=1e5+5;
int n,d,m,a[xn],b[xn],t[xn],ans;
bool ck(int num)
{
    ll dis=(ll)d*(num+1);
    int p1=1,p2=n,s1=num+1,s2=n-num;
    memcpy(a,b,sizeof b);
    while(s1<s2)
    {
        int tmp=m;
        while(a[p1]<tmp)tmp-=a[p1++]; a[p1]-=tmp;
        if(p1-s1>dis)return 0;
        if(s1==(n+1)/2&&(n&1))break;
        tmp=m;
        while(a[p2]<tmp)tmp-=a[p2--]; a[p2]-=tmp;
        if(s2-p2>dis)return 0;
        dis+=d;
        s1++; s2--;
    }
    return 1;
}
int main()
{
    scanf("%d%d%d",&n,&d,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i];
    int l=0,r=(n+1)/2;
    while(l<=r)
    {
        if(ck(mid))ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/Zinn/p/9716632.html