Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 1443 | Accepted: 407 | |
Case Time Limit: 5000MS |
Description
Given a sequence of N ordered pairs of positive integers (Ai, Bi), you have to partition it into several contiguous parts. Let p be the number of these parts, whose boundaries are (l1, r1), (l2, r2), ... ,(lp, rp), which satisfy li = ri − 1 + 1, li ≤ ri, l1 = 1, rp = n. The parts themselves also satisfy the following restrictions:
For any two pairs (Ap, Bp), (Aq, Bq), where (Ap, Bp) is belongs to the Tpth part and (Aq, Bq) the Tqth part. If Tp < Tq, then Bp > Aq.
Let Mi be the maximum A-component of elements in the ith part, say
Mi = max{Ali, Ali+1, ..., Ari}, 1 ≤ i ≤ p
it is provided that
where Limit is a given integer.
Let Si be the sum of B-components of elements in the ith part. Now I want to minimize the value
max{Si:1 ≤ i ≤ p}
Could you tell me the minimum?
Input
The input contains exactly one test case. The first line of input contains two positive integers N (N ≤ 50000), Limit (Limit ≤ 231-1). Then follow N lines each contains a positive integers pair (A, B). It's always guaranteed that
max{ A 1, A 2, ..., An} ≤ LimitOutput
Sample Input
4 6 4 3 3 5 2 5 2 4
Sample Output
9
Hint
Source
题解:因为某些特殊原因而做了这题。。
然后我发现这题很神。。。
具体分组条件就是分组必须满足两个条件:
1.对于任意元素p和元素q(p<q)在不同段中,那么有Bp>Aq
2.所有段的A元素的最大值的和要小于给定的限制。
所以我们按b排序,维护一定要分在一起的区间,最后二分答案,就完了(说起来好像很简单的样子
最后orz感谢网上dalao
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,a[100000],b[100000],f[100000],p1[100000],p2[100000],q[100000];
bool cmp(int aa,int bb){
return b[aa]<b[bb];
}
int pd(int x){
int i,j=1,l=1,r=0,s=0,ii;
memset(f,0,sizeof(f));
for(i=1;i<=n;i++){
s+=b[i];
while(s>x)s-=b[j++];
if(j>i)return 0;
while(l<=r&&a[q[r]]<=a[i])r--;
while(l<=r&&q[l]<j)l++;
q[++r]=i;
f[i]=f[j-1]+a[q[l]];
for(ii=l+1;ii<=r;ii++)
f[i]=min(f[i],f[q[ii-1]]+a[q[ii]]);
}
return f[n]<=m;
}
int main(){
int l,r,mid,i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]),p1[i]=p2[i]=i;
sort(p1+1,p1+n+1,cmp);
for(i=n,j=1;i;i--){
for(;j<=n&&b[p1[j]]<=a[i];j++)p2[p1[j]]=i;
}
for(i=1,j=1;i<=n;i=l,j++){
a[j]=a[i];b[j]=b[i];
for(l=i+1,r=max(p2[i],i);l<=r;l++){
a[j]=max(a[j],a[l]);
b[j]+=b[l];
r=max(r,p2[i]);
}
}
n=j-1;
l=0;r=1000000000;
while(l<r){
mid=(l+r)/2;
if(pd(mid))r=mid;
else l=mid+1;
}
printf("%d",l);
}