HDU 4455 Substrings[多重dp]
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3269 Accepted Submission(s): 999
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
Each test case starts with a positive integer n, the array length. The next line consists of n integers a1,a2…an, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=106, 0<=Q<=104, 0<= a1,a2…an <=106
1 1 2 3 4 4 5
3
1
2
3
0
10
12


#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
int n,num[N];
int cx[N],add[N],lnc[N];
long long f[N];
void Discretization(){
memset(cx,,sizeof cx);
memset(lnc,,sizeof lnc);
for(int i=;i<=n;i++){
if(!cx[num[n-i+]]){
cx[num[n-i+]]=;
lnc[i]=lnc[i-]+;
}
else{
lnc[i]=lnc[i-];
}
}
memset(cx,,sizeof cx);
memset(add,,sizeof add);
for(int i=;i<=n;i++){
add[i-cx[num[i]]]++;
cx[num[i]]=i;
}
}
void DynamicProgramming(){
memset(f,,sizeof f);
f[]=n;int delta=n;
for(int i=;i<=n;i++){
f[i]=f[i-]-lnc[i-];
delta-=add[i-];
f[i]+=delta;
}
}
void Solution(){
int Q,x;
for(scanf("%d",&Q);Q--;) scanf("%d",&x),printf("%lld\n",f[x]);
}
int main(){
while((~scanf("%d",&n))&&n){
for(int i=;i<=n;i++) scanf("%d",&num[i]);
Discretization();
DynamicProgramming();
Solution();
}
return ;
}
HDU 4455 Substrings[多重dp]的更多相关文章
- hdu 4455 Substrings (DP 预处理思路)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 4455 Substrings(计数)
题目链接:hdu 4455 Substrings 题目大意:给出n,然后是n个数a[1] ~ a[n], 然后是q次询问,每次询问给出w, 将数列a[i]分成若干个连续且元素数量为w的集合,计算每个集 ...
- hdu 4455 Substrings(找规律&DP)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4455 Substrings ( DP好题 )
这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...
- HDU 4455.Substrings
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU - 4455 Substrings(非原创)
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct ...
- HDU 4455 Substrings --递推+树状数组优化
题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- docker 部署nginx+weblogic集群
測试一段时间.再来报告
- mongodb查询之从多种分类中获取各分类最新一条记录
mongodb查询之从多种分类中获取各分类最新一条记录 2017年04月06日 13:02:47 monkey_four 阅读数:6707更多 个人分类: MongoDBJavaScript 文章 ...
- Laravel Debugbar
Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...
- 获取表单提交MVC错误信息
if (!ModelState.IsValid) { List<string> Keys = ModelState.Ke ...
- Hello World! 这是我的第一个 CGI 程序
Hello World! 这是我的第一个 CGI 程序上面的 C++ 程序是一个简单的程序,把它的输出写在 STDOUT 文件上,即显示在屏幕上.在这里,值得注意一点,第一行输出 Content-ty ...
- KO 数据库分类系统介绍
KEGG Orthology数据库不仅对基因的功能进行了扩充和整理,还对功能进行了细致的分类: 针对基因的功能,共有3级分类,第一级有6个大类: 打开下面这个链接 http://www.genome. ...
- while(scanf("%d",&n)!=EOF)与while(cin>>n)
我们知道scanf函数是C语言里面的,其返回值是,被输入函数成功赋值的变量个数.针对于int counts = scanf("%d",&n);来说如果赋值成功那么其返回值 ...
- asp.net MVC提高开发速度(创建项目模板)
- kafka学习之-配置详解
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- BDIP和BVLC纹理分析
Young Deok Chun 等人提出了基于 BVLC 矩和 BDIP 矩的一种纹理分析方法.BVLC 能显示粗糙和光滑特性,BDIP 能够很好的提取波谷和边缘.它们直接在彩色空间上进行处理,能有效 ...