"Bzoj" "1986" "Usaco 2004 DEC"/"POJ" "2373" zoned area irrigation

Source: Internet
Author: User

dp/Monotone Queue optimization

First of all, regardless of the cow's favorite interval, the DP equation is certainly more obvious: $ f[i]=min (f[k]) +1,i-2*b \leq K \leq i-2*a $ of course here the $i$ and $k$ are even ~ this should be well understood ... Every time the sprinkler is an even-length interval ...

Then add the cow's favorite interval, just like this: when $ i>cow[j].x $, make $ I=COW[J].Y, j++$ that is to say the middle of the position all do not consider putting the sprinkler.

Obviously we can maintain the K for each node in a monotone queue! Well, I see the students here can try to write and read the first.

If you have a sample, don't worry, try my set of data:

2 16
2 4
7 8
6 12

Trick:

Each cow's favorite interval is an "open zone"! The dividing point can be irrigated by different sprinkler (take a closer look at the diagram of the sample)

The first English question ... I didn't look at English in Chinese. I don't see any more "illegal output-1" so sad ...

Each f[i] can not just calculate out on the team tail + into the tail, because at this time the next position for i+2, may be able to transfer to i+2 of the legitimate state to bounce out, and f[i] can not be transferred to F[I+2]! (Because of the limitation of a) it causes f[i+2] to calculate errors (of course f[l) and it is possible to make a mistake.

In fact, because we maintain the queue is a legal state interval, so the current illegal state should not enter the team, but should be in each update f[i] before the f[i-2*a] into the team, so that all the nodes in the queue is a legitimate state.

However!! There was a loophole in that practice! Because we will jump when we meet the cows ' love zone! A To! So some legal status will be too late to enter the team (such as the data I gave the f[6] ... So in the case of cows in the range of the time to put all the legal status in the team (of course, to maintain the queue monotonicity ...) Need to play the tail of the team)

1 /**************************************************************2 problem:19863 User:tunix4 language:c++5 result:accepted6 time:40 Ms7 memory:9092 KB8 ****************************************************************/9  Ten //POJ 2373 One#include <cmath> A#include <vector> -#include <cstdio> -#include <cstring> the#include <cstdlib> -#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) + #defineF (i,j,n) for (int i=j;i<=n;++i) - #defineD (i,j,n) for (int i=j;i>=n;--i) + #definePB Push_back A using namespacestd; at intGetint () { -     intv=0, sign=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -     returnv*=Sign ; - } in Const intn=1e6+Ten, inf=~0u>>2; -typedefLong LongLL; to /******************tamplate*********************/ + //#define DEBUG - structcow{ the     intx, y; * Cow () {} $     BOOL operator< (ConstCow &b)Const{Panax Notoginseng         returnx<b.x | | (x==b.x && y<b.y); -     } the}cow[1010]; + intf[n],n,l,a,b; A intQ[n]; the intMain () { + #ifndef Online_judge -Freopen ("2373.in","R", stdin); $ //freopen ("2373.out", "w", stdout); $ #endif -N=getint (); L=getint (); A=getint (); b=getint (); -F (I,1, n) cow[i].x=getint (), cow[i].y=getint (); theSort (cow+1, cow+n+1); - #ifdef DebugWuyiF (I,1, N) printf ("%d%d\n", cow[i].x,cow[i].y); thecout <<Endl; - #endif Wu     intj=1; -F (I,1, l) f[i]=INF; About     intst=0, ed=0; $f[0]=0; -q[ed++]=0; -      for(intI=2; i<=l;i+=2){ -          while(i>cow[j].x && j<=N) { A             intlast=i; +I=max (i, (cow[j].y+1)/2*2), J + +; the              for(inti=last;i<=i;i+=2) -                 if(f[i-2*a]!=INF) { $                      while(St<ed && f[q[ed-1]]>f[i-2*a]) ed--; theq[ed++]=i-2*A; the                 } the         } the          while(St<ed && q[st]<i-2*B) st++; -         if(f[i-2*a]!=INF) { in              while(St<ed && f[q[ed-1]]>f[i-2*a]) ed--; theq[ed++]=i-2*A; the         } About         if(St<ed && i-q[st]>=2*a) f[i]=f[q[st]]+1; the     } the #ifdef Debug theF (I,1, l) printf ("%d", F[i]==inf? -1: F[i]); +cout <<Endl; - #endif theprintf"%d\n", F[l]==inf? -1: F[l]);Bayi     return 0; the}
View Code

"Bzoj" "1986" "Usaco 2004 DEC"/"POJ" "2373" zoned area irrigation

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.