HDU3450_Counting Sequences

题意:

让你从所给的序列中找到他的子序列,使他们相邻之间差距不超过d,问有多少个转移的子序列


分析:

这题第一眼大概就知道是状态转移,sum[i]表示以前i个中有多少个,那么sum[i+1]比sum[i]

多了一个以第i+1为结尾的子序列,那么只需要知道前面当中以x(x与第i+1距离不超过d)结尾的子序列个数和,那么这个时候在用dp[x]表示当前以x结尾有多少个子序列,但是数字太大不能直接记录,直接求和.

所以需要在状态转移时候运用到一些技巧,树状数组(也可以用线段树)和离散化;

先读入所以数字,然后排序编号,并用树状数组维护


实现:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>

using namespace std;
const int maxn = 2e5+100;
const int MOD = 9901;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
#define ll long long

ll cnt;
map<ll,ll> ID;
ll a[maxn],b[maxn],dp[maxn],n;
void getID(ll x) {
    if(!ID.count(x)) {
        ID[x] = ++cnt;
    }
}
ll lowbit( ll x )
{
    return x & (-x);
}

void add(ll x,ll d)
{
    while( x <= n)
    {
        dp[x] = (dp[x] + d)%MOD;;
        x += lowbit(x);
    }
}

ll sum (ll x)
{
    int ans = 0;
    while(x)
    {
        ans = (ans + dp[x])%MOD;
        x -= lowbit(x);
    }
    return ans%MOD;
}

int main(){
#ifdef LOCAL
    freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
  //freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
 #endif
     ll d;
    while( cin >> n >> d) {
        ID.clear();cnt = 0;
        memset(dp,0,sizeof dp);
        for(int i = 0; i  < n; ++i) {
            scanf("%lld", &a[i]);
            b[i] = a[i];
        }
        sort(b,b+n);
        for(int i = 0; i < n; ++i) getID(b[i]);
        for(int i = 0; i < n; i++) {
            int l = lower_bound(b,b+n,a[i] - d) -b;
            int r = upper_bound(b,b+n,a[i] + d) - b-1;
            //if(r == l)
            l = ID[b[l]],r = ID[b[r]];
            ll num = (sum(r) - sum(l-1) +1)%MOD;
            add(ID[a[i]],num );
        }
        cout << (sum(cnt) + 20*MOD- n)%MOD << endl;

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值