hdu 2227(树状数组+dp)
2024-09-04 11:10:20
Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1844 Accepted Submission(s): 677
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}.
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.
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.
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
1 2 3
Sample Output
7
题意:找到一个字符串里面所有的不下降子序列的数量之和。
题解:设dp[i] 以a[i]结尾的非递减子串的个数,可以知道 dp[i] = sum(dp[j])+1 (1<=j<i,a[j]<a[i]),这里a[j]<a[i]我们可以直接利用求解逆序数的原理得到,利用树状数组维护整个递推式。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
const int mod = ;
int n;
int a[N],b[N],c[N];
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=n;i+=lowbit(i)){
c[i]=(c[i]+v)%mod;
}
}
int getsum(int idx){
int sum = ;
for(int i=idx;i>=;i-=lowbit(i)){
sum = (sum+c[i])%mod;
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF){
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(b+,b++n);
int ans = ;
for(int i=;i<=n;i++){
int idx = lower_bound(b+,b++n,a[i])-b;
ans=getsum(idx);
update(idx,ans+);
}
printf("%d\n",getsum(n));
}
return ;
}
最新文章
- mysql 5.7.15单机主从快速搭建并配置复制表到不同库
- 简单的js菜单
- js部分总结
- socketserver模块写的一个简单ftp程序
- mybatis实战
- Jetty 与 Tomcat 比较,及性能分析
- axure rp pro 6.5
- ANDROID_MARS学习笔记_S01原始版_010_ContentProvider
- springboot 1.5.2 集成kafka 简单例子
- python之路第四篇(基础篇)
- Android动态加载技术(插件化技术)
- Script error.解决方法
- Visual Stuido Online:如何禁止多人同时签出同一文件
- jquery 語法
- [TensorBoard] Name &; Variable scope
- [转]Hadoop Hive sql语法详解
- 《C++ Primer Plus》16.2 智能指针模板类
- ZooKeeper 集群的安装、配置---Dubbo 注册中心
- WeX5入门之欢乐捕鱼打包
- haproxy有关session的问题