Problem 2236 第十四个目标

本文介绍了一道关于寻找严格递增子序列数量的问题,并提供了一种使用线段树和动态规划结合的方法来解决该问题。通过实例说明了如何计算给定序列中严格递增子序列的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.fzu.edu.cn/problem.php?pid=2236

 Problem 2236 第十四个目标

Accept: 2    Submit: 3
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

目暮警官、妃英里、阿笠博士等人接连遭到不明身份之人的暗算,柯南追踪伤害阿笠博士的凶手,根据几起案件现场留下的线索发现凶手按照扑克牌的顺序行凶。在经过一系列的推理后,柯南发现受害者的名字均包含扑克牌的数值,且扑克牌的大小是严格递增的,此外遇害者与毛利小五郎有关。

为了避免下一个遇害者的出现,柯南将可能遭到暗算的人中的数字按关联程度排列了出来,即顺序不可改变。柯南需要知道共有多少种可能结果,满足受害人名字出现的数字严格递增,但是他柯南要找出关键的证据所在,所以这个任务就交给你了。

(如果你看不懂上面在说什么,这题是求一个数列中严格递增子序列的个数。比如数列(1,3,2)的严格递增子序列有(1)、(3)、(2)、(1,3)、(1,2),共5个。长得一样的但是位置不同的算不同的子序列,比如数列(3,3)的答案是2。)

 Input

多组数据(<=10),处理到EOF。

第一行输入正整数N(N≤100 000),表示共有N个人。

第二行共有N个整数Ai(1≤Ai≤10^9),表示第i个人名字中的数字。

 Output

每组数据输出一个整数,表示所有可能的结果。由于结果可能较大,对1 000 000 007取模后输出。

 Sample Input

31 3 2

 Sample Output

5
线段树,dp[i]表示以i结尾的序列有多少个,转移方程是dp[i]=sigma(dp[1],dp[i-1])+1,更新dp[i]区间和与更新用线段树

#include <cstdio>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int SIZE=1e5+10;
const int maxn=1<<30;
const int MOD=1e9+7;
int s[SIZE],X[SIZE];
struct sge{
    int sum;
}a[SIZE<<2];
int Bin(int key,int n,int X[]){
    int l=0,r=n-1;
    while(l<=r){
        int m=(l+r)>>1;
        if(X[m]==key)return m;
        if(X[m]>key)r=m-1;
        else l=m+1;
    }
    return -1;
}
void build(int l,int r,int rt){
    a[rt].sum=0;
    if(l==r)return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}
void pushup(int rt){
    a[rt].sum=(a[rt<<1].sum+a[rt<<1|1].sum)%MOD;
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R)return a[rt].sum;
    int m=(l+r)>>1;
    int sum=0;
    if(L<=m)sum=(sum+query(L,R,lson))%MOD;
    if(R>m)sum=(sum+query(L,R,rson))%MOD;
    return sum;
}
void update(int pos,int c,int l,int r,int rt){
    if(l==r){
        a[rt].sum=(a[rt].sum+c)%MOD;
        return ;
    }
    int m=(l+r)>>1;
    if(pos<=m)update(pos,c,lson);
    else update(pos,c,rson);
    pushup(rt);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&s[i]);
            X[i]=s[i];
        }
        sort(X,X+n);
        int cnt=1,ret=0,sum;
        for(int i=1;i<n;i++)
            if(X[i]!=X[i-1])X[cnt++]=X[i];
        build(0,cnt-1,1);
        for(int i=0;i<n;i++){
            int pos=Bin(s[i],cnt,X);
            if(pos==0)sum=0;
            else sum=query(0,pos-1,0,cnt-1,1);
            ret=(ret+sum+1)%MOD;
            update(pos,sum+1,0,cnt-1,1);
        }
        printf("%d\n",ret);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值