http://acm.hdu.edu.cn/showproblem.php?pid=4455SubstringsTimeLimit:10000/5000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):2229  &nb"/>

hdu4455 dp

Posted yangykaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu4455 dp相关的知识,希望对你有一定的参考价值。

php?

pid=4455">http://acm.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): 2229    Accepted Submission(s): 695


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
/**
hdu4455  dp
题目大意:给定一个序列,求其全部长度为w的子区间中不同元素的个数之和
解题思路:这个题与其说是dp还不如说是递推好一些。首先定义c[i]表示前面近期的与其同样的数的距离是i的数的个数,sum[i]表示前面近期的与其同样的数
          的距离大于等于i的数的个数。num[i]表示最后一个长度为i的区间含有的不同数的个数。dp[1]=n,dp[i]=dp[i-1]-num[i]+sum[i];
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=1000050;
typedef long long LL;
int c[maxn],sum[maxn],num[maxn],pre[maxn];
LL dp[maxn];
int n,m,a[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        memset(pre,0,sizeof(pre));
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            c[i-pre[a[i]]]++;
            pre[a[i]]=i;
        }
        sum[n]=c[n];
        for(int i=n-1;i>0;i--)
        {
            sum[i]=sum[i+1]+c[i];
        }
        memset(c,0,sizeof(c));///数组重用
        c[a[n]]=1;
        num[1]=1;
        for(int 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(int i=2;i<=n;i++)
        {
            dp[i]=dp[i-1]-num[i-1]+sum[i];
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int x;
            scanf("%d",&x);
            printf("%I64d\n",dp[x]);
        }
    }
    return 0;
}


以上是关于hdu4455 dp的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4455 Substrings(线性dp,很有意思)

Substrings(hdu 4455)

HDU4455

4455[Zjoi2016]小星星 容斥+dp

[BZOJ4455][ZJOI2016]数星星(容斥DP)

bzoj 4455 [Zjoi2016]小星星 树形dp&容斥