Substrings

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3269    Accepted Submission(s): 999

Problem Description
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct elements’ number in all substrings of length w. For example, the array is { 1 1 2 3 4 4 5 } When w = 3, there are five substrings of length 3. They are (1,1,2),(1,2,3),(2,3,4),(3,4,4),(4,4,5)
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
 
Input
There are several test cases.
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
 
Output
For each test case, your program should output exactly Q lines, the sum of the distinct number in all substrings of length w for each query.
 
Sample Input
7
1 1 2 3 4 4 5
3
1
2
3
0
 
Sample Output
7
10
12
 
Source
 
Recommend

#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]的更多相关文章

  1. hdu 4455 Substrings (DP 预处理思路)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. hdu 4455 Substrings(计数)

    题目链接:hdu 4455 Substrings 题目大意:给出n,然后是n个数a[1] ~ a[n], 然后是q次询问,每次询问给出w, 将数列a[i]分成若干个连续且元素数量为w的集合,计算每个集 ...

  3. hdu 4455 Substrings(找规律&DP)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 4455 Substrings ( DP好题 )

    这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...

  5. HDU 4455.Substrings

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 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 ...

  7. HDU 4455 Substrings --递推+树状数组优化

    题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...

  8. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  9. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. zookeeper连接 org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able or retry gave up [main-EventThread]

    ERROR org.apache.curator.framework.imps.CuratorFrameworkImpl Background exception was not retry-able ...

  2. hbase 部署

    hbase的部署相对于java来说就比较简单啦,主要过程如下: 1.下载hbase最新的稳定版 2.拷贝到相应的目录 3.修改conf目录下的hbase-env.sh,设置java 和不适用内置的zo ...

  3. JavaScript中的Boolean 方法与Number方法

    <html> <head> <script type="text/javascript"> //创建 var str = "aaafg ...

  4. mysql 创建新用户并添加权限

    1.添加用户 1.1 添加一个新用户: mysql>grant usage on *.* to " with grant option; 上面这种只支持mysql服务器本地登录. 1. ...

  5. Keypress - 捕获键盘输入的JavaScript库

    Keypress 是一个强大的 JavaScript 库,用于捕获键盘输入.这是一个有非常特殊的功能的输入捕获库,它是很容易掌握和使用,并且不依赖第三方库.在网站开发中,经常会碰到需要处理键盘输入的场 ...

  6. 用wamp5和ZendOptimizer搭建环境运行Discuz论坛

    用wamp5和ZendOptimizer搭建环境运行Discuz论坛 用wamp5和ZendOptimizer搭建环境运行Discuz论坛 一: 环境:      1:Windows操作系统(Linu ...

  7. 美化VC界面(用户登录界面)

    源代码:下载 VC开发程序单调的界面相信大家都是深有感触,提到界面美化编程,人们都会说做界面不要用VC写,太难了.一句俗语:难者不会,会者不难.VC的美化界面编程并没有人们想像的那么难.这篇文章是我写 ...

  8. (转)RTP协议全解(H264码流和PS流)

    写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个比较全面的解析, 其中借鉴了很多文章,我都列在了文章最后,在此表示感谢. 互联网的发展离不开大家的无私奉献,我决定从我做起,希 ...

  9. CCF - 最大矩形

    试题编号: 201312-3 试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n ...

  10. 学习 TList 类的实现[2]

    我原来以为 TList 可能是一个链表, 其实只是一个数组而已. 你知道它包含着多大一个数组吗? MaxListSize 个!MaxListSize 是 Delphi 在 Classes 单元定义的一 ...