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;
}