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)
上一篇 2022年1月23日 下午6:00
下一篇 2022年1月23日 下午6:00


相关推荐

  • IDEA 打不开怎么办 ?「建议收藏」

    IDEA 打不开怎么办 ?「建议收藏」IDEA打不开怎么办?系统win10IDEA2020.1.3使用chooseruntime插件修改运行环境后无法启动IDEA一、修改运行环境导致的(例如:将原本jre8修改为本地的jre11出现了问题)1、搜索idea64.exe.jdk文件。2、找到你本地原来使用的官方jre,本文本地安装的是D:\Java\jdk1.8.0_181\jre。3、将idea64.exe.jdk文件中内容修改为官方可用的。例如:将D:\Java\jdk-11.0.2\bin\

    2022年8月30日
    8
  • 编译安装httpd apache服务器

    编译安装httpd apache服务器

    2022年4月2日
    39
  • ioctl函数_通过ioctl函数设置IP不允许修改

    ioctl函数_通过ioctl函数设置IP不允许修改一、什么是ioctl  ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。  ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。  用户程序所作的只是通过命令码(cmd)告诉驱动程序它想…

    2022年10月18日
    5
  • 谷歌Nano Banana生图大模型使用指南

    谷歌Nano Banana生图大模型使用指南

    2026年3月16日
    3
  • 产品拆解图生成利器:Nano-Banana详细教程

    产品拆解图生成利器:Nano-Banana详细教程

    2026年3月15日
    4
  • java写爬虫没python好吗_java爬虫代码

    java写爬虫没python好吗_java爬虫代码写在前面前段时间实习结束回来休息,某日闲逛无意间又打开了半次元,突然看见几个好漂亮的coser小姐姐,就想存个图片当壁纸(づ ̄3 ̄)づ╭❤~然而又发现,很多小姐姐都设置了作品禁止保存或者是右下角带水印身为一个重度强迫症患者,默默打开浏览器,看看能不能偷鸡扒到原图。。。开始之前1.所有图片都是各位作者付出辛苦劳动得来的,请尊重coser版权。2.图片自己下载使用可以,请勿用于商业用途,转载请先取…

    2025年11月16日
    6

发表回复

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

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