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的更多相关文章

  1. HDU-4455 Substrings(DP)

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

  2. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  3. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  4. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  5. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  6. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  7. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  9. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

随机推荐

  1. Bind Enum To DropDownList ?

    0. 准备测试数据 0.1 : 枚举 using System; using System.Collections.Generic; using System.Linq; using System.W ...

  2. NOJ——1274The battle of Red Cliff(并查集按秩合并)

    [1274] The battle of Red Cliff 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Zero loves palying Sanguo game very ...

  3. HDU——1257最少拦截系统(贪心)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. POJ2594Treasure Exploration(最小路径覆盖,相交)

    Treasure Exploration Have you ever read any book about treasure exploration? Have you ever see any f ...

  5. jenkins发送html测试报告

    jenkins发送html测试报告  https://blog.csdn.net/galen2016/article/details/77975965/ <!DOCTYPE html> & ...

  6. [Codeforces Round #297 Div. 2] E. Anya and Cubes

    http://codeforces.com/contest/525/problem/E 学习了传说中的折半DFS/双向DFS 先搜前一半数,记录结果,然后再搜后一半数,匹配之前结果. #include ...

  7. 【CF1016B】Segment Occurrences(模拟)

    题意:给定两个串s和t,多次询问s的一个区间[l ,r]中有多少个子串与t串相同 len<=1e3,q<=1e5 思路:前缀和 #include<cstdio> #includ ...

  8. grunt 入门 应用grunt对代码进行压缩

    1.什么是grunt grunt的官方解释是:javascript世界的构建工具. 为何要构建工具? 一句话:自动化.对于需要反复重复的任务,例如压缩(minification).编译.单元测试.li ...

  9. phpcms V9 广告模块中广告模板修改

    广告模块模板位置 \phpcms\modules\poster\install\templates\*.html 我的需求: 去掉边框控制代码,是否显示边框我将在页面模板中设置,因些需要删除模板中的以 ...

  10. ZOJ 2619: Generator

    类型:概率 + 解方程组(高斯消元法) + KMP(好吧其实我用的是暴力~)题意:你可以等概率的选择大写字母里的前n个字母,在纸上写啊写,一直到出现给定的字符串.问写的字母个数的期望.思路: 期望递推 ...