HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1442 Accepted Submission(s): 516
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}.
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.
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.
Sample Input
3
1 2 3
Sample Output
7
题目分析
题目大意:给你一个串,求这个串中不递减的子串有多少个?初一看,完全想不到会是用树状数组,但是他就是这么神奇,不递减就想到逆序数,逆序数又想到了树状数组,居然是用他。他是求子串有多少个,又要用到DP,这里DP就是用树状数组慢慢推上去,最后是注意是求和会溢出,记得%1000000007。
#include<iostream>
#include<stdio.h>
#include<memory.h>
#include<algorithm> using namespace std ; struct node
{
int val, id ;
}a[]; bool cmp(node a, node b)
{
return a.val < b.val ;
} int b[] , c[] , s[] ,n ; int lowbit( int i)
{
return i&(-i);
} void update ( int i , int x )
{
while(i<=n)
{
s[i]+=x;
if(s[i]>=)
s[i]%=;
i+=lowbit(i);
}
} int sum( int i )
{
int sum = ;
while( i > )
{
sum += s[i] ;
if( sum >= )
sum %= ;
i-=lowbit( i ) ;
}
return sum ;
} int main()
{
int i , res ;
while(scanf("%d",&n)!=EOF)
{
memset( b , , sizeof(b)) ;
memset( s , , sizeof(s)) ;
for(i=;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id = i ;
}
sort(a+,a+n+,cmp);
b[a[].id] = ;
for(i=;i<=n;i++)
{
if(a[i].val!=a[i-].val)
b[a[i].id] = i ;
else b[a[i].id] = b[a[i-].id] ;
}
res = ;
for(i=;i<=n;i++)
{
c[i] = sum( b[i] ) ;
update ( b[i] , c[i]+ ) ;
}
printf("%d\n",sum(n));
}
return ;
}
HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)的更多相关文章
- 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 dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227 用dp[i]表示以第i个数为结尾的nondecreasing串有多少个. 那么对于每个a[i] 要去找 & ...
- HDU2227Find the nondecreasing subsequences(树状数组+DP)
题目大意就是说帮你给出一个序列a,让你求出它的非递减序列有多少个. 设dp[i]表示以a[i]结尾的非递减子序列的个数,由题意我们可以写出状态转移方程: dp[i] = sum{dp[j] | 1&l ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [USACO]奶牛抗议(DP+树状数组+离散化)
Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...
- JZYZOJ 1360 [usaco2011feb]人品问题 DP 树状数组 离散化
http://172.20.6.3/Problem_Show.asp?id=1360 好想好写 代码 #include<iostream> #include<cstdio&g ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
随机推荐
- 7九章算法强化班全解--------Hadoop跃爷Spark
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- CRUD查询
简单查询: 1.最简单的查询 select*form 表名; *查所有的列select*form info 2.查询指定列 select code,name form info 3.修改结果集的列名 ...
- C# I/O
获取运行时的动态目录 private static string GetDataDir_Data() { var parent = Directory.GetParent(Directory.GetC ...
- JSon解析
1.解析总是犯糊涂 在图中,[] 中括号表示是jsonarray,{} 小括号 表示对象 这个解析可以写成: JSONArray array=new JSONArray(result); for( ...
- IE8下导入EXCEL数据传到客户端以附件下载
IE8下导入EXCEL数据传到客户端以附件下载方式出现,而不显示数据,解决方法:以text/html格式返回. HttpResponseMessage message = new HttpRespon ...
- Sublime Text实用小技巧
1.输入"!"或"html:5",然后按Tab键: html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html:4s:用于 ...
- unity播放视频
方法一: 1.用一个Quad(或者其他的)做屏幕,调整摄像机位置和它的大小,使它显示在你想要的区域 2.将视频拖入Unity,拖入,拖入,拖入:不是将视频直接copy到指定的文件夹中 3.将下面脚本挂 ...
- 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】
一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...
- 【Android】一道Android OpenGL笔试题
一道Android OpenGL笔试题 SkySeraph May. 5th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph个人站点:www.sky ...
- HTTP 的重定向301,302,303,307(转)
HTTP 的重定向301,302,303,307(转) (2012-12-11 11:55:04) 转载▼ 标签: 杂谈 分类: 网络 301 永久重定向,告诉客户端以后应从新地址访问.302 作为H ...