Substrings

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

Total Submission(s): 1727    Accepted Submission(s): 518

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
 

1、
非常明显,长度为1的答案为dp[1]=n;

2、
长度为2的为dp[2]=dp[1]+x-y=7+4-1=10;

X为添加的一个数和前边不同的个数,{1,1},{1,2},{2,3},{3,4},{4,4},{4,5} 为4;

Y为去掉的不足2的区间有几个不同数字,长度为1的最后一个区间{5},须要舍去。为1;

3、

长度为3的为dp[3]=dp[2]+x-y=10+4-2=12。

X为添加的一个数和前边不同的个数,{1,1,2}。{1,2,3},{2,3,4}。{3,4,4},{4,4,5}为4;

Y为去掉的不足3的区间有几个不同数字。长度为2的最后一个区间{4,5},须要舍去,为2;

所以,我们须要得到当前数字和它上次出现的位置差的大小。详细实现看代码。。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1000005
#define LL __int64
const int inf=0x1f1f1f1f;
int a[N],len[N],pre[N],vis[N],f[N];
LL dp[N];
int main()
{
int i,n,m;
while(scanf("%d",&n),n)
{
memset(pre,-1,sizeof(pre)); //记录一个值上次出现的位置
memset(len,0,sizeof(len)); //len[i]:有几个间隔为i的数
memset(dp,0,sizeof(dp)); //记录终于答案
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
len[i-pre[a[i]]]++;
pre[a[i]]=i;
}
for(i=n-1;i>=0;i--)
len[i]+=len[i+1];
memset(f,0,sizeof(f)); //f[i]从后往前记录后i个数有几个不同值
memset(vis,0,sizeof(vis));
for(i=1;i<n;i++)
{
f[i]=f[i-1];
if(!vis[a[n-i]])
{
vis[a[n-i]]=1;
f[i]++;
}
}
dp[1]=n;
for(i=2;i<=n;i++)
dp[i]=dp[i-1]+len[i]-f[i-1];
scanf("%d",&m);
while(m--)
{
scanf("%d",&i);
printf("%I64d\n",dp[i]);
} }
return 0;
}

hdu 4455 Substrings (DP 预处理思路)的更多相关文章

  1. HDU 4455 Substrings ( DP好题 )

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

  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]

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

  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. 【HDOJ】4455 Substrings

    5000ms的时限,还挺长的.算法是DP.思路是找到ans[1..n]的结果,然后Query就容易做了.问题是怎么DP?考虑:1 1 2 3 4 4 5w=1: 7, 7 = 1 * 7w=2: 10 ...

  9. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

随机推荐

  1. C++将文件内容一次性读入内存

    结合字符串流,将文件中的内容一次性读入内存,代码如下: #include <string> using std::ostringstream; using std::ifstream; u ...

  2. MySQL教程及经常使用命令1.1

    在线教程 21分钟 MySQL 新手教程 w3school在线教程(MYSQL) 变量 查看系统变量 show global variables 查看详细变量 show global variable ...

  3. T-Sql中的pivot和unpivot

    写在前面 今天休息,抽空了解下pivot和unpivot,记得老师讲行转列的时候,貌似提到过,不过他说的最多的就是“这个你们私下可以自己学,很简单的...”,简单你咋不讲呢,不吐槽他了,还是好好整理下 ...

  4. 两个activity之间的数据传递

    1.清单文件第二个activity<activity android:name="com.example.twodatapass.ResultActivity" androi ...

  5. 分析一个socket通信: server/client

    分析一个socket通信: server/client1 server 1. 创建一个server_socket文件,并绑定端口,然后监听端口 (socket, bind, listen) 2. 查询 ...

  6. 3522: [Poi2014]Hotel( 树形dp )

    枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) = dist( x , b ) = dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显 ...

  7. Android:创建文件或文件夹以及获取sd卡根目录

    目录结构: 功能,可以根据录入的目录或者文件夹生成相应的文件或者文件夹 首先需要添加一个权限: <uses-permission android:name="android.permi ...

  8. Qt调用DLL

    声明: 事先我已经自己动手写了一个简单的dll文件(myDLL.dll),C版接口的.并且用我前两篇有关DLL文章里面的方法,从dll中导出了导入库(.lib)文件,dll中有两个函数,原型如下:   ...

  9. 关于C(m,n)%p的故事

    序 遥远的\(\mod p\)(\(p\)是质数)大陆有一个恶魔:\[C(m,n)={m!\over n! (m-n)!}\] 于是大家有了各种求逆元的方法.这里MOD = p. 壹 for (int ...

  10. NEC协议

    注意: 用示波器在接收头抓的电平看起来和NEC协议刚好相反, 那是因为:HS0038B 这个红外一体化接收头,当收到有载波的信号的时候,会输出一个低电平,空闲的时候会输出高电平. 具体情况,具体分析. ...