# BZOJ【1609】 麻烦的聚餐

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

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 1198  Solved: 711
[Submit][Status][Discuss]

5
1
3
2
1
1

1

## Source

 1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<cstdlib>
7 #define maxn 30000+100
8 #define inf 0x7fffffff
9 using namespace std;
10 int a[maxn],f[maxn];
11 int main()
12 {
13     int n,ans=-inf;
14     cin>>n;
15     for(int i=1;i<=n;++i) f[i]=1;
16     for(int i=1;i<=n;++i) scanf("%d",&a[i]);
17     for(int i=2;i<=n;++i)
18       for(int j=1;j<i;++j)
19        if((a[i]>=a[j])&&(f[i]<f[j]+1))
20        {
21           f[i]=f[j]+1;
22           if(f[i]>ans) ans=f[i];
23        }
24     //for(int i=1;i<=n;++i) printf("%d\n",f[i]);
25     for(int i=1;i<=n;++i) f[i]=1;
26     for(int i=n-1;i>=1;--i)
27       for(int j=n;j>i;j--)
28         if((a[i]>=a[j])&&(f[i]<f[j]+1))
29         {
30             f[i]=f[j]+1;
31             if(f[i]>ans) ans=f[i];
32         }
33     cout<<n-ans;
34 }
View Code

nlogn

 1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 #define maxn 4100000
7 using namespace std;
8 int a[maxn],b[maxn],c[maxn];
9 int main()
10 {
11     int n,len1,len2;
12     cin>>n;
13     for(int i=1;i<=n;++i) scanf("%d",&a[i]);
14     c[0]=-1;c[1]=a[1];len1=1;
15     for(int i=2;i<=n;++i)
16     {
17         if(a[i]>=c[len1]) c[++len1]=a[i];
18         else
19         {
20             int pos=upper_bound(c+1,c+len1+1,a[i])-c;
21             c[pos]=a[i];
22         }
23     }
24     for(int i=1;i<=n;++i) c[i]=-1;
25     c[0]=-1;c[1]=a[n];len2=1;
26     for(int i=n-1;i>=1;--i)
27     {
28         if(a[i]>=c[len2]) c[++len2]=a[i];
29         else
30         {
31             int pos=upper_bound(c+1,c+len2+1,a[i])-c;
32             c[pos]=a[i];
33         }
34     }
35     int ans=max(len1,len2);
36     cout<<n-ans;
37 } 
View Code

posted @ 2015-10-13 11:47  Maydaytyh  阅读(329)  评论(0编辑  收藏  举报