HDU_2227_FindTheNondecreasingSubsequences

来源:互联网 发布:淘宝怎么看真假 编辑:程序博客网 时间:2024/05/18 02:33

Find the nondecreasing subsequences

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


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
31 2 3
 

Sample Output
7
 

Author
8600
 

Recommend
lcy

水题

坐标离散化一下

然后每个点的和应该是这个点之前的子串个数

因此每次add加的操作是加的之前所有满足条件子串的个数+1

1 2 3 4

如读到1 则前面没有 就0加1

读到2 则前面的有1 1+1=2;

读到3 则前面的有 1 2 1+2+1=4;

读到4 则前面的有 1 2 3 1+2+4+1=8;

因此总共有1+2+4+8=15

注意下数据量还有取模


#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;typedef long long LL;const int M=1e5+5;const int MO=1e9+7;LL tree[M];struct NODE{    int v,p;}node[M];bool cmp1(NODE a,NODE b){    if(a.v==b.v)        return a.p<b.p;    return a.v<b.v;     //因为输入没有保证数字不重复,防止排序和离散造成原本可以形成的序列被否定}bool cmp2(NODE a,NODE b){    return a.p<b.p;}inline int lowbit(int x){    return x&(-x);}LL getsum(int x){    LL s=0;    for(int i=x;i;i-=lowbit(i))        s=(s+tree[i])%MO;    return s;}void add(int x,LL v){    for(int i=x;i<M;i+=lowbit(i))        tree[i]=(tree[i]+v)%MO;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        memset(tree,0,sizeof(tree));        for(int i=0;i<n;i++)        {            scanf("%d",&node[i].v);            node[i].p=i;        }        sort(node,node+n,cmp1);        for(int i=0;i<n;i++)            node[i].v=i+1;        sort(node,node+n,cmp2);        for(int i=0;i<n;i++)            add(node[i].v,getsum(node[i].v)+1); //加的是之前满足条件数字情况的和        printf("%I64d\n",getsum(M-1));    }    return 0;}


0 0
原创粉丝点击