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 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
 

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;
}

最新文章

  1. Android ant自动打包脚本:自动替换友盟渠道、版本号、包名
  2. jdbc工具类封装
  3. js/jQuery判断浏览器名称、内核版本、浏览器壳
  4. LPC4370 ACDHS speed and DMA
  5. 【项目相关】MVC中使用WebUploader进行图片预览上传以及编辑
  6. ActiveX控件的基本操作方法以及如何在VS2010下使用控件
  7. jq使用手册
  8. Java Web编程的主要组件技术——Hibernate入门
  9. xml 实现圆形图 和 椭圆形图
  10. 在 SharePoint 2010 中访问数据
  11. 西南民大oj(两园交求面积)
  12. HTML知识点总结之表单元素
  13. 记一次Eureka启动报Failed to start bean &#39;eurekaAutoServiceRegistration&#39; 。。。错误
  14. 解决OrangePi 耳机孔没有声音
  15. 很好的一篇eureka的讲解文章
  16. tomcat安装apr优化
  17. 模块二 hashlib模块、configparser模块、logging模块
  18. Java类、属性、方法、构造方法、块、内部类的基本概念
  19. Java框架知识点总结
  20. 微软工程师主讲的SqlServer2005视频教程

热门文章

  1. stdcall、cdecl、fastcall、thiscall 、naked call的汇编详解
  2. centos php扩展开发流程
  3. 解决TCP网络传输“粘包”问题
  4. android armeabi与armeabi-v7a
  5. Oracle中查询各种对象的方法小结
  6. list和用vector区别(Vector相当于是数组,读写快,插入慢)
  7. mysql 表级锁
  8. 浅尝key-value数据库(一)——一览NoSQL
  9. Light OJ 1318 Strange Game 组合数+高速幂+分解因子
  10. easyUI相关知识