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

HDOJ 题目2227 Find the nondecreasing subsequences(树状数组,离散化,DP)

2015-11-02 20:44 495 查看


Find the nondecreasing subsequences

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1728 Accepted Submission(s): 631



Problem Description

How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.

Input

The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.

Output

For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.

Sample Input

3
1 2 3


Sample Output

7


Author

8600

Recommend

lcy | We have carefully selected several similar problems for you: 3450 2642 3030 3015 3016
ac代码

153526982015-11-02 18:35:20Accepted22271123MS7632K1190 BG++XY_
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
using namespace std;
int val[100010],t[100010];
int a[100010],cnt;
int lowbit(int x)
{
return x&(-x);
}
void add(int p,int val)
{
while(p<=cnt)
{
a[p]=(a[p]+val)%mod;
p+=lowbit(p);
}
}
int getsum(int x)
{
int ans=0;
while(x>0)
{
ans=(ans+a[x])%mod;
x-=lowbit(x);
}
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&val[i]);
t[i]=val[i];
}
sort(t+1,t+n+1);
cnt=unique(t+1,t+1+n)-(t+1);
map<int ,int>mp;
for(i=1;i<=cnt;i++)
{
mp[t[i]]=i;
}
int ans=0;
add(1,1);
for(i=1;i<=n;i++)
{
int x=getsum(mp[val[i]]);
// printf("%d\n",x);
ans=(ans+x)%mod   ;
add(mp[val[i]],x);
}
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: