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 ...
随机推荐
- CentOS 6.3下Samba服务器的安装与配置方法(图文详解)
这篇文章主要介绍了CentOS 6.3下Samba服务器的安装与配置方法(图文详解),需要的朋友可以参考下 一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件, ...
- Winform自定义控件基础(二)
protected override void WndProc(ref Message m)
- C# Mvc异常处理过滤器
using System; using System.Text; using EMS.Domains.Core; using System.Web.Mvc; using Json.Net; using ...
- C/C++: C++变量和基本类型
1. 如何选择类型的准则 当明确知晓数值不可能为负的时候,应该选择无符号类型. 使用int执行整数运算的时候,在实际应用中,short常常显得太小而long一般和int有一样的尺寸,如果数值超过了in ...
- 在MonthCalendar控件中选中日期
Calendar.MONTH Calendar now=Calendar.getInstance();System.out.print(now.get(Calendar.MONTH));得到的月份少1 ...
- 将数据导入Excel
/** * 查询未打印订单 * @param req * @param sort * @param order * @param rows * @param page * @return */ pub ...
- marquee实现文字移动效果;js+div实现文字无缝移动效果
1.marquee实现文字移动: <marquee width="220px;" scrollamount="5" onmouseover="t ...
- vue-resource
代替ajax操作 在mian.js里面引入 import vueResource from 'vue-resource' mian.js会 加载全局js 下面介绍的是基于Vue实例的方式,一般项目只需 ...
- 把包发布到npm官网
一.包 包就是多模块的集合,CommonJS的包规范给程序员提供了组织模块的标准,减少沟通成本. 规范: 所有的模块放在demo文件夹下(包名)的lib文件夹里面 在lib文件夹的同级目录下新建ind ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...