HDOJ 题目4455 Substrings(DP)
来源:互联网 发布:ubuntu设置dns地址 编辑:程序博客网 时间:2024/05/18 01:10
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2592 Accepted Submission(s): 801
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
Recommend
We have carefully selected several similar problems for you: 5551 5550 5549 5548 5547
题目大意:给个n,下边n个数表示这个序列,然后一个q,q个询问,每个询问一个w,问所有长度的w的连续的子序列中不同数的个数和
思路:http://blog.csdn.net/a601025382s/article/details/12283581
ac代码
153681062015-11-03 19:46:04Accepted44551404MS29128K1221 BC++XY_
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define LL long longusing namespace std;int a[1000100],c[1000100],sum[1000100],num[1000100];int pre[1000100];LL dp[1000100];int main(){ int n; while(scanf("%d",&n)!=EOF,n) { int i; memset(pre,0,sizeof(pre)); memset(c,0,sizeof(c)); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { // scanf("%d",&a[i]); c[i-pre[a[i]]]++; pre[a[i]]=i; } sum[n]=c[n]; for(i=n-1;i>=1;i--) sum[i]=sum[i+1]+c[i]; num[1]=1; memset(c,0,sizeof(c)); c[a[n]]++; for(i=2;i<=n;i++) { if(c[a[n-i+1]]==0) { num[i]=num[i-1]+1; c[a[n-i+1]]=1; } else num[i]=num[i-1]; } dp[1]=n; for(i=2;i<=n;i++) dp[i]=dp[i-1]-num[i-1]+sum[i]; int q; scanf("%d",&q); while(q--) { int w; scanf("%d",&w); printf("%lld\n",dp[w]); } }}
0 0
- HDOJ 题目4455 Substrings(DP)
- 【DP】 HDOJ 4455 Substrings
- HDU 4455 Substrings (DP)
- HDU 4455 Substrings(dp)
- HDU 4455 Substrings(DP)
- hdu 4455 Substrings (经典DP)
- hdu 4455 Substrings (DP 预处理思路)
- dp-hdu-4455-Substrings
- HDU 4455 Substrings dp
- HDU4455 Substrings(DP)
- HDOJ 题目3555 Bomb(数位dp)
- HDOJ 题目3555 Bomb(数位dp)
- HDOJ 题目4833 Best Financing(DP)
- HDOJ 题目3555 Bomb(数位DP)
- HDOJ 题目4714 Tree2cycle(树形DP)
- HDOJ 题目4055 Number String(DP)
- HDOJ 题目1158Employment Planning(DP)
- HDOJ 题目3853 LOOPS(概率DP)
- Bit String Reordering(模拟题)
- 醉醒间
- cf #2 A. Winner
- Linux下安装JDK
- lightoj 1224(trie)
- HDOJ 题目4455 Substrings(DP)
- Inna and New Matrix of Candies(CodeForces - 400B)
- 【noip冲刺赛】:循环整数
- 64位CentOS6.5下安装hive
- IOS 下载、查询等
- Android RelativeLayout布局位置属性
- javascript转到新的页面
- Visualizing Chess Data With ggplot
- 如何添加一个VFS接口