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(找规律&amp;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. Robberies(简单的01背包 HDU2955)

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

  2. php微信2 获取自定义菜单中的access_token

    //自定义菜单中获取access_token    static function get_access_token($appid,$secret){        $url="https: ...

  3. 网络通信 --&gt; Linux 五种IO模型

    Linux 五种IO模型 聊聊Linux 五种IO模型

  4. Django教程01-全流程

    目录 1.Django简介 1.1. Django安装 2. 创建一个基础的Django项目 2.1. 初始化项目 2.2. 设计数据库 2.2.1. 设计目标表 2.2.1. 创建一个数据库 2.2 ...

  5. 20190319xlVBA_根据考勤数据统计缺勤缺考数据

    Sub SubtotalPickFile() Dim StartTime As Variant Dim UsedTime As Variant StartTime = VBA.Timer Dim fi ...

  6. 一文掌握Docker Compose

    目录 Docker Compose介绍 Docker Compose安装 Docker Compose基本示例 1.基本文件及目录设置 2.创建一个Dockerfile 3.通过docker-comp ...

  7. 解决阿里云安骑士漏洞警告:wordpress WPImageEditorImagick 指令注入漏洞

    解决:wordpress WPImageEditorImagick 指令注入漏洞 前些天在阿里云服务器上安装了wordpress,阿里云提示有wordpress WP_Image_Editor_Ima ...

  8. Linux运维一:生产环境CentOS6.6系统的安装

    CentOS 6.6 x86_64官方正式版系统(64位)下载地址 系统之家:http://www.xitongzhijia.net/linux/201412/33603.html 百度网盘:http ...

  9. tensorflow 卷积神经网络基本参数()

    目录: 1. tf.placeholder_with_default(tf.constant(1.0),shape=[],name='use_dropout')   # 设置一个占位符 2. tf.c ...

  10. Socket Tools的使用

    1.启动工具 Socket Tools.exe , 分享:链接:http://pan.baidu.com/s/1dFiuEHz 密码:1sv9 2.在本地创建TCP Server,自动启动监听 3.在 ...