HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)
题意:
给你一个序列a[],求它的不降子序列的个数
分析:
dp[i]表示以i结尾不降子序列的个数,dp[i]=sum(dp[j])+1(j<i&&a[j]<=a[i]);答案就是sum(dp[i])
但发现一个问题n很大O(n^2)肯定超时,想起前面做的两道题,用线段树优化,它是用维护的是和,可以用BIT优化,但又发现a[i]的值很大没法存,就又想到了离散化,恩这个题就解决了。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll a[N],bit[N],tmp[N],dp[N];
int n;
void add(int x,ll d){
while(x<=n){
bit[x]=(bit[x]+d)%mod;
x+=(x&(-x));
}
}
ll sum(int x){
ll num=;
while(x>){
num=(num+bit[x])%mod;
x-=(x&(-x));
}
return num;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=;i<=n;++i){
scanf("%I64d",&a[i]);
tmp[i]=a[i];
}
sort(tmp+,tmp+n+);
int len=unique(tmp+,tmp+n+)-tmp-;
/*for(int i=1;i<=len;++i)
cout<<tmp[i]<<endl;*/
memset(dp,,sizeof(dp));
memset(bit,,sizeof(bit));
ll total=;
for(int i=;i<=n;++i){
int pos=lower_bound(tmp+,tmp+len+,a[i])-tmp;
dp[i]=;
dp[i]=(dp[i]+sum(pos))%mod;
add(pos,dp[i]);
total=(total+dp[i])%mod;
}
printf("%I64d\n",total);
}
return ;
}
HDU 2227-Find the nondecreasing subsequences(dp+BIT优化)的更多相关文章
- 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(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] 要去找 & ...
- 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 (数状数组)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...
- hdu 5745 La Vie en rose DP + bitset优化
http://acm.hdu.edu.cn/showproblem.php?pid=5745 这题好劲爆啊.dp容易想,但是要bitset优化,就想不到了. 先放一个tle的dp.复杂度O(n * m ...
- HDU 3506 (环形石子合并)区间dp+四边形优化
Monkey Party Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- HDU 5313——Bipartite Graph——————【二分图+dp+bitset优化】
Bipartite Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
随机推荐
- CLR 读书笔记
http://www.cnblogs.com/wang_yb/tag/CLR%20via%20C%23%E7%AC%94%E8%AE%B0/
- cojs 简单的数位DP 题解报告
首先这道题真的是个数位DP 我们考虑所有的限制: 首先第六个限制和第二个限制是重复的,保留第二个限制即可 第五个限制在转移中可以判断,不用放在状态里 对于第一个限制,我们可以增加一维表示余数即可 对于 ...
- lintcode :最小路径和
题目: 最小路径和 给定一个只含非负整数的m*n网格,找到一条从左上角到右下角的可以使数字和最小的路径. 样例 注意 你在同一时间只能向下或者向右移动一步 解题: 这个和求三角形的最小路径的差不多 ...
- ASP.NET MVC 3 Razor 视图引擎 基本语法
本篇博文将进入MVC 3 的世界了,首先学习一下MVC 3 新增的Razor视图引擎的基本语法. 1. 使用 @ 字符将代码添加到页面中.正如传统的aspx视图的<% %>相同. ...
- Android Cursor空指针的问题
最近几天无聊自己动手写个音乐播放器,用到Cursor来取得数据库中音乐文件的信息,但是当用到Cursor的时候总是报空指针错误,后来发现是模拟器上没有音乐文件,使用Cursor的时候 ,若Cursor ...
- IIS 无法打开页面,只能重启的问题
最终解决方案: 要变通解决此问题,启用 EnableAggressiveMemoryUsage 注册表项在注册表中.当启用了 EnableAggressiveMemoryUsage 注册表项 Http ...
- Myeclipse多行注释快捷键及其他快捷键
当时我看到struts2讲解视频的时候,讲解员居然能一下子注释掉好几行代码,而且注释的很整齐,然我大吃一惊,上网搜了下Myeclipse的快捷键还真多 选择你要注释的那一行或多行代码,按Ctrl+/即 ...
- git设置对比工具
windows下设置 beyond compare 3 为 git 的对比工具. 首先需要先安装 beyond compare 3 工具,切记需要安装安装版的,不要搞绿色版的. mac下使用 Kal ...
- ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)
1 imageView.setScaleType(ImageView.ScaleType.FIT_XY ); 1 这里我们重点理解ImageView的属性android:scaleType,即Imag ...
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...