Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 183 Accepted Submission(s): 42
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
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
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
这题不容易想到,一看题目,看到这数据范围,看到查询的方式。。。一直在往树状数组或者线段树方面去想。
想到了用DP解决就不难了。
用DP的思路O(n)复杂度解决。
以样例为例说明:
1 1 2 3 4 4 5;
明显dp[1]=n=7;
长度为1的时候有7个区间。从长度为1到长度为2,就是把前6个区间往后增加一个数,把最后一个区间去掉。
增加的6个数要看在该区间是否出现过,只要看它上一个相等的元素距离是否大于2
所以dp[2]=dp[1]-1+4;
以此类推就可以得出所以的dp值了。
dp[i]=dp[i-1]-A+B;
减的A是最后一个长度为i-1的区间的不同数的个数,这个很容易预处理得出来。
加的B是第t个数到它上一个数的距离大于i-1的个数.
这个B值也容易得出。
用s[i]表示离上一个数的距离为i的个数,不断减掉就得到B了。
具体看代码:
//============================================================================ // Name : HDU4455.cpp // Author : kuangbin // Version : // Copyright : Your copyright notice // Description : // DP // //============================================================================ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; const int MAXN=1000010; int a[MAXN];//1-n输入的数列 int f[MAXN];//f[i]表示a[i]在前面最近出现的位置,f[i]==0表示从左到右第一次出现 int s[MAXN];//s[i]表示 t-f[t]==i,1<=t<=n的t的个数,即离上一个相等元素的距离为i的个数 long long dp[MAXN];//需要输出的结果 int ss[MAXN];//ss[i]表示最后的i个数含有的不同元素的个数 int main() { int n; int m; while(scanf("%d",&n)==1 && n) { memset(f,0,sizeof(f)); memset(s,0,sizeof(s)); //顺着求s数组 for(int i=1;i<=n;i++) { scanf("%d",&a[i]); s[i-f[a[i]]]++; f[a[i]]=i; } memset(f,0,sizeof(f));//f数组标记在后面是否出现过 ss[1]=1; f[a[n]]=1; for(int i=2;i<=n;i++) { if(f[a[n-i+1]]==0) { f[a[n-i+1]]=1; ss[i]=ss[i-1]+1; } else ss[i]=ss[i-1]; } dp[1]=n; int sum=n; //从dp[i-1]扩展到dp[i]就是去掉最后一个区间的个数,把前面的区间长度增加1, //加上相应增加的种类数 for(int i=2;i<=n;i++) { dp[i]=dp[i-1]-ss[i-1];//减掉最后一个区间的种类数 sum-=s[i-1]; dp[i]+=sum;//加上前面的区间增加一个长度后增加的种类数 } scanf("%d",&m); int t; while(m--) { scanf("%d",&t); printf("%I64d\n",dp[t]); } } return 0; }