1≤A≤1,000,000,000,0≤B≤1,000,000,000,1≤N≤100,000
sum表示可以运到i点的鱼
如果sum是负值,再减去路费,就是先满足i城镇的,再处理下一个城镇
如果sum是正值,如果小于路费,就权当不要了,否则,减去路费运到下一个城镇
1 #include <algorithm>
2 #include <cstdio>
3 #define LL long long
4
5 using namespace std;
6
7 const int N(100000+15);
8 LL n,dis[N],fish[N];
9
10 LL l,r,mid,ans;
11 bool check(LL x)
12 {
13 LL sum=0;
14 for(int i=1;i<n;i++)
15 {
16 sum+=fish[i]-x;
17 if(sum<0) sum-=dis[i+1]-dis[i];
18 else if(sum<=dis[i+1]-dis[i]) sum=0;
19 else sum-=dis[i+1]-dis[i];
20 }
21 return sum+fish[n]>=x;
22 }
23
24 int main()
25 {
26 scanf("%lld",&n);
27 for(int i=1;i<=n;i++)
28 scanf("%lld%lld",dis+i,fish+i),r+=fish[i];
29 for(;l<=r;)
30 {
31 mid=l+r>>1;
32 if(check(mid))
33 {
34 l=mid+1;
35 ans=mid;
36 }
37 else r=mid-1;
38 }
39 printf("%lld",ans);
40 return 0;
41 }