您的位置:首页 > 产品设计 > UI/UE

HDU - 2227 Find the nondecreasing subsequences

2014-04-13 15:34 417 查看
题意:求不递减的子序列的个数

思路:跟昨天那题HDU-3030不同的是,昨天的是严格的递增的子序列数,稍微修改一下就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int MOD = 1000000007;
const int MAXN = 100010;

ll arr[MAXN],n;
int num[MAXN],sortNum[MAXN];

int lowbit(int x){
	return x&(-x);
}

void update(int x,ll d){
	while (x <= n){
		arr[x] += d;
		arr[x] %= MOD;
		x += lowbit(x);
	}
}

ll sum(int x){
	ll ans = 0;
	while (x > 0){
		ans += arr[x];
		ans %= MOD;
		x -= lowbit(x);
	}
	return ans;
}

int main(){
	while (scanf("%lld",&n) != EOF){
		memset(arr,0,sizeof(arr));
		for (int i = 1; i <= n; i++){
			scanf("%d",&num[i]);
			sortNum[i] = num[i];
		}
		sort(sortNum+1,sortNum+1+n);
		for (int i = 1; i <= n; i++){
			int index = lower_bound(sortNum+1,sortNum+1+n,num[i])-sortNum;
			ll temp = sum(index);
			update(index,temp+1);
		}
		cout << sum(n) << endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: