ZOJ-1508 intervals difference Constraint

Source: Internet
Author: User

Given several consecutive integer intervals [ai, BI] and CI, we now need such a set Z, the element in the Z set must have at least one CI at the intersection of the element in each given interval.

Solution: Consider dis [I] as the number of elements in Z smaller than or equal to integer I. Then for the given condition of the problem has the expression dis [B]-Dis [A-1]> = CI, according to this inequality can be side. In addition, there are some implicit conditions: 0 <= dis [I]-Dis [I-1] <= 1, side construction of these relations. Then the required value is dis [Max]-Dis [Min-1]> = m in the m, where Max and min are the smallest left and right boundary, so dis [Max] = 0, that is, the opposite number of short circuits from Max to Min-1.

Here I explain why it is the shortest short circuit from Max to Min-1, since dis [Max]-Dis [Min-1]> = m is always set, and m is also a variable, therefore, if M is greater than M's maximum value, the problem is transformed into how to solve the maximum value of M in this inequality, through the transformation of the formula sub-to dis [Min-1]-Dis [Max] <=-M, that is, to find the minimum value of-m, and this-m on the way is the smallest Min-1 to the max of the short circuit, otherwise dis [Min-1] will be replaced by DIS [Max]-M.

CodeAs follows:

# Include <cstdlib> # Include <Cstdio> # Include <Iostream> # Include <Cstring># Include <Algorithm> Using   Namespace  STD;  Int N, idx, head [ 50005  ];  Int  Min, Max;  //  Set the map to find the information dis [I] in the shortest path, which indicates the number of information contained in the Z set smaller than I.  Struct  Edge {  Int  V, CT, next;} e [ 160000  ];  Void Insert ( Int A, Int B, Int  CT) {e [idx]. v = B, E [idx]. Ct = CT; E [idx]. Next = Head [a]; head [A] = Idx ++ ;}  Int Dis [ 50005  ];  Char Vis [50005  ]; # Include <Queue> Void  Spfa () {queue < Int > Q; memset (DIS,  0x3f , Sizeof  (DIS); memset (VIS,  0 , Sizeof  (VIS); q. Push (max); vis [Max] = 1 , DIS [Max] = 0 ;  While (! Q. Empty ()){  Int V = Q. Front (); q. Pop (); vis [v] = 0  ;  Int  TV;  For ( Int I = head [v]; I! =- 1 ; I = E [I]. Next ){  If (DIS [E [I]. V]> dis [v] + E [I]. CT) {dis [E [I]. V] = Dis [v] + E [I]. CT;  If (! Vis [E [I]. V]) {q. Push (E [I]. V); vis [E [I]. V] = 1  ;}}}}}  Int  Main (){  Int  A, B, C;  While (Scanf ( " % D  " , & N )! = EOF) {idx = 0  ; Min = 50005 , Max = 0  ; Memset (Head,  0xff , Sizeof  (Head ));  For ( Int I = 0 ; I <n; ++I) {scanf (  "  % D  " , & A, & B ,& C ); ++ A, ++ B; //  Because 0 is a valid number, a negative number will appear when the minimum number is reduced by 1. Min = Min (Min, a); max = Max (max, B); insert (B, - 1 ,- C );}  For ( Int I = min; I <= max; ++ I) {insert (I - 1 , I, 1  ); Insert (I, I - 1 , 0  );} Spfa (); printf (  "  % D \ n  " ,-Dis [Min- 1  ]);}  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.