Bzoj 1314:river River crossing Priority queue

Source: Internet
Author: User

1314:river River Time limit:10 Sec Memory limit:162 MB
submit:26 Solved:10
[Submit] [Status] [Discuss] Descriptionzy with n small kid across the river, small kid into two: Senior, sophomore grade, because there is a generation gap problem, if the same boat on the first grade students and sophomore grades of the difference more than K, there will be an disharmony event. Of course, if all the same grade on a ship, There will never be a quarrel. Now the Zy in the order of the small kid queue to arrange the boat, and can not let them cross the river in dispute. For the kid who is waiting to get on the boat, either let him on board, or the boat that stops at the ferry, and let him go to another boat, The man in the same boat is more than M. In order to get all the little kid to cross the river, in the case of the small kid queue, the minimum number of boats required. The first line of Input gives n,m,k. The meaning of the following line n lines used to describe the small kid's queue, one character per line "a" or "L" output minimum required number of ship sample Input5 4 1
A
L
L
L
A
Sample Output2
HINT

The first three people a boat, the last two people a boat
Data range
30% data in 1<=n<=1000
100% data in 1<=n<=250000,1<=m,k<=n

This problem is so few people are not normal AH.

It is easy to see, if the input sequence to +1/-1, and then do the prefix and, the current dp[i] can be j-i>=m and abs (A[j]-a[i]) <=k a rectangle within the DP value of the region, first kdtree certainly no problem, see there is no simple point of practice, Thinking about the whole two points, found not very good processing, notice that the above rectangular region has a fixed width, and the x-coordinate monotonically increment, so it is easy to think of each a[i] maintenance priority queue.

With Deque<int> to MLE, the reason is that deque encapsulated a lot of maintenance of the volume of access to the subscript, and changed to list<int> memory is much better.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<deque>#include<list>using namespacestd;#defineMAXN 251000#defineLCH (now<<1)#defineRCH (now<<1^1)#defineSmid ((l+r) >>1)#defineMaxt maxn*4#defineINF 0x3f3f3f3fintA[MAXN];intdp[maxn];list<int>LST[MAXN];intSgt[maxt];voidModify_sgt (intNowintLintRintPosintv) {        if(l==R) {Sgt[now]=v; return ; }        if(pos<=Smid)        Modify_sgt (LCH,L,SMID,POS,V); ElseModify_sgt (Rch,smid+1, r,pos,v); Sgt[now]=min (Sgt[lch],sgt[rch]);}intQuery_sgt (intNowintLintRintXinty) {        if(L==x && r==y)returnSgt[now]; if(y<=Smid)returnQuery_sgt (lch,l,smid,x,y); Else if(smid<x)returnQuery_sgt (rch,smid+1, R,x,y); Else                returnMin (Query_sgt (lch,l,smid,x,smid), Query_sgt (rch,smid+1, r,smid+1, y));} List<int>que;intMain () {Freopen ("Input.txt","R", stdin); intn,m,t,x,y,z; scanf ("%d%d%d\n",&n,&m,&t); Charch; intLevel=0; intmxvvv=0; memset (Sgt,inf,sizeof(SGT)); a[0]=0;  for(intI=1; i<=n;i++) {scanf ("%c\n",&ch); if(ch=='A') A[i]=1; ElseA[i]=-1; }         for(intI=1; i<=n;i++) A[i]+=a[i-1];  for(intI=0; i<=n;i++) level=min (a[i],level);  for(intI=0; i<=n;i++) MXVVV=Max (A[I],MXVVV); level=-Level ; MXVVV+=Level ;  for(intI=0; i<=n;i++) A[i]+=Level ; dp[0]=0; lst[a[0]].push_back (0); Modify_sgt (1,0, mxvvv,a[0],dp[lst[a[0]].front ()]); Que.push_back (1);  for(intI=1; i<=n;i++) {Dp[i]=query_sgt (1,0, Mxvvv,max (0, a[i]-t), Min (mxvvv,a[i]+t)) +1; if(Que.size () && A[que.front ()]-a[que.front ()-1]==a[i]-a[i-1]) Dp[i]=min (Dp[i],dp[que.front ()-1]+1);  while(Lst[a[i]].size () && dp[lst[a[i]].back ()]>=Dp[i]) lst[a[i]].pop_back ();                Lst[a[i]].push_back (i); Modify_sgt (1,0, Mxvvv,a[i],dp[lst[a[i]].front ()]); if(i>=m) {if(Lst[a[i-m]].size () && Lst[a[i-m]].front () ==i-m) {lst[a[i-m]].                                Pop_front (); if(lst[a[i-m]]. Size ()) Modify_sgt (1,0, mxvvv,a[i-m],dp[lst[a[i-m]].                                Front ()]); ElseModify_sgt (1,0, mxvvv,a[i-M],inf); }                }                if(i==1|| a[i]-a[i-1]==a[i+1]-A[i]) {                         while(!que.empty () && dp[que.back ()-1]>=Dp[i]) que.pop_back (); Que.push_back (i+1); }Else                {                         while(!que.empty ()) Que.pop_back (); Que.push_back (i+1); }                if(I-que.front () +1>=m) Que.pop_front (); } printf ("%d\n", Dp[n]);}

Bzoj 1314:river River crossing Priority 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.