HDU 4455.Substrings
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3240 Accepted Submission(s): 990
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
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
1 1 2 3 4 4 5
3
1
2
3
0
10
12
拿样例写一下
1: {1}, {1}, {2}, {3}, {4}, {4}, {5}
2: {1,1}, {1,2}, {2,3}, {3,4}, {4,4}, {4,5}
3: {1,1,2}, {1,2,3}, {2,3,4}, {3,4,4}, {4,4,5}
4: {1,1,2,3}, {1,2,3,4}, {2,3,4,4}, {3,4,4,5}
...
容易发现可以得出一个发现:
长度为w的值肯定包含长度为w-1的值减去最后一个字数组的权值和
例:
2: {1,1} , {1,2} , {2,3} , {3,4} , {4,4} , {4,5}
3: {1,1,} , {1,2,} , {2,3,} , {3,4,} , {4,4,}
dp[3] = dp[2] - 权值[{4,5}] + ?
而'?'就表示添加上那些标蓝色的数之后要添加的值.
设某个标蓝色的数为a[i],另外一个和它相等的且和它最近的数为a[j],且i>j
如果i-j>w的话那么添上这个标蓝的数就可以使得dp[w]+1
所以我们只要求出cnt[dis]即可,dis既某数离在它之前且相等的且和它最近的数的距离.
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=1e6+,inf=0x3f3f3f3f,mod=1e9+;
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
int a[MAXN];
int cnt[MAXN],dp[MAXN];
ll sign[MAXN];
void run(int n)
{
memset(cnt,,sizeof(cnt));
memset(sign,,sizeof(sign));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
int x=i-sign[a[i]];
cnt[i-sign[a[i]]]++;
sign[a[i]]=i; ///sign[i]表示i的位置
//cout<<x<<" "<<cnt[x]<<endl;
}
memset(dp,,sizeof(dp));
memset(sign,,sizeof(sign));
for(int i=n,t=; i>; i--,t++)
{
if(sign[a[i]]) dp[t]=dp[t-];
else dp[t]=dp[t-]+,sign[a[i]]=; ///sign[i]标记i是否出现过
}
memset(sign,,sizeof(sign));
int t=n;
for(int i=; i<=n; i++)
{
sign[i]=sign[i-]-ll(dp[i-]);
t-=cnt[i-];
sign[i]+=t;
}
}
int main()
{
int n;
while(scanf("%d",&n)&&n!=)
{
run(n);
int q;
scanf("%d",&q);
while(q--)
{
int w;
scanf("%d",&w);
cout<<sign[w]<<endl;
}
}
return ;
}
HDU 4455.Substrings的更多相关文章
- hdu 4455 Substrings(计数)
题目链接:hdu 4455 Substrings 题目大意:给出n,然后是n个数a[1] ~ a[n], 然后是q次询问,每次询问给出w, 将数列a[i]分成若干个连续且元素数量为w的集合,计算每个集 ...
- hdu 4455 Substrings(找规律&DP)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 4455 Substrings (DP 预处理思路)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4455 Substrings[多重dp]
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU - 4455 Substrings(非原创)
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct ...
- HDU 4455 Substrings --递推+树状数组优化
题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...
- HDU 4455 Substrings ( DP好题 )
这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
http://acm.hdu.edu.cn/showproblem.php?pid=4455 https://icpcarchive.ecs.baylor.edu/index.php?option=c ...
- Substrings(hdu 4455)
题意: 给定一个序列ai,个数为n.再给出一系列w:对于每个w,求序列中,所有长度为w的连续子串中的权值和,子串权值为子串中不同数的个数. /* dp[i]表示长度为i的序列不同元素个数之和. 考虑从 ...
随机推荐
- java-学习1
作为一个想要深入的程序猿,只是学习前端是不够的,我总结我的前端工作是围绕着html.css.js展开写的再好也是展现在表面,所以 我想学习一门能够深入的后台语言,想来想去我还是选择java作为以后深入 ...
- jQuery写省级联动列表,创造二维数组,以及如何存/调用二维数组中的数据
jQuery写省级联动列表,创造二维数组来存放数据,然后通过each来遍历调用,通过creatTxtNode创建文本节点,通过createElement创建标签option,在通过append将文本写 ...
- 数据持久化PlayerPrefs
1.Unity3D中的数据持久化是以键值对的形式存储的,可以看作是一个字典 2.Unity3D中的值是通过键名来读取的,当值不存在时,返回默认值 3.在Unity中只支持int.float.strin ...
- msf客户端渗透(四):关闭UAC,hash、防火墙、服务、磁盘、DEP、防病毒、远程桌面、桌面截图、获取tooken
关闭UAC 如果你获得一个system权限的session 进入到这个session 进入到shell 依次输入以下命令 cmd.exe /k %windir%\System32\reg.exe AD ...
- Hibernate一对多单向关联和双向关联映射方法及其优缺点 (待续)
一对多关联映射和多对一关联映射实现的基本原理都是一样的,既是在多的一端加入一个外键指向一的一端外键,而主要的区别就是维护端不同.它们的区别在于维护的关系不同: 一对多关联映射是指在加载一的一端数据的同 ...
- python--第六天总结
执行系统命令 可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 co ...
- SQL Server 2008 R2官方中文版下载
SQL Server 2008是一个重大的产品版本,它推出了许多新的特性和关键的改进,使得它成为至今为止的最强大和最全面的SQL Server版本. 在现今数据的世界里,公司要获得成功和不断发展,他们 ...
- Mac上安装Git
转载注明出处:http://blog.csdn.net/xiaohanluo/article/details/53214933 Git安装 下载Git有两种方法 直接下载安装包,Git下载地址 用ho ...
- 链表中倒数第k个结点(python)
题目描述 输入一个链表,输出该链表中倒数第k个结点 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.v ...
- OpenVPN 2.2.1 之后期维护
一.Openvpn 用户注销 每个公司都会用员工离职,因此注销vpn用户也就成了运维人员日常工作的一部分. 其实Openvpn在设计的时候也想到了这点,我们可以使用 revoke-full shell ...