Substrings

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

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
We have carefully selected several similar problems for you:  6018 6017 6016 6015 6014 
 
题意:有n个数,q个询问。求每次询问的连续长度为w的子数组权值的和。数组的权值为数组内不相同的数的个数。
 
思路:

拿样例写一下

1: {1},           {1},           {2},        {3},            {4},          {4},          {5}

2: {1,1},        {1,2},        {2,3},      {3,4},         {4,4},       {4,5}

3: {1,1,2},     {1,2,3},     {2,3,4},    {3,4,4},      {4,4,5}

4: {1,1,2,3},  {1,2,3,4},  {2,3,4,4},  {3,4,4,5}

...

容易发现可以得出一个发现:

长度为w的值肯定包含长度为w-1的值减去最后一个字数组的权值和

例:

2: {1,1} ,  {1,2} ,    {2,3} ,    {3,4} ,    {4,4} ,   {4,5}

3: {1,1,} , {1,2,} , {2,3,} , {3,4,} , {4,4,} 

dp[3] = dp[2] - 权值[{4,5}] + ?

而'?'就表示添加上那些标蓝色的数之后要添加的值.

设某个标蓝色的数为a[i],另外一个和它相等的且和它最近的数为a[j],且i>j

如果i-j>w的话那么添上这个标蓝的数就可以使得dp[w]+1

所以我们只要求出cnt[dis]即可,dis既某数离在它之前且相等的且和它最近的数的距离.

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=1e6+,inf=0x3f3f3f3f,mod=1e9+;
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
int a[MAXN];
int cnt[MAXN],dp[MAXN];
ll sign[MAXN];
void run(int n)
{
memset(cnt,,sizeof(cnt));
memset(sign,,sizeof(sign));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
int x=i-sign[a[i]];
cnt[i-sign[a[i]]]++;
sign[a[i]]=i; ///sign[i]表示i的位置
//cout<<x<<" "<<cnt[x]<<endl;
}
memset(dp,,sizeof(dp));
memset(sign,,sizeof(sign));
for(int i=n,t=; i>; i--,t++)
{
if(sign[a[i]]) dp[t]=dp[t-];
else dp[t]=dp[t-]+,sign[a[i]]=; ///sign[i]标记i是否出现过
}
memset(sign,,sizeof(sign));
int t=n;
for(int i=; i<=n; i++)
{
sign[i]=sign[i-]-ll(dp[i-]);
t-=cnt[i-];
sign[i]+=t;
}
}
int main()
{
int n;
while(scanf("%d",&n)&&n!=)
{
run(n);
int q;
scanf("%d",&q);
while(q--)
{
int w;
scanf("%d",&w);
cout<<sign[w]<<endl;
}
}
return ;
}

HDU 4455.Substrings的更多相关文章

  1. hdu 4455 Substrings(计数)

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

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

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

  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]

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

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

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

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

  7. HDU 4455 Substrings ( DP好题 )

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

  8. Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)

    http://acm.hdu.edu.cn/showproblem.php?pid=4455 https://icpcarchive.ecs.baylor.edu/index.php?option=c ...

  9. Substrings(hdu 4455)

    题意: 给定一个序列ai,个数为n.再给出一系列w:对于每个w,求序列中,所有长度为w的连续子串中的权值和,子串权值为子串中不同数的个数. /* dp[i]表示长度为i的序列不同元素个数之和. 考虑从 ...

随机推荐

  1. hibernate flush clear的区别

    有的时候你执行了更新什么的操作不一定能查出来:没有保存到数据库 以下的缓存是指一级缓存,即session:默认缓存是一级缓存: flush的意思就是执行sql,但是还没有commit,没有持久化:再清 ...

  2. JS拖拽元素原理及实现代码

    一.拖拽的流程动作 ①鼠标按下②鼠标移动③鼠标松开 二.拖拽流程中对应的JS事件 ①鼠标按下会触发onmousedown事件 ②鼠标移动会触发onmousemove事件 ③鼠标松开会触发onmouse ...

  3. padding-bottom图片自适应

    今天学了慕课网的去哪了视频轮播图的图片自适应是这么做的htm,cssl如下:为什么padding-bottom 取值62.08% 呢,因为图片的高为465px,宽为749px. 465/749 既为6 ...

  4. zookeeper 集群配置采坑 Connection refused WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@584] - Cannot open channel to 3 at election address slave2/192.168.127.133:3888

    坑一: Cannot open channel to at election address slave1/ java.net.ConnectException: Connection refused ...

  5. ServiceWorker.state

    [ServiceWorker.state] ServiceWorker.state The state read-only property of the ServiceWorker interfac ...

  6. cdh 安装系列1-- manager 6.01 安装

    一.如果是远程centos安装,请参考我得博客,通过xmanager 链接centos桌面 二.Non-production Installation Ideal for trying Clouder ...

  7. tomcat 启动报错org.hibernate.cfg.annotations.SimpleValueBinder.setType

    url: https://blog.csdn.net/zhx_0323/article/details/78844323 # A fatal error has been detected by th ...

  8. java面试题:网络通信

    网络分层 Q:OSI网络七层模型. Http Q:http协议的状态码有哪些?含义是什么? 200,服务器已成功处理了请求. 302,重定向. 400,错误请求. 401,未授权,请求要求身份验证. ...

  9. DC之setup-hold time详解

    转自一下站点: http://www.blogbus.com/bb2hh-logs/20463915.html

  10. LightOJ - 1030 期望+dp

    题目链接:https://vjudge.net/problem/25907/origin 一个山洞,里面有有1到n个位置,每个位置都有一定的金币,你有一个六面的骰子,一开始你在1,每次摇到了哪个数就往 ...