HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=2227
用dp[i]表示以第i个数为结尾的nondecreasing串有多少个。
那么对于每个a[i]
要去找 <= a[i]的数字那些位置,加上他们的dp值即可。
可以用树状数组维护
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int MOD = ;
const int maxn = + ;
LL a[maxn], b[maxn];
int n;
LL c[maxn];
LL lowbit(LL x) {
return x & (-x);
}
void UpDate(int pos, LL val) {
while (pos <= n) {
c[pos] += val;
if (c[pos] >= MOD) c[pos] %= MOD;
pos += lowbit(pos);
}
}
LL query(int pos) {
LL ans = ;
assert(pos >= );
while (pos) {
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
void work() {
memset(c, , sizeof c);
for (int i = ; i <= n; ++i) {
cin >> a[i];
b[i] = a[i];
}
sort(b + , b + + n);
LL ans = ;
for (int i = ; i <= n; ++i) {
int pos = lower_bound(b + , b + + n, a[i]) - b;
LL tans = query(pos) + ;
ans += tans;
if (ans >= MOD) ans %= MOD;
UpDate(pos, tans);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
while (cin >> n) work();
return ;
}
HDU 2227 Find the nondecreasing subsequences dp思想 + 树状数组的更多相关文章
- 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 (线段树)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- 【CF314C】Sereja and Subsequences(DP,树状数组)
题意:给定一个N个数的数列,求所有不同不下降子序列的乘积之和,其中不同指的是组成它的数字和长度不完全相同 n (1 ≤ n ≤ 10^5) a[i]<=10^6 思路:考虑DP.设DP[a[i] ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...
- HDU - 5542 The Battle of Chibi(LIS+树状数组优化)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
随机推荐
- BZOJ 2244: [SDOI2011]拦截导弹 DP+CDQ分治
2244: [SDOI2011]拦截导弹 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度.并且能够拦截 ...
- vmware nat不能上网的解决办法
1 很多奇怪的问题都是vmware突然不能上网导致的 当yum.pip等包管理工具突然不能上网了时,要ping www.baidu.com,看看网络是不是好的. 2 nat网络出现问题的解决办法 2. ...
- yum lock
状态 :睡眠中,进程ID:18439Another app is currently holding the yum lock; waiting for it to exit... 另一个应用程序是: ...
- Does Hadoop require SSH?
https://wiki.apache.org/hadoop/FAQ#Does_Hadoop_require_SSH.3F Hadoop provided scripts (e.g., start-m ...
- golang---信号signal
golang中os/signal包的使用 chenbaoke · 2015-06-17 20:03:59 · 2748 次点击 · 预计阅读时间 1 分钟 · 不到1分钟之前 开始浏览 这是一个创建于 ...
- Java面试必会知识点
1.== 和 equals()比较: (1)== 是运算符,equals()是Object中定义的方法: (2)== 比较的是 数值 是否相同,基本类型比较数值,引用类型比较对象地址的数值:且变量类型 ...
- 秒懂单链表及其反转(reverse)
什么是链表,这种数据结构是由一组Node组成的,这群Node一起表示了一个序列.链表是最普通,最简单的数据结构(物理地址不连续),它是实现其他数据结构如stack, queue等的基础. 链表比起数组 ...
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用
目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 多线程的四种解决方案:pthread,NSThread,GCD,N ...
- Eclipse中文乱码问题
最近用TortoiseSVN从androiddex上下载了一个android工程,导入之后发现里面的中文都是乱码,于是在Eclipse里通过Windows-Preferences-Workspace修 ...
- MYSQL数据库学习----查询
查询语句是MYSQL数据库中用到的最多的语句. 查询语句分为几种 单表查询 集合函数查询 连接查询 子查询 合并查询 正则表达式查询 一:单表查询 SELECT 属性 FROM 表名 [WHERE 查 ...