(寒假集训) Cow Jog(二分优化的最长上升子数列)

Cow Jog

时间限制: 1 Sec  内存限制: 64 MB
提交: 24  解决: 5
[提交][状态][讨论版]

题目描述

Farmer John's N cows (1 <= N <= 100,000) are out exercising their hooves again, jogging along an infinite track.  Each cow starts at a distinct position on the track, and some cows run at different speeds.

The track is divided into lanes so that cows may move past each other. No two cows in the same lane may ever occupy the same position. Farmer John doesn't want any cow to have to change lanes or adjust speed, and he wonders how many lanes he will need to accomplish this if the cows are going to run for T minutes (1 <= T <= 1,000,000,000).

输入


The first line of input contains N and T.

The following N lines each contain the initial position and speed of a single cow.  Position is a nonnegative integer and speed is a positive integer; both numbers are at most 1 billion.  All cows start at distinct positions, and these will be given in increasing order in the input.

输出

A single integer indicating the minimum number of lanes necessary so that no two cows in the same lane ever occupy the same location (including at time T).

样例输入

5 3
0 1
1 2
2 3
3 2
6 1

样例输出

3
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 2e9
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 1e5+5;
const int M = 4e5+5;
ll m,t,v;
int cnt=0,n;
ll a[N],d[N],j,len;
int  LIS(ll *arr, int n){
    ll *dp=new ll [n+5];
    int  where, idx = 1;
    dp[idx] = arr[0];
    for (int i = 1; i < n; ++i){
        if (arr[i]>=dp[idx]){
            idx++;
            dp[idx] = arr[i];
        }
        else{
            where = upper_bound(dp+1, dp + idx+1, arr[i]) - dp;//非下降
            dp[where] = min(dp[where], arr[i]);
        }
    }
    delete[] dp;
    return idx;
}
 
int main() {
    scanf("%d%lld",&n,&t);
    for (int i = 1; i <=n; ++i) {
        scanf("%lld%lld",&m,&v);
        a[i]=m+t*v;
    }
    for(int i=n;i>0;i--){
        d[cnt++]=a[i];
    }
    printf ("%d
", LIS(d,n));
    return 0;
}
原文地址:https://www.cnblogs.com/jianrenfang/p/6270661.html