HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列。
题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新时不要忘记加1,因为当前元素本身也是一个子序列,比如数列离散后为1 3 2 4 5,那么第一位得到之前的答案为0,更新时1位加1,第二位算出为1,更新时3位加(1+1),第三位也一样,一次类推,同树状数组求逆序对的方法一样,但是更新的不是1,而是之前所有的答案数加1。
#include <iostream>
#include <cstdio>
using namespace std;
const int N=100005;
const int mod=1000000007;
struct node{int id,v;}a[N];
int b[N],c[N],n;
bool cmp(node a,node b){return a.v<b.v;}
int sum(int x){int s=0;while(x>0)s+=c[x],s%=mod,x-=x&-x;return s;}
void updata(int x,int num){while(x<=n)c[x]+=num,c[x]%=mod,x+=x&-x;}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<=n;i++)b[i]=c[i]=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i].v);
a[i].id = i;
}
sort(a+1,a+n+1,cmp);
b[a[1].id]=1;
for(int i=2;i<=n;i++){
if(a[i].v!=a[i-1].v)b[a[i].id]=i;
else b[a[i].id]=b[a[i-1].id];
}
for(int i=1;i<=n;i++)updata(b[i],sum(b[i])+1);
printf("%d\n",sum(n));
}
return 0;
}
HDU 2227 Find the nondecreasing subsequences的更多相关文章
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 2227 Find the nondecreasing subsequences (线段树)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences(DP)
Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...
- HDU 2227 Find the nondecreasing subsequences (数状数组)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)
Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...
- HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
题意: 给你一个序列a[],求它的不降子序列的个数 分析: dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]); ...
- HDU2227Find the nondecreasing subsequences(树状数组+DP)
题目大意就是说帮你给出一个序列a,让你求出它的非递减序列有多少个. 设dp[i]表示以a[i]结尾的非递减子序列的个数,由题意我们可以写出状态转移方程: dp[i] = sum{dp[j] | 1&l ...
随机推荐
- Oracle中的日期和字符串互相转换
转载出处:http://blog.sina.com.cn/s/blog_44a005380100k6rv.html TO_DATE格式(以时间:2007-11-02 13:45:25为例) ...
- 浅谈Mybatis(二)
一.resultMap 作用:发现数据库的查询结果与实体之间不匹配时,需要通过ResultMap来进行映射处理.常用于多表查询. 多表查询还是比较复杂的,因为可能的情况很多.这里只说两种情况: 1.1 ...
- windows下nginx+tomcat分布式集群部署
首先官网下载 http://nginx.org/en/download.html,我的本地环境为 实现的架构: 从图上可以看出,nginx作为负载均衡请求分发器,当请求A应用时候,分发到A集群,同理 ...
- 从零开始PHP学习 - 第五天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- laravel安装说明
1.composer已安装.没安装可以看我上一篇 :http://www.cnblogs.com/seakt/p/4503249.html 2.配置环境变量,找到composer的位置,一般默认都在 ...
- js传参java接收乱码解决方案
js传参处理 encodeURI(encodeURI(name)); java接收处理 URLDecoder.decode(request.getParameter("name") ...
- 07-2. A+B和C (15)
给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=10),是测试用例的个数.随后给出T组测试用例,每组占一行,顺序给出A.B ...
- Category、Extension
Category,分类,类目.主要作⽤用是为没有源代码的添加方法,例系统自带的NSString. 通过Category添加的方法会成为原类的一部分.从⽽而达到扩展一 个类的功能. Category ...
- RedHat升级内核成功
升级前 uname -aLinux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x8 ...
- :last-child的诡异的问题!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...