【HDU】5141 LIS again 在普通nlogn求LIS的基础上稍作修改

传送门:【HDU】5141 LIS again


题目分析:我们枚举所有点R作为终点,求【1,R】内有多少个符合条件的L使得【L,R】的lis等于【1,n】的LIS。为每个终点R,找到能得到LIS的最右端的起点L,这样从1~L这段区间内的点都是可以和R构成lis等于LIS的合法区间的。这个我们就求LIS的时候顺便保存二分得到的数组中每个位置对应最右端的起点,然后插入或替换点的时候就从上一个位置推过来,同时记录此时该长度下的合法区间个数,当更新LIS长度时,记录下的合法区间重置。


代码如下:


#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define rt o , l , r
#define mid ( ( l + r ) >> 1 )

const int MAXN = 100005 ;

int cnt[MAXN] ;
int s[MAXN] , top ;
int n ;

int search ( int x , int l , int r ) {
	while ( l < r ) {
		int m = mid ;
		if ( s[m] >= x ) r = m ;
		else l = m + 1 ;
	}
	return l ;
}

void scanf ( int& x , char c = 0 ) {
	while ( ( c = getchar () ) < '0' ) ;
	x = c - '0' ;
	while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ;
}

void solve () {
	int ans = 0 , t = 0 ;
	int x ;
	top = 0 ;
	For ( i , 1 , n ) {
		scanf ( x ) ;
		if ( !top || s[top] < x ) {
			s[++ top] = x ;
			if ( top == 1 ) cnt[1] = i ;
			else cnt[top] = cnt[top - 1] ;
			ans = cnt[top] ;
			t = 0 ;
		} else {
			int y = search ( x , 1 , top ) ;
			if ( y == 1 ) cnt[1] = i ;
			else cnt[y] = cnt[y - 1] ;
			s[y] = x ;
			ans += cnt[top] ;
			if ( ans >= 1000000000 ) {
				ans -= 1000000000 ;
				++ t ;
			}
		}
	}
	printf ( "%I64d\n" , ans + t * 1000000000LL ) ;
}

int main () {
	while ( ~scanf ( "%d" , &n ) ) solve () ;
	return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值