您的位置:首页 > 其它

杭电acm 4455(dp)

2017-08-10 22:01 274 查看


Substrings

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3320    Accepted Submission(s): 1024


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

 

Source

2012 Asia Hangzhou Regional Contest

题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数。m次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和。

题目分析:定义dp(len)表示所有长度为len的连续区间的egg之和。

则,dp(len)=dp(len-1)-egg(最后一个长度为len-1的连续区间)+每个长度为len的区间中的新增元素不重复的区间个数。

想法:

egg(最后一个长度为len-1的连续区间)用num【i】表示。

每个长度为len的区间中的新增元素不重复的区间个数用sum【i】表示。

用c【i】来统计有多少个元素与前面距离i的元素相等(或与距离i的元素为第一元素)。

用pre【i】来之前值为i的元素的位置。

把以上数组初始化后+ dp[i]=dp[i-1]-num[i-1]+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)&&n)

    {

        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
=c
;

        for(int i=n-1;i>0;i--)

        {

            sum[i]=sum[i+1]+c[i];

        }

        memset(c,0,sizeof(c));//数组重用

        c[a
]=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;

}

代码二:简化sum【】

#include<stdio.h>

#include<string.h>

# define LL long long

const int N=1000000;

int a[N+5];

int A[N+5];

int f[N+5];

int cnt[N+5];

LL dp[N+5];

int main()

{

    int n;

    while(scanf("%d",&n)&&n)

    {

        memset(f,0,sizeof(f));

        memset(cnt,0,sizeof(cnt));

        for(int i=1;i<=n;i++)

        {

            scanf("%d",a+i);

            cnt[i-f[a[i]]]++;

            printf("%d\t",i-f[a[i]]);

            f[a[i]]=i;

        }

        printf("\n");

        for(int i=1;i<=n;i++)

        {

            printf("%d\t",cnt[i]);

        }

        printf("\n");

        for(int i=1;i<=n;i++)

        {

            printf("%d\t",f[i]);

        }

        printf("\n");

        memset(f,0,sizeof(f));

        A[n+1]=0;

        for(int i=n;i>=1;i--)

        {

            if(f[a[i]]) A[i]=A[i+1];

            else A[i]=A[i+1]+1;

            f[a[i]]=1;

        }

        dp[1]=n;

        int sum=n;

        for(int i=2;i<=n;i++)

        {

            dp[i]=dp[i-1]-A[n-i+2];

            sum-=cnt[i-1];

            dp[i]+=sum;

        }

        int m,x;

        scanf("%d",&m);

        while(m--)

        {

            scanf("%d",&x);

            printf("%lld\n",dp[x]);

        }

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: