DP+树状数组——FZU 2236 第十四个目标

  • 题目链接: http://acm.fzu.edu.cn/problem.php?pid=2236

  • 题意:给出长度为N的数组,求一共有多少个严格递增子序列

  • 分析:我们很容易想到动态规划来解这道题,设定 DP[i] 为以第i个元素结尾的严格递增子序列的数量,那么 DP[i]=i1j=1DP[j]where(A[j]<A[i]) ,因为N的大小为1E5,如果直接这样求和肯定妥妥的超时,所以我们需要再优化一下。

  • 优化: 因为是严格递增子序列,所以我们可以先用vector存下元素后去重,方便之后的计算,并从小到大排序vector。然后我们按顺序遍历原数组,对于每一个值,二分搜索排序后的vector,得到这个值对应的大小位置loc,那么对于loc之前的所有位置求和,即求出之前所有值小于它并且出现在它前面的A[j]对应的DP[j]的和,然后+1再更新到loc里。

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
#include <climits>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100005
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
const double eps = 1e-6;
#define bug cout<<88888888<<endl;
#define debug(x) cout << #x" = " << x;
int N;
int A[MaxN];
vector<int> V;
struct BIT{
    int n;
    int tree[MaxN];
    void init( int n ){
        this->n = n ;
        CLR(tree);
    }

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

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

    void update( int x, int val ){
        for( int i = x; i <= n; i += lowbit( i ) ){
            tree[i] = ( tree[i] + val ) % mod ;
        }
    }
}bit;
void solve()
{
    bit.init(N);
    for(int i=1;i<=N;i++)
    {
        int loc = lower_bound(V.begin(),V.end(),A[i])-V.begin()+1;
        int val = (bit.sum(loc-1)+1)%mod;
        bit.update(loc,val);
    }
    printf("%d\n",bit.sum(N));
}
int main()
{
    //std::ios::sync_with_stdio(false);
    while(~scanf("%d", &N))
    {
        V.clear();
        for(int i=1;i<=N;i++) 
        {
            scanf("%d", &A[i]);
            V.push_back(A[i]);
        }
        sort(V.begin(), V.end());
        V.erase(unique(V.begin(),V.end()), V.end());
        solve();
    }
    //system("pause");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值