506C Mr. Kitayuta vs. Bamboos

分析

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,p,k,a[100100],h[100100],now[100100],cnt[100100];
inline bool ck(int mid){
    int i,j,tot=0,res=0;
    for(i=1;i<=n;i++)now[i]=h[i]+a[i]*m,tot+=max(0ll,(now[i]-mid-1+p)/p);
    for(i=0;i<=m;i++)cnt[i]=0;
    if(tot>m*k)return 0;
    for(i=1;i<=n;i++)if(now[i]>mid)
      for(j=(now[i]-mid-1)%p+1;j<=now[i]-mid;j+=p)
        if(j<=h[i])cnt[0]++;else if(j>h[i]+(m-1)*a[i])return 0;else cnt[(j-h[i]-1+a[i])/a[i]]++;
    for(i=0;i<m;i++){
      res+=cnt[i];
      res=max(0ll,res-k);
    }
    return !res;
}
signed main(){
    int i,j;scanf("%lld%lld%lld%lld",&n,&m,&k,&p);
    for(i=1;i<=n;i++)scanf("%lld%lld",&h[i],&a[i]);
    int le=0,ri=1e15;
    while(ri-le>1){
      int mid=(le+ri)>>1;
      if(ck(mid))ri=mid;
        else le=mid;
    }
    printf("%lld\n",ri);return 0;
}

 

posted @ 2019-11-08 15:08  水题收割者  阅读(142)  评论(0编辑  收藏  举报