poj3245 Sequence Partitioning

24 篇文章 0 订阅
3 篇文章 0 订阅
Sequence Partitioning
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 (AiBi), you have to partition it into several contiguous parts. Let p be the number of these parts, whose boundaries are (l1r1), (l2r2), ... ,(lprp), which satisfy l= ri − + 1, l ril1 = 1, rn. The parts themselves also satisfy the following restrictions:

  1. For any two pairs (ApBp), (Aq, Bq), where (Ap, Bp) is belongs to the Tpth part and (AqBq) the Tqth part. If Tp < Tq, then B> Aq.

  2. Let Mi be the maximum A-component of elements in the ith part, say

    Mi = max{AliAli+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 (AB). It's always guaranteed that

max{ A 1A 2, ...,  An} ≤ Limit

Output

Output the minimum target value.

Sample Input

4 6
4 3
3 5
2 5
2 4

Sample Output

9

Hint

An available assignment is the first two pairs are assigned into the first part and the last two pairs are assigned into the second part. Then  B 1 >  A 3B 1 >  A 4B 2 >  A 3B 2 >  A 4, max{ A 1A 2}+max{ A 3A 4} ≤ 6, and minimum max{ B 1+ B 2B 3+ B 4}=9.

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值