hdu5261 Monotone Queue

Source: Internet
Author: User
Tags cmath

Test instructions very difficult to understand, I saw many times, and finally still see the discussion version of the other people's question and answer, only to understand test instructions, really Khan.

In fact, the topic is equivalent to give n points, the n points evenly distributed on a circle (know the circle radius), the distance between point and point (ARC length) is known, the point is the right value, known, point and point distance is equal to its shortest journey (arc length) plus two points of weight, ask the farthest two points subscript.

Because it is a ring, not good processing, so I entered the time to deal with a simple, so that the problem into a straight line of the equivalence problem. The practice is to add the first half of the sequence after the input sequence, such as the sample 5 2 1 10 1 10 10, can be processed into 1 10 1 10 10 1 10, so that only the sequential processing, but the final loss of the subscript is to be processed, because the requirement is the smallest dictionary order subscript pair.

The problem of becoming a straight line is relatively simple. The easy-to-understand approach is to maintain a half-length queue, when dealing with I, to compare the points in the queue with the I-point, update the maximum, and then I queue, the head element out of the team. This is the efficiency of n^2, in the monotonous queue, it can be over. However, I do with the priority queue, the code is slightly more complicated than the monotonous queue, is also the brain wood, has not finished, Taurus put my code to go with the monotonous queue and then hit. First put on the Taurus monotone queue code, and then paste me, are 1a:

/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespacestd;#defineD (x)typedefLong LongLL;Const intMAXN =200000;intR, N; LL m;intDATA[MAXN]; LL ans;intANSI, ANSJ;voidUpdate (ll A, ll b) {ll temp= (b-a) * R + data[a] +Data[b]; A%= N-m; b%= N-m; if(A >b) Swap (A, b); if(Temp >ans) {D (printf ("a%lld b%lld\n", A, b)); Ans=temp; ANSI=A; ANSJ=b; return; }    if(Temp <ans)return; if(ans = = +)    if(ANSI > A | | (ANSI = = a && ansj >b)) {ANSI=A; ANSJ=b; return; }}ll work () {deque<LL>Q; Q.push_back (0); Long LongCur_pos =0; Long LongRET =0;  for(inti =1; i < N; i++) {Cur_pos+=R;  while(!q.empty () && Q.front () < I-m) Q.pop_front ();        Update (Q.front (), i);  while(!q.empty () && (I-q.back ()) * R + data[q.back ()] <Data[i]) q.pop_back ();    Q.push_back (i); }    returnret;}intMain () {intT; scanf ("%d", &T);  for(intt =1; T <= t; t++) {scanf ("%d%d", &n, &R);  for(inti =0; i < N; i++) {scanf ("%d", &Data[i]); } m= N/2;  for(inti =0; I < m; i++) {Data[i+ N] =Data[i]; } N+=m; Ans=0;        Work (); printf ("Case #%d\n%d%d\n", T, ANSI +1, ANSJ +1); }    return 0;}

Here's my priority queue:

/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespaceStd;typedefLong LongLL;Const intMAXN =200000; typedefstructMont {intheight; intindex;    LL value; Mont (intII =0,intHH =0, LL vv =0) {Height=hh; Index=II; Value=VV; }} Mont;BOOLInlineoperator< (Constmont& M1,Constmont&m2) {    returnM1.value <M2.value;} LL R;intN, M;intDATA[MAXN]; LL ans;intANSI, Ansj;inlinevoidTreatint&i,int&j) {if(I >= N-m) {i= i% (N-m); }    if(J >= N-m) {J= j% (N-m); }    if(I >j) {intt =i; I=J; J=T; }}voidWork () {priority_queue<Mont>Mont; Mont.push (Mont (0, data[0], data[0]));  for(inti =1; i < N; i++) {         while(Mont.top (). Index < (i-m)) {Mont.pop (); } Mont topm=Mont.top (); LL Tans= (i-topm.index) * R + data[i] +Data[topm.index]; if(Tans >ans) {ans=tans; ANSI=Topm.index; ANSJ=i;        Treat (ANSI, ANSJ); } Else if(Tans = =ans) {            intx =Topm.index; inty =i;            Treat (x, y); if(X < ANSI | | (x = = ANSI && y <ANSJ)) {ANSI=x; ANSJ=y; }} mont.push (Mont (i, Data[i], Data[i]IR)); }}intMain () {intT; scanf ("%d", &T);  for(intt =1; T <= t; t++) {scanf ("%d%lld", &n, &R);  for(inti =0; i < N; i++) {scanf ("%d", &Data[i]); } m= N/2;  for(inti =0; I < m; i++) {Data[i+ N] =Data[i]; } N+=m; Ans=0;        Work (); printf ("Case #%d:\n%d%d\n", T, ANSI +1, ANSJ +1); }    return 0;}

hdu5261 Monotone Queue

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.