HDU-4455 Substrings DP递推
来源:互联网 发布:打阿里云的投诉电话 编辑:程序博客网 时间:2024/05/22 12:17
传送门:http://acm.split.hdu.edu.cn/showproblem.php?pid=4455
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3426 Accepted Submission(s): 1050Problem 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
71 1 2 3 4 4 531230
Sample Output
71012
Source
2012 Asia Hangzhou Regional Contest
思路:
i+1的答案是i的答案减去最后一段,加上前面各段增长1增加的情况数。统计与前面相同数字的距离大于等于i的即为增加的情况数。
#include <cstdio>#include <cstring>using namespace std;const int N=1000005;int a[N];int dist[N];///与前一个相同数字距离为i的个数int distsum[N];///距离大于等于i的个数long long dp[N];///用int会爆int ls[N];int last[N];int main(){ int n; while (scanf("%d",&n)!=EOF) { if (n==0) break; memset(dist,0,sizeof(dist)); memset(last,0,sizeof(last)); memset(ls,0,sizeof(ls)); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); dist[i-last[a[i]]]++; last[a[i]]=i; } memset(last,0,sizeof(last)); last[a[n]]=1; ls[n]=1; for (int i=n-1;i>=1;i--) { if (last[a[i]]) ls[i]=ls[i+1]; else { ls[i]=ls[i+1]+1; last[a[i]]=1; } } memset(distsum,0,sizeof(distsum)); for (int i=n;i>=1;i--) distsum[i]=distsum[i+1]+dist[i]; dp[1]=n; for (int i=2;i<=n;i++) { dp[i]=dp[i-1]+distsum[i]-ls[n+2-i]; } int w; scanf("%d",&w); while (w--) { int x; scanf("%d",&x); printf("%lld\n",dp[x]); } } return 0;}
阅读全文
0 0
- HDU-4455 Substrings DP递推
- hdu 4455 Substrings 预处理+递推
- hdu 4455 Substrings(树状数组+递推)
- HDU 4455 Substrings(递推+优化)
- dp-hdu-4455-Substrings
- HDU 4455 Substrings dp
- HDU 4455 Substrings --递推+树状数组优化
- HDU 4455 Substrings (DP)
- HDU 4455 Substrings(预处理+dp)
- HDU 4455 Substrings(dp)
- HDU 4455 Substrings(DP)
- hdu 4455 Substrings(找规律&DP)
- hdu 4455 Substrings (经典DP)
- HDU 4455 Substrings DP 不同转相同
- hdu 4455 Substrings (DP 预处理思路)
- HDU 4455 Substrings 树状数组 + DP
- hdu 4455 Substrings dp+线段数组
- hdu 4455 Substrings # dp / 线段树
- BZOJ2118: 墨墨的等式 思维建图
- servlet的重定向(response.sendRedirect())
- 【机房收费系统】——任务启动
- loadrunner Lr_类函数之lr_abort()
- spring-aop---AspectJ注解式切面编程。
- HDU-4455 Substrings DP递推
- 欢迎使用CSDN-markdown编辑器
- loadrunner Lr_类函数之lr_abort()
- 配置 VirtualBox backend
- Java Thread.yield详解
- 算法第一周
- loadrunner Lr_类函数之lr_checkpoint()
- loadrunner Lr_类函数之lr_continue_on_error()
- leetcode---search-for-a-range---查找