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格式教材下载 ...
随机推荐
- sysenter内核入口点代码分析
参考:http://www.mouseos.com/windows/kernel/KiFastCallEntry.html http://www.mouseos.com/windows/kernel/ ...
- 【读书笔记】:MIT线性代数(4):Independence, Basis and Dimension
Independence: The columns of A are independent when the nullspace N (A) contains only the zero vecto ...
- 年度重大升级,IntelliJ IDEA 2019.2 稳定版发布
文章转载自 OSCHINA 社区 [http://www.oschina.net] 期待已久. 7月24日,JetBrains 正式发布了 IntelliJ IDEA 2019.2 稳定版. 作为 I ...
- c# WInform 自定义导航布局
问题形成原因:软件一般都是左侧树导航或上部菜单导航,做好一个软件后,有的客户可能想用一个页面做导航图像,而各个客户用的功能可能不同,所以导航布局需要自定义. 思路:1.把菜单列出来 2.双击菜单生成一 ...
- [FW]使用kprobes查看内核内部信息
使用printk打印变量等方法,是调试内核的有效方法之一,但是这种方法必须重新构建并用新内核启动,调试效率比较低.以内核模块的方式使用kprobes.jprobes,就可以在任意地址插入侦测器,执行包 ...
- [fw]GDT是在分段中為了相容real mode 跟 protected mode的產物
在Protected Mode下,一个重要的必不可少的数据结构就是GDT(Global Descriptor Table). 为什么要有GDT?我们首先考虑一下在Real Mode下的编程模型: 在R ...
- 修改el-input
无论是input框,还是select, 都是对 el-input 进行修改: .houseRegisterCompany .el-input{ width: 130px; }
- 钉钉机器人SDK 封装预警消息发送工具
1 群机器人 (1) 引言 钉钉聊天群内支持的群机器人, 类似QQ 群机器人, 可以发天气, 讲笑话那样; 钉钉群机器人支持自定义机器人, 允许开发者管理机器人做预警消息通知; ...
- String类可以被继承吗?我们来聊聊final关键字!
String类可以被继承吗?我们来聊聊final关键字! String在java基础知识中绝对是个重点知识,关于String的一些问题也是非常的多,而且牵涉到内存等高级知识,在面试中也是经常被考察的一 ...
- 使用await写异步优化代码
使用promise: function readMsg(){ return dispatch=>{ axios.post('/msgList').then(res=>{ console.l ...