1609: [Usaco2008 feb]eating together trouble dinner time limit:10 Sec Memory limit:64 MB
submit:1093 solved:655
[Submit] [Status] [Discuss] Description
To avoid overcrowding in restaurants, FJ asked cows to eat in 3 batches. Every day before dinner, the cows will queue up in front of the restaurant, according to FJ's assumption that all the 3rd batch of cows at the end of the queue, the front of the team from the 1th batch of eating cows occupy, in the middle of the location is the 2nd batch of the cows to eat. As the cows did not understand FJ's arrangement, the queue before supper became a big hassle. The first cow has a card indicating her meal lot d_i (1 <= d_i <= 3). Although all N (1 <= n <= 30,000) cows were lined up in neat teams, everyone could see that the numbers on the cards were completely disorganized. After several chaotic re-queues, FJ found an easy way: the cows did not move, and he walked down the line to get rid of the numbers on the cow card he thought was the wrong team, and finally got a queue where the cows in each group he wanted were standing together. For example 111222333 or 333222111. Well, as you can see, FJ does not oppose a front-and-back queue, so he could turn all cows backwards and enter the restaurant in normal order. You know, FJ is a very lazy person. He wanted to know that if he wanted to achieve his goal, he would have to change the number of the cows ' cards at least. All cows will not move their position when the FJ is changed to the card number.
Input
Line 1th: 1 integers: N 2nd. N+1 Line: Line i+1 is 1 integers for the first cow's meal lot d_i
Output
Line 1th: Output 1 integers, for FJ at least to change the number of cows on the card, so that the number can be changed into the way he imagined
Sample Input5
1
3
2
1
1
Input Description:
There are 5 cows in the queue, the 1th and the last 2 cows are set for the first meal, the 2nd cow is the third batch of meals, the 3rd head is the second batch of meals.
Nlogn the longest non-ascending and non-descending sub-sequence, the maximum value and n can be the difference. 8Ms of water over.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<Set>#include<map>#include<vector>#include<queue>using namespacestd;intRead () {CharC=GetChar (); intA=0; while(c<'0'|| C>'9') c=GetChar (); while(c>='0'&&c<='9') {a=a*Ten+c-'0'; C=GetChar (); } returnA;}intn,d[30001],top,stack[30001],la,lb;intMain () {n=read (); for(intI=1; i<=n;i++) d[i]=read (); Top=0; stack[0]=-1; for(intI=1; i<=n;i++) { if(D[i]>=stack[top]) stack[++top]=D[i]; Else { intL=1, r=top; while(l<=r) {intMid= (l+r) >>1; if(D[i]>=stack[mid]) l=mid+1;Elser=mid-1; } Stack[l]=D[i]; }} la=n-top; Top=0; stack[0]=10000007; for(intI=1; i<=n;i++) { if(D[i]<=stack[top]) stack[++top]=D[i]; Else { intL=1, r=top; while(l<=r) {intMid= (l+r) >>1; if(D[i]<=stack[mid]) l=mid+1;Elser=mid-1; } Stack[l]=D[i]; }} lb=n-top; printf ("%d", Min (la,lb)); return 0;}
[Usaco2008 Feb] [BZOJ1609] Eating together a troublesome dinner