Poj 3245 sequence partitioning (DP + binary + monotonic queue + SBT)

Source: Internet
Author: User
Sequence partitioning
Time limit:8000 ms   Memory limit:65536 K
Total submissions:710   Accepted:207
Case time limit:5000 Ms

Description

Given a sequenceNOrdered pairs of positive integers (AI,Bi), You have to partition it into several contiguous parts. LetPBe the number of these parts, whose boundaries are (L1,R1 ),
(L2,R2 ),...,(Lp,RP), Which satisfyLi=RI −1 + 1,LiRi,L1 =
1,RP=N. The parts themselves also satisfy the following restrictions:

  1. For any two pairs (AP,BP),(AQ,BQ), Where (AP,BP) Is belongs toTPTh part and (AQ,BQ)
    TheTQTh part. IfTP<TQ, ThenBP>AQ.

  2. LetMiBe the maximumA-Component of elements inITh part, say

    Mi= Max {Ali,ALI +1 ,...,Ari}, 1 ≤IP

    It is provided that

    Where limit is a given integer.

LetSiBe the sumB-Components of elements inITh part. Now I want to minimize the value

Max {Si: 1 ≤I≤ P}

Cocould 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 {A1,A2 ,...,An} ≤Limit

Output

Output the minimum target value.

Sample Input

4 64 33 52 52 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. ThenB1>A3,B1>A4,B2>A3,B2>A4,
Max {A1,A2} + max {A3,A4} ≤ 6, and minimum max {B1 +B2,B3 +B4} = 9.

Source

Poj monthly -- 2007.07.08, Chen, danqi

Question: http://poj.org/problem? Id = 3245

Question: Let's give you a pair of columns, which are actually two columns. You can divide the series into any segment. The requirements are as follows:

1. For any element P and element Q (P <q) in different segments, there are BP> AQ

2. The sum of the maximum values of element a of all segments must be smaller than the given limit.

Evaluate the maximum value of the sum of Element B in each segment that meets the preceding conditions.

Analysis: first, consider the first restriction. We will find that if the Aj after a series is greater than or equal to the current bi, all the elements of I to J must be in one block, then they will be merged. Merging means that a takes the maximum value and B takes the sum. There are many ways to merge them. We can sort the subscript of Element B in ascending order of Element B, then, element a is enumerated in reverse order, and all elements B smaller than element a are marked with the largest label, and then all parts are merged once.

The rest is the second limit. We will find that the second limit is the reverse solution to this question. We only need to have two answers and use the previous practice to determine the feasibility.

Code:

#include<set>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const int mm=55555;int a[mm],q[mm],p[mm],f[mm],b[mm];int i,j,n,limit,l,r,m,ans;multiset<int>sbt;bool check(int s){    int i,p,l=0,r=-1,tmp;    __int64 sum=0;    sbt.clear();    for(p=i=1;i<=n;++i)    {        sum+=b[i];        while(sum>s)sum-=b[p++];        if(p>i)return 0;        while(l<=r&&a[i]>=a[q[r]])        {            if(l<r)sbt.erase(f[q[r-1]]+a[q[r]]);            --r;        }        q[++r]=i;        if(l<r)sbt.insert(f[q[r-1]]+a[q[r]]);        while(q[l]<p)        {            if(l<r)sbt.erase(f[q[l]]+a[q[l+1]]);            ++l;        }        f[i]=f[p-1]+a[q[l]];        tmp=*sbt.begin();        if(l<r&&f[i]>tmp)f[i]=tmp;    }    return f[n]<=limit;}bool cmp(int u,int v){    return b[u]<b[v];}int main(){    while(~scanf("%d%d",&n,&limit))    {        for(i=1;i<=n;++i)            scanf("%d%d",&a[i],&b[i]),q[i]=p[i]=i;        sort(p+1,p+n+1,cmp);        for(j=1,i=n;i>0;--i)            while(j<=n&&b[p[j]]<=a[i])q[p[j++]]=i;        for(j=i=1;i<=n;i=l,++j)        {            a[j]=a[i],b[j]=b[i];            for(l=i+1,r=max(q[i],i);l<=r;++l)            {                b[j]+=b[l];                a[j]=max(a[j],a[l]);                if(q[l]>r)r=q[l];            }        }        n=j-1,l=0,r=2147483647;        while(l<=r)        {            m=(l+r)>>1;            if(check(m))ans=m,r=m-1;            else l=m+1;        }        printf("%d\n",ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.