HDU_2227_FindTheNondecreasingSubsequences

9 篇文章 0 订阅
4 篇文章 0 订阅

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
  
  
3 1 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值