BZOJ 1609 Usaco Eating Together

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1609 Usaco Eating Together相关的知识,希望对你有一定的参考价值。

看完题目第一眼的感觉就是求一个最长不上升子序列 和 最长不下降子序列
O(N^2)一定是跑不过去的 所以要写个O(NlogN)的算法
结果成功写挫~
思维难度不大,注意二分容易写爆炸。

 

#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
int n;
int num[30005],b[30005];
int Max,MAX;
 
void DOWN(){
    Max = 1;
    int j;
    b[Max] = num[Max];
    for(int i=2;i<=n;i++){
        if(num[i]<=b[Max]) j = ++Max;
        else{
            int l = 1;
            int r = Max+1;
            while(l<r){
                int m = (l+r)>>1;
                if(b[m]>=num[i]) l = m+1;
                else r = m;
            }
            j = l;
        }
        b[j] = num[i];
    }
    /*for(int i=1;i<=Max;i++){
        printf("b[%d]:%d\n",i,b[i]);
    }*/
    MAX=Max;
}
 
void UP(){
    memset(b,0,sizeof(b));
    Max = 1;
    int j ;
    b[Max] = num[Max];
    for(int i=2;i<=n;i++){
        if(num[i]>=b[Max]) j=++Max;
        else{
            int l = 1;
            int r = Max+1;
            while(l<r){
                int m = (l+r)>>1;
                if(b[m]<=num[i]) l = m+1;
                else r = m;
            }
            j= l;
        }
        b[j] = num[i];
    }
    /*for(int i=1;i<=Max;i++){
        printf("b[%d]:%d\n",i,b[i]);
    }*/
    MAX = max(Max,MAX);
}
 
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&num[i]);
    DOWN();
    UP();
    printf("%d\n",n-MAX);
    return 0;
}

 

以上是关于BZOJ 1609 Usaco Eating Together的主要内容,如果未能解决你的问题,请参考以下文章

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

BZOJ 1609: [Usaco2008 Feb]Eating Together

[BZOJ1609] [Usaco2008 Feb] Eating Together麻烦的聚餐 (dp)

BZOJ_1609_[Usaco2008_Feb]_Eating_Together_麻烦的聚餐_(LIS)

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

BZOJ 1609 [Usaco2008 Feb]Eating Together麻烦的聚餐:LIS & LDS (nlogn)