HDU3450_Counting Sequences
题意:
让你从所给的序列中找到他的子序列,使他们相邻之间差距不超过d,问有多少个转移的子序列
这题第一眼大概就知道是状态转移,sum[i]表示以前i个中有多少个,那么sum[i+1]比sum[i]
多了一个以第i+1为结尾的子序列,那么只需要知道前面当中以x(x与第i+1距离不超过d)结尾的子序列个数和,那么这个时候在用dp[x]表示当前以x结尾有多少个子序列,但是数字太大不能直接记录,直接求和.
所以需要在状态转移时候运用到一些技巧,树状数组(也可以用线段树)和离散化;
先读入所以数字,然后排序编号,并用树状数组维护
Description
And for a sub-sequence{ai1,ai2,ai3...aik},if it matches the following qualities: k >= 2, and the neighboring 2 elements have the difference not larger than d, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number
of its perfect sub-sequence.
Input
Output
Sample Input
4 2
1 3 7 5
Sample Output
4
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector> using namespace std;
const int maxn = 2e5+100;
const int MOD = 9901;
#define pr(x) cout << #x << " = " << x << " ";
#define prln(x) cout << #x << " = " << x <<endl;
#define ll long long ll cnt;
map<ll,ll> ID;
ll a[maxn],b[maxn],dp[maxn],n;
void getID(ll x) {
if(!ID.count(x)) {
ID[x] = ++cnt;
}
}
ll lowbit( ll x )
{
return x & (-x);
} void add(ll x,ll d)
{
while( x <= n)
{
dp[x] = (dp[x] + d)%MOD;;
x += lowbit(x);
}
} ll sum (ll x)
{
int ans = 0;
while(x)
{
ans = (ans + dp[x])%MOD;
x -= lowbit(x);
}
return ans%MOD;
} int main(){
#ifdef LOCAL
freopen("C:\\Users\\User Soft\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\User Soft\\Desktop\\out.txt","w",stdout);
#endif
ll d;
while( cin >> n >> d) {
ID.clear();cnt = 0;
memset(dp,0,sizeof dp);
for(int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
b[i] = a[i];
}
sort(b,b+n);
for(int i = 0; i < n; ++i) getID(b[i]);
for(int i = 0; i < n; i++) {
int l = lower_bound(b,b+n,a[i] - d) -b;
int r = upper_bound(b,b+n,a[i] + d) - b-1;
//if(r == l)
l = ID[b[l]],r = ID[b[r]];
ll num = (sum(r) - sum(l-1) +1)%MOD;
add(ID[a[i]],num );
}
cout << (sum(cnt) + 20*MOD- n)%MOD << endl; }
return 0;
}
HDU3450_Counting Sequences的更多相关文章
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [Leetcode] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)
Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...
- leetcode 187. Repeated DNA Sequences 求重复的DNA串 ---------- java
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- Python数据类型之“序列概述与基本序列类型(Basic Sequences)”
序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...
- Extract Fasta Sequences Sub Sets by position
cut -d " " -f 1 sequences.fa | tr -s "\n" "\t"| sed -s 's/>/\n/g' & ...
- 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 440 Solved: 16 ...
- MOOCULUS微积分-2: 数列与级数学习笔记 1. Sequences
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
随机推荐
- Linux查看大文件日志
Linux 查看大日志文件1.使用 less 命令 less filename 但是使用上述命令的坏处是,默认打开的位置在第一行,并且当切换到实时滚动模式(按 F ,实现效果类似 tail -f 效果 ...
- LeetCode 求众数 II
题目链接:https://leetcode-cn.com/problems/majority-element-ii/ 题目大意: 略. 分析: k个一起删, 最后check一下即可. 代码如下: #d ...
- asp.net的处理机制(.ashx/.aspx)
浅谈自己对asp.net 处理机制的图解 图解的内容有点多(包含asp.net 的处理机制和页面生命周期的重要事件,建议小伙伴把图片下载查看可好?) asp.net处理机制解说 当浏览器发送一条请求给 ...
- centos6.8安装redis
1.下载Redis3.2.5安装包 cd /optwget http://download.redis.io/releases/redis-3.2.5.tar.gz 2.解压.编译.安装redis-3 ...
- C++中一个类(非继承类)对象,所占内存空间大小
离职后在家里带了半年多了,这半年多里没有编写过一行代码,倒是看过一些书,但是差不多也都是囫圃吞枣.房子也快要装修,也得赶快找一个工作了,不然养车,还要玩摄影,没收入的日子真是不好过啊.呵呵. 按惯例, ...
- 洛谷 P1197 [JSOI2008]星球大战——并查集
先上一波题目 https://www.luogu.org/problem/P1197 很明显删除的操作并不好处理 那么我们可以考虑把删边变成加边 只需要一波时间倒流就可以解决拉 储存删边顺序倒过来加边 ...
- Codeforces 1132D(二分答案+堆)
题面 传送门 分析 二分答案,考虑如何判定 可以用贪心的方法,每次找最快没电的电脑,在没电前1单位时间给它充电 正确性显然 实现上可以维护一个堆,存储每个电脑电用完的时刻,每次从堆顶取出最小的一个给它 ...
- poj2752Seek the Name, Seek the Fame(next数组)
题目传送门 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2439 ...
- 使用autoconf和automake生成Makefile
使用环境: 我的是Ubuntu 16.04,需要用到autoconf和automake,没有的话自行安装. 以helloworld为例: 1.首先新建一个文件夹然后进去没的说:然后自然得先写出那个著名 ...
- android sdcard保存文件