HDU4455 Substrings(DP)
来源:互联网 发布:oracle sql循环语句 编辑:程序博客网 时间:2024/06/08 02:56
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2918 Accepted Submission(s): 895
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
71 1 2 3 4 4 531230
Sample Output
71012
Source
2012 Asia Hangzhou Regional Contest
这题不容易想到,一看题目,看到这数据范围,看到查询的方式。。。一直在往树状数组或者线段树方面去想。想到了用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了。
具体看代码:
#include <iostream>#include <stdio.h>#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;}
0 0
- HDU4455 Substrings(DP)
- hdu4455(DP)
- hdu4455 dp
- hdu4455 dp
- hdu4455——dp
- hdu4455 线性DP
- hdu4455之树状数组+DP
- HDU4455
- HDU 4455 Substrings (DP)
- HDU 4455 Substrings(dp)
- HDU 4455 Substrings(DP)
- hdu 4455 Substrings (经典DP)
- hdu 4455 Substrings (DP 预处理思路)
- HDOJ 题目4455 Substrings(DP)
- dp-hdu-4455-Substrings
- 【DP】 HDOJ 4455 Substrings
- HDU 4455 Substrings dp
- 647. Palindromic Substrings (DP)
- .net MVC学习记录--1.MVC简介
- 格式化时间的 2016.07.14 13:22:50
- c# dictionary 给某键值赋值之前是否一定要add此键值
- Keras与各种各样的自编码器
- Activiti基础教程--03(HelloWorld)
- HDU4455 Substrings(DP)
- 简单使用Reveal:分析IOS UI界面
- 更新媒体库
- SSO-C#跨域单点登录(二)
- PAT L2-002. 链表去重 模拟
- 本地通知
- HDU 3371 Prim或kruskal实现
- eclipse - 取消英文拼音检查
- Django接受前端数据的几种方法