hdu2227---Find the nondecreasing subsequences(dp+树状数组)

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: 2642 2688 1541 3030 3015

dp[i]表示以第i个元素结尾的不下降序列个数

dp[i]=∑i?1j=1 dp[j] +1

n达10万,所以用树状数组来优化

/*************************************************************************
    > File Name: hdu2227.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年06月02日 星期二 18时33分24秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 100010;
static const int mod = 1000000007;
LL tree[N];

int lowbit(int x) {
    return x & (-x);
}

void add(int n, int x, LL val) {
    for (int i = x; i <= n; i += lowbit(i)) {
        tree[i] += val;
        tree[i] %= mod;
    }
}

LL sum(int x) {
    LL ans = 0;
    for (int i = x; i; i -= lowbit(i)) {
        ans += tree[i];
        ans %= mod;
    }
    return ans;
}

LL dp[N];
int Arr[N];
int xis[N];
int cnt;

int search(int val) {
    int l = 1, r = cnt;
    int mid;
    while (l <= r) {
        mid = (l + r) >> 1;
        if (xis[mid] == val) {
            break;
        }
        if (xis[mid] > val) {
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return mid;
}

int main() {
    int n;
    while (~scanf("%d", &n)) {
        cnt = 0;
        memset(tree, 0, sizeof(tree));
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &Arr[i]);
            xis[++cnt] = Arr[i];
        }
        sort(xis + 1, xis + cnt + 1);
        cnt = unique(xis + 1, xis + cnt + 1) - xis - 1;
        memset(dp, 0, sizeof(dp));
        LL ans = 0;
        for (int i = 1; i <= n; ++i) {
            int val = search(Arr[i]);
            LL Sum = sum(val);
            dp[i] = 1 + Sum;
            dp[i] %= mod;
            add(n, val, dp[i]);
            ans += dp[i];
            ans %= mod;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-10-25 22:46:59

hdu2227---Find the nondecreasing subsequences(dp+树状数组)的相关文章

HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

题目链接: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)                                             

codeforces 597C C. Subsequences(dp+树状数组)

题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is

树形DP+树状数组 HDU 5877 Weak Pair

1 //树形DP+树状数组 HDU 5877 Weak Pair 2 // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 3 // 这道题要离散化 4 5 #include <bits/stdc++.h> 6 using namespace std; 7 #define LL long long 8 typedef pair<int,int> pii; 9 const double inf = 12345678901234

poj 3616 Milking Time dp+树状数组

题意: 给一堆区间,每个区间有开始时间s,结束时间e,和收益w,现在要找一些区间使收益和最大,且区间之间的间隔最小为r. 分析: 这道题用dp做是简单题,用dp+树状数组做是中等题.dp问题的关键是对状态的定义.有两种方法,一:dp[k]表示按开始时间排序到第k个区间能取得的最大收益.二:dp[t]表示在时间t时能获得的最大收益.定义好状态方程就好写了这不再赘述.有趣的是这个时间复杂度.设一共有M个区间,所有区间的最大时间为L,第一种是M^2的,第二种是M*(logL+logM)的,这题M才10

奶牛抗议 DP 树状数组

奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i]-sum[j]\ge0) \] \(O(n^2)\)过不了,考虑优化 移项得: \[ f[i]=\sum f[j]\;(j< i,sum[i]\ge sum[j]) \] 这时候我们发现相当于求在\(i\)前面并且前缀和小于\(sum[i]\)的所有和,这就可以用一个树状数组优化了,在树状数组维护下标为

hdu 2227(dp+树状数组)

题目链接: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): 1466    Accepted Submission(s): 521 Problem Description

hdu 4991(dp+树状数组)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4991 Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 221    Accepted Submission(s): 112 Problem Description A numeric sequence

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

第十四个目标(dp + 树状数组)

Problem 2236 第十四个目标 Accept: 17    Submit: 35 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 目暮警官.妃英里.阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶.在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关. 为了避免