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

hdu 2227 Find the nondecreasing subsequences

2011-10-30 09:32 411 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227

Find the nondecreasing subsequences

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 476 Accepted Submission(s): 186


[align=left]Problem Description[/align]
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}.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.

[align=left]Sample Input[/align]

3 1 2 3

[align=left]Sample Output[/align]

7

//找不下降子序列的个数

离散化+树状数组;

dp[a[j]]=sum( dp[a[i]] )+1; (i>=1 && i<j && a[j]>=a[i]) //单独的a[j]也算一个

因为a[i]<2^31,所以要先离散化一下,把所有数映射到1---n这个区间内。

code:

View Code

# include<stdio.h>
# include<string.h>
# include<stdlib.h>
# define N 100005
# define Mod 1000000007
int s
,n,a
;
__int64 dp
;
int cmp(const void *a,const void *b)
{
return *(int *)a - *(int *)b;
}
int find(int x)
{
int left,right,mid,ans;
left=1;
right=n;
while(right>=left)
{
mid=(right+left)/2;
if(a[mid]==x) {ans=mid;right=mid-1;}
else if(a[mid]>x) right=mid-1;
else left=mid+1;
}
return ans;
}
__int64 query(int i)
{
__int64 count;
count=0;
while(i>=1)
{
count+=dp[i];
count%=Mod;
i-=i&(-i);
}
return count;
}
void insert(int i,__int64 ans)
{
while(i<=n)
{
dp[i]+=ans;
dp[i]%=Mod;
i+=i&(-i);
}
}
int main()
{
int i,ans;
__int64 sum,num;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
a[i]=s[i];
}
qsort(a+1,n,sizeof(a[1]),cmp);
memset(dp,0,sizeof(dp));
sum=0;
for(i=1;i<=n;i++)
{
ans=find(s[i]);
num=query(ans)+1;
insert(ans,num);
sum+=num;
sum%=Mod;
}
printf("%I64d\n",sum%Mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: