# [Usaco2008 Feb][BZOJ1609] Eating Together麻烦的聚餐

## 1609: [Usaco2008 Feb]Eating Together麻烦的聚餐

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1093  Solved: 655
[Submit][Status][Discuss]

## Sample Input

5
1
3
2
1
1

NlogN求最长不上升和不下降子序列，取最大值与N作差即可。8Ms水过。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
{
char c=getchar();
int a=0;
while (c<'0'||c>'9') c=getchar();
while (c>='0'&&c<='9')
{
a=a*10+c-'0';
c=getchar();
}
return a;
}
int n,d[30001],top,stack[30001],la,lb;
int main()
{
for (int i=1;i<=n;i++) d[i]=read();
top=0;
stack[0]=-1;
for (int i=1;i<=n;i++)
{
if (d[i]>=stack[top]) stack[++top]=d[i];
else
{
int l=1,r=top;
while (l<=r)
{
int mid=(l+r)>>1;
if (d[i]>=stack[mid]) l=mid+1; else r=mid-1;
}
stack[l]=d[i];
}
}
la=n-top;
top=0;
stack[0]=10000007;
for (int i=1;i<=n;i++)
{
if (d[i]<=stack[top]) stack[++top]=d[i];
else
{
int l=1,r=top;
while (l<=r)
{
int mid=(l+r)>>1;
if (d[i]<=stack[mid]) l=mid+1; else r=mid-1;
}
stack[l]=d[i];
}
}
lb=n-top;
printf("%d",min(la,lb));
return 0;
}

posted @ 2015-07-19 08:52  ws_fqk  阅读(203)  评论(0编辑  收藏  举报