zoukankan      html  css  js  c++  java
  • HDU 2227 Find the nondecreasing subsequences (数状数组)

    Find the nondecreasing subsequences

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


    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
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int mod=1000000007;
    const int N=100010;
    
    struct node{
        int val,id;
    }a[N];
    
    int n,b[N],c[N],arr[N];
    
    int lowbit(int x){
        return x&(-x);
    }
    
    void update(int i,int val){
        while(i<=n){
            arr[i]+=val;
            arr[i]%=mod;
            i+=lowbit(i);
        }
    }
    
    int Sum(int i){
        int ans=0;
        while(i>0){
            ans+=arr[i];
            ans%=mod;
            i-=lowbit(i);
        }
        return ans;
    }
    
    int cmp(node a,node b){
        return a.val<b.val;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d",&n)){
            memset(b,0,sizeof(b));
            memset(arr,0,sizeof(arr));
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i].val);
                a[i].id=i;
            }
            sort(a+1,a+n+1,cmp);
            b[a[1].id]=1;
            for(int i=2;i<=n;i++)
                if(a[i].val==a[i-1].val)
                    b[a[i].id]=b[a[i-1].id];
                else
                    b[a[i].id]=i;
            for(int i=1;i<=n;i++){
                c[i]=Sum(b[i]);
                update(b[i],c[i]+1);
            }
            printf("%d
    ",Sum(n));
        }
        return 0;
    }
  • 相关阅读:
    正则表达式尽量写的精简,提高运行效率
    IndexError:string index out of range
    Python3 字符编码
    python3字符编码错误
    pip
    正则贪婪和非贪婪
    [Ss]+ 可以匹配多行html,最常用的还是.*?
    正则,分组,字符集,使用场景
    使用jodis连接codis的时候报异常:Exception in thread "main" redis.clients.jedis.exceptions.JedisException: Proxy list empty
    codis 的dashboard服务无法启动 提示pid已经运行
  • 原文地址:https://www.cnblogs.com/jackge/p/3169205.html
Copyright © 2011-2022 走看看