zoukankan      html  css  js  c++  java
  • 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)

    链接:https://ac.nowcoder.com/acm/contest/984/D
    来源:牛客网

    饥饿的牛
    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld
    题目描述
    Farmer John养了N(1 <= N <= 5,000)头奶牛,每头牛都有一个不超过32位二进制数的正整数编号。FJ希望奶牛们在进食前,能按编号从小到大的顺序排好队,但奶牛们从不听他的话。为了让奶牛们养成这个习惯,每次开饭时,FJ从奶牛中顺序地挑出一些,这些奶牛的编号必须按挑出的顺序递增。然后FJ让被挑出的奶牛们吃饭——其他奶牛就只能饿肚子了。
    现在,你得到了这一次开饭前队伍中从前到后所有奶牛的编号。奶牛们想请你计算一下,按照FJ的规定,最多有多少头奶牛能吃上饭?
    比如说,有11头奶牛按以下顺序排好了队(数字代表奶牛的编号)
    2 5 18 3 4 7 10 9 11 8 15
    对于这个队列,最多可以让7头奶牛吃上饭,她们的编号分别为2,3,4,7,10,11,15。队列2,5,3,10,15是不合法的,因为第3头奶牛的编号(3)小于她前面一头奶牛的编号(5)。
    输入描述:
    第1行: 一个整数,N
    第2..?行: 除了最后一行,每一行都包含恰好20个用空格隔开的整数,依次表 示队伍中从前到后的奶牛的编号。如果N不能整除20,那么最后一行包含的数字不到20个
    输出描述:
    第1行: 输出按照FJ的规定,最多可以挑出的奶牛的数目
    示例1
    输入
    复制
    11
    2 5 18 3 4 7 10 9 11 8 15
    输出
    复制
    7

    题意:

    思路:

    显然是求LIS, 即最长上升子序列。
    数据范围可以让nn过,但是我写的是nlogn ,
    而且用的是树状数组的方法,不是二分。当做敲板子训练手速了。
    数据范围很大,bit的话,需要离散化。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int tree[maxn];
    int n;
    int a[maxn];
    int ans=0;
    int lowbit(int x)
    {
         return x&(-x);
    }
    void modify(int id,int x)
    {
        while(id<maxn)
        {
            tree[id]=max(tree[id],x);
            id+=lowbit(id);
        }
    }
    int query(int id)
    {
        int res=0;
        id--;
        while(id)
        {
            res=max(res,tree[id]);
            id-=lowbit(id);
        }
        return res;
    }
    int b[maxn];
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
        //freopen("D:\common_textcode_stream\out.txt","w",stdout);
        gbtb;
        cin>>n;
        repd(i,1,n)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        repd(i,1,n)
        {
            a[i]=lower_bound(b+1,b+1+n,a[i])-b;
        }
        repd(i,1,n)
        {
            int x=query(a[i])+1;
            ans=max(ans,x);
            modify(a[i],x);
        }
        cout<<ans<<endl;
        
        
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    那些年读过的书《Java并发编程实战》和《Java并发编程的艺术》三、任务执行框架—Executor框架小结
    那些年读过的书《Java并发编程实战》二、如何设计线程安全类
    那些年读过的书《Java并发编程实战》一、构建线程安全类和并发应用程序的基础
    重读《深入理解Java虚拟机》七、探究Java内存模型
    重读《深入理解Java虚拟机》六、Java泛型 VS C#泛型 (伪泛型 VS 真泛型)
    重读《深入理解Java虚拟机》五、虚拟机如何执行字节码?程序方法如何被执行?虚拟机执行引擎的工作机制
    重读《深入理解Java虚拟机》四、虚拟机如何加载Class文件
    input 文本框禁止输入表情
    win7右下角无线网图标显示未连接,但是实际上已连接上,也能上网
    history.back(-1)和history.go(-1)的区别
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11167505.html
Copyright © 2011-2022 走看看