hdu 4455 Substrings (DP 预处理思路)「建议收藏」

hdu 4455 Substrings (DP 预处理思路)

大家好,又见面了,我是全栈君。

Substrings

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1727    Accepted Submission(s): 518




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 a
1,a
2…a
n, 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<=10
6, 0<=Q<=10
4, 0<= a
1,a
2…a
n <=10
6
 


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

 

1、 非常明显,长度为1的答案为dp[1]=n;

2、 长度为2的为dp[2]=dp[1]+x-y=7+4-1=10;

X为添加的一个数和前边不同的个数,{1,1},{1,2},{2,3},{3,4},{4,4},{4,5} 为4;

Y为去掉的不足2的区间有几个不同数字,长度为1的最后一个区间{5},须要舍去。为1;

3、

长度为3的为dp[3]=dp[2]+x-y=10+4-2=12。

X为添加的一个数和前边不同的个数,{1,1,2}。{1,2,3},{2,3,4}。{3,4,4},{4,4,5}为4;

Y为去掉的不足3的区间有几个不同数字。长度为2的最后一个区间{4,5},须要舍去,为2;

所以,我们须要得到当前数字和它上次出现的位置差的大小。详细实现看代码。。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 1000005
#define LL __int64
const int inf=0x1f1f1f1f;
int a[N],len[N],pre[N],vis[N],f[N];
LL dp[N];
int main()
{
    int i,n,m;
    while(scanf("%d",&n),n)
    {
        memset(pre,-1,sizeof(pre)); //记录一个值上次出现的位置
        memset(len,0,sizeof(len)); //len[i]:有几个间隔为i的数
        memset(dp,0,sizeof(dp));   //记录终于答案
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            len[i-pre[a[i]]]++;
            pre[a[i]]=i;
        }
        for(i=n-1;i>=0;i--)
            len[i]+=len[i+1];
        memset(f,0,sizeof(f));   //f[i]从后往前记录后i个数有几个不同值
        memset(vis,0,sizeof(vis));
        for(i=1;i<n;i++)
        {
            f[i]=f[i-1];
            if(!vis[a[n-i]])
            {
                vis[a[n-i]]=1;
                f[i]++;
            }
        }
        dp[1]=n;
        for(i=2;i<=n;i++)
            dp[i]=dp[i-1]+len[i]-f[i-1];
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&i);
            printf("%I64d\n",dp[i]);
        }

    }
    return 0;
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116273.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • es6 转es5_es6转换es5

    es6 转es5_es6转换es5为什么要es6转es5?答:es6代码在老版本的浏览器中无法执行。怎么将es6代码转为es5代码,让其在老版本的浏览器中执行?答:使用babel模块,babel是一个使用非常广泛的es6转换器,这就意味着我们可以将es6代码转为es5代码,从而在老版本的浏览器中执行。使用步骤:新建一个新的用来编写es6代码的文件夹,进入到该文件中,初始化一个项目npminit表示一步步通过配置来初始化一个项目npminit-y表示使用默认设置来快速初始化一个项目局部安装babel-cli

    2022年9月24日
    3
  • CentOS下的Apache和PHP的编译安装

    CentOS下的Apache和PHP的编译安装

    2021年8月21日
    58
  • 关于如何访问当前页面所在的iframe属性[通俗易懂]

    今天碰到个问题,在当前页面访问包含他的iframe标签中的属性。用如下方法:window.frameElement如果要id就在后边加上.id 用什么加什么。其实很简单。就是知识面太少。

    2022年4月17日
    56
  • DB4O详细介绍

    DB4O详细介绍深入db4o深入db4o这是RickGrehan发表在TheServerSide上的一篇关于面向对象数据库–db4o的文章,较全面地介绍了db4o的关键特性,希望对大家认识db4o能有所帮助。(2007.12.07最后更新)db4o-针对对象的数据库-是一个完全的对象数据库;它以使对象在其生命周期中-无论是在数据库内或是在外-都保持着它们的本性这样一种方…

    2022年7月21日
    13
  • jmeter相关面试题_jmeter面试题及答案

    jmeter相关面试题_jmeter面试题及答案一、接口测试1、接口测试流程1、首先是从开发那里拿到API接口文档,了解接口业务、包括接口地址、请求方式,入参、出参,token鉴权,返回格式等信息。2、然后使用Postman或Jmeter工具执行接口测试,一般使用Jmeter的步骤是这样的: a、首先新建一个线程组。 b、然后就是新建一个HTTP请求默认值。(输入接口服务器IP和端口) c、再新建很多HTTP请求,一个请求一个用例。(输入接口路径,访问方式,参数等) d、然后创建断言和查看结果树。3、最后调试并执行用例,最后编写接口测试报

    2022年9月30日
    3
  • 异常处理三原则_异常状态

    异常处理三原则_异常状态DRF框架的默认异常处理设置如下:默认使用模块下的函数进行异常处理自定义异常处理可以自定义异常处理函数,在DRF框架默认异常处理函数的基础上,添加一些其他的异常处理,比如数据库处理1)自定

    2022年8月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号