"BZOJ1669" D Hungry Cattle [Usaco2006 Oct] Hungry Cows Bull Holiday Team Competition 5 (LIS, discrete tree array)

Keywords: PHP less iOS

Links: https://ac.nowcoder.com/acm/contest/984/D
Source: Niuke.com

Hungry cattle
Time limit: C/C++ 1 second, 2 seconds for other languages
Space limitations: C/C++ 32768K, other languages 65536K
64bit IO Format: %lld
Title Description
Farmer John has N (1 <= N <= 5,000) cows, each with a positive integer number of no more than 32 binary digits.FJ expects cows to line up numbered from smallest to largest before eating, but cows never listen to him.In order for dairy cows to develop this habit, the FJ picks out some of the cows sequentially at each meal, and their numbers must be incremented in the order they are picked out.Then the FJ lets the picked cows eat --- the other cows have to be hungry.
Now you get the number of all the cows in the queue before and after this meal.The cows want to ask you to calculate how many cows can eat at most according to the FJ regulations?
For example, 11 cows are queued in the following order (numbers represent cow numbers)
2 5 18 3 4 7 10 9 11 8 15
For this queue, up to seven cows can be fed, numbered 2,3,4,7,10,11,15, respectively.Queue 2,5,3,10,15 is illegal because the number of the third cow (3) is less than the number of the cow before her (5).
Enter a description:
Line 1: An integer, N
Line 2.?: Except for the last line, each line contains exactly 20 integers separated by spaces, which in turn represent the number of the cows in the queue from front to back.If N cannot divide by 20, the last line contains fewer than 20 numbers
Output description:
Line 1: Output the maximum number of dairy cows that can be picked as specified by the FJ
Example 1
input
copy
11
2 5 18 3 4 7 10 9 11 8 15
output
copy
7

Topic:

Ideas:

Obviously, LIS is the longest ascending subsequence.
The data range can be passed by nn, but I'm writing nlogn,
And it uses the tree array method, not the dichotomy.Use as a knockboard to train your hand speed.
The data range is large and needs to be discretized if bit is used.

See code for details:

#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\n",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), '\0', 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_text\code_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 == '\n');
    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';
        }
    }
}



Posted by dude81 on Wed, 10 Jul 2019 10:28:02 -0700