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 ...
随机推荐
- Excel表格中汉字转拼音
一.使用“实用汉字转拼音V4.8” 软件 下载地址http://www.orsoon.com/soft/4413.html 或则百度 很多的 二.Excel自定义函数方法: 1.启动Excel 200 ...
- iOS手机号正则表达式并实现344格式 (正则的另一种实现方式)
[Demo下载地址]https://git.oschina.net/remainedmute/PhoneNumDemo.git 相关博客http://www.jianshu.com/p/00da4d8 ...
- 理解ROS话题
首先需要打开一个终端在里面运行roscore: roscore 再打开一个终端,在里面运行一个turtlesim_node节点: rosrun turtlesim turtlesim_node 打开另 ...
- 在Eclipse中使用Maven构建SpringMVC项目
环境搭建 安装JDK, Eclipse, Tomcat等 – 请参考网上常见攻略. 安装Maven: 下载需要的Maven 版本( http://maven.apache.org/download.c ...
- 基于jQuery带标题的图片3D切换焦点图
今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...
- (转)C++笔记:面向对象编程基础
非常棒的总结 http://blog.csdn.net/liufei_learning/article/details/21312701 面向对象编程基础 面向对象编程基于三个基本概念: 数据抽象-类 ...
- nyoj 138 找球号(二)(哈希)
题目:nyoj——138 /*** 哈希求解...采用链表保存 插入时,可以去除重复 查找 找到该组,然后在改组的查找 当这个组不存在时或是没有找到时是 NO 其他是YES 1e6+1 时间最短 */ ...
- Linux 安装g++
g++ 它的全名不叫g++而是叫gcc-c++; 所以要安装它就可以用 yum install gcc-c++;
- 转:CI伪静态化
去掉php框架CI默认url中的index.php 2010-03-17 17:33:07| 分类: php框架ci |字号 订阅 CI默认的rewrite url中是类似这样的,例如你的CI根 ...
- Windows 8.1 正式版微软官方原版镜像下载(新增10/17新版下载)
中文版:中国区OEM预装版本,特定国家版,锁定语言,其它功能和核心版没有区别.简体中文单语言版:锁定语言,其它功能和核心版没有区别.专业版+核心版[零售版][推荐]:镜像内包含专业版(Professi ...