Poj 3245 sequence partitioning

Source: Internet
Author: User

Poj_3245

This question is very similar to that of poj_3017, but the known restrictions and requirements are reversed.

This question is actually very difficult to read, but it is still understandable Based on the hint he gave. I suggest you not to look at the expressions in the first section. The more I see it, the more dizzy it is, I don't know what he is talking about, as long as I know that the groups to be divided must be continuous, and then I can ignore the first paragraph.

For the first condition, it is actually required that all P <q and p and q are not in the same group. Therefore, BP> AQ must be available. In other words, if P <q and BP <= AQ exist, p and q must be in the same group, in this way, we can combine the points that must be grouped together with those between them as a point, and then use sum {bi} as the B of the point, use max {ai} as the of this point.

After the first condition is processed, the problem becomes: divide these points into several groups, and the sum of the MI values of each group must meet the constraints, and find the minimum value of max {Si, si indicates the sum of B values of vertices in group I.

Because the restriction condition for the sum of MI is global, we cannot guarantee that this condition is always true when processing a part, so we need to change the way of thinking, divide the minimum value x of max {Si} into the following two points: divide these points into several groups, and the sum of B values in each group cannot exceed X, calculate the sum of the MI values of each group and the minimum sum of the maximum values of group A, and determine whether the value is less than or equal to limit. In this way, the question is converted into the question poj_3017. How can we find the minimum sum of the maximum values of A in each group without exceeding the sum of the values of B in each group, you can refer to my poj_3017 question: http://www.cnblogs.com/staginner/archive/2012/04/02/2429850.html.

# Include <stdio. h>
# Include < String . H>
# Define Maxd 50010
Int N, M, L, t, node, size [maxd], left [maxd], right [maxd], pool [maxd], top;
Struct Pair
{
Int A, B;
} P [maxd];
Int Maxa [maxd], a [maxd], B [maxd], Q [maxd], d [maxd], B [maxd];
Long Long Int Key [maxd], F [maxd];
Int Max ( Int X, Int Y)
{
Return X> Y? X: Y;
}
Int Min (Int X, Int Y)
{
Return X <Y? X: Y;
}
Void Leftrotate ( Int & T)
{
Int K = right [T];
Right [T] = left [k];
Left [k] = T;
Size [k] = size [T];
Size [T] = size [left [T] + size [right [T] + 1 ;
T = K;
}
Void Rightrotate ( Int & T)
{
Int K = left [T];
Left [T] = right [k];
Right [k] = T;
Size [k] = size [T];
Size [T] = size [left [T] + size [right [T] + 1 ;
T = K;
}
Void Maintain ( Int & T, Int Flag)
{
If (Flag = 0 )
{
If (Size [left [left [T]> size [right [T])
Rightrotate (t );
Else If (Size [right [left [T]> size [right [T])
Leftrotate (left [T]), rightrotate (t );
Else
Return ;
}
Else
{
If (Size [right [right [T]> size [left [T])
Leftrotate (t );
Else If (Size [left [right [T]> size [left [T])
Rightrotate (right [T]), leftrotate (t );
Else
Return ;
}
Maintain (left [T], 0 );
Maintain (right [T], 1 );
Maintain (t,0 );
Maintain (t, 1 );
}
Void Init ()
{
Int I, J, K, Min, Max, sum;
For (I = 1 ; I <= N; I ++)
Scanf ( " % D " , & P [I]. A, & P [I]. B );
Maxa [n + 1 ] = 0 ;
For (I = N; I> 0 ; I --)
Maxa [I] = max (P [I]. A, maxa [I + 1 ]);
M = B [ 0 ] = 0 ;
For (I = 1 ; I <= N; I ++)
{
Max = P [I]. A, min = P [I]. B, sum = P [I]. B;
While (I <= N & min <= maxa [I + 1 ])
{
++ I;
Max = max (P [I]. A, max );
Min = min (P [I]. B, min );
Sum + = P [I]. B;
}
++ M;
A [m] = max, B [m] = sum;
B [m] = B [m- 1 ] + B [m];
}
}
Long Long Int Delete (Int & T, Long Long Int V)
{
-- Size [T];
If (Key [T] = v | (v <key [T] & left [T] = 0 ) | (V> = Key [T] & right [T] = 0 ))
{
Int K = Key [T];
If (Left [T] = 0 | Right [T] =0 )
Pool [top ++] = t, t = left [T] + right [T];
Else
Key [T] = Delete (left [T], key [T] + 1 );
Return K;
}
If (V <key [T])
Return Delete (left [T], V );
Else
Return Delete (right [T], V );
}
Void Newnode ( Int & T, Long Long Int V)
{
If (Top)
T = pool [-- top];
Else
T = ++ node;
Key [T] = V;
Size [T] = 1 ;
Left [T] = right [T] = 0 ;
}
Void Insert ( Int & T, Long Long Int V)
{
If (T = 0 )
{
Newnode (T, V );
Return ;
}
++ Size [T];
If (V <key [T])
Insert (left [T], V );
Else
Insert (right [T], V );
Maintain (T, V> = Key [T]);
}
Long Long Int Searchmin ( Int & T)
{
If (Left [T] = 0 )
Return Key [T];
Else
Return Searchmin (left [T]);
}
Int Dowell ( Int Limit)
{
Int I, J, K, front, rear, demo;
T = node = Top = size [ 0 ] = Left [ 0 ] = Right [ 0 ] = 0 ;
Front = rear = demo-= 0 ;
For (I = 1 ; I <= m; I ++)
{
While (B [I]-B [demit]> limit)
++ Demo;
While (Front <rear & Q [Front] <= demo)
{
Delete (T, F [d [Front] + A [Q [Front]);
++ Front;
}
While (Front <rear & A [Q [rear- 1 ] <= A [I])
{
Delete (T, F [d [rear- 1 ] + A [Q [rear- 1 ]);
-- Rear;
}
Q [rear] = I, d [rear] = front <rear? Q [rear- 1 ]: Demo;
Insert (T, F [d [rear] + A [I]);
++ Rear;
If (D [Front] <demo)
{
Delete (T, F [d [Front] + A [Q [Front]);
D [Front] = demo;
Insert (T, F [demo-] + A [Q [Front]);
}
F [I] = searchmin (t );
}
Return F [m] <= L;
}
Void Solve ()
{
Int I, Max, Min, mid;
Min = 0 ;
For (I = 1 ; I <= m; I ++)
Min = max (B [I], min );
-- Min, max = B [m];
For (;;)
{
Mid = (max-min +1 )/ 2 + Min;
If (Mid = max)
Break ;
If (Dowell (MID ))
Max = mid;
Else
Min = mid;
}
Printf ( " % D \ n " , Max );
}
Int Main ()
{
While (Scanf ( " % D " , & N, & L) = 2 )
{
Init ();
Solve ();
}
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.