Substrings

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

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
 
Source
 

题意:

给你一个数组{a1,a2,a3........an}。然后定义了一个询问。给你一个w。要你求出。所有a[i+1],a[i+2]......a[i+w]。中不同元素个数的和。i+w<=n。

思路:

dp[i]表示w为i时的答案。我们考虑dp[i+1]即长度增加一个后的情况。

对于n=3时。dp[3]已知

[1 1 2 ]3 4 4 5

对于n=4时。

[1 1 2 3] 4 4 5

相当于在以前三元集中加入一个数。所以如果该元素出现过dp[4]的值和dp[3]值一样。

如果不一样就要在dp[3]的基础上减一个。但是对于数组的最后三个数已经不能算作dp[4]的值。但dp[3]把它们

算作在内。所以要把他们减出来。我们对于数组中的每一个数。维护一个数组pre[v]表示数值v上次出现的位置。那么i-pre[v]即两数的不重复区间长度。

哎。。。比赛时居然为一个__int64卡了半天。还是不专业呀。都没有分析数据范围。。。还是最后十分钟做出来的。

不然应该有机会做做其它题目的。

详细见代码:

#include <iostream>
#include<string.h>
#include<stdio.h> using namespace std;
const int maxn=1000010;
int a[maxn],pre[maxn],len[maxn];
__int64 dp[maxn],rest;//注意数据范围呀!!
bool vis[maxn];
int main()
{
int n,q,w,i; while(scanf("%d",&n),n)
{
memset(pre,-1,sizeof pre);
memset(len,0,sizeof len);
memset(vis,0,sizeof vis);
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];//len[i]代表长度大于等于i的个数
dp[0]=0;
dp[1]=n;
rest=1;
vis[a[n-1]]=true;
for(i=2;i<=n;i++)
{
dp[i]=dp[i-1]+len[i]-rest;//rest为长度不足i的部分
if(!vis[a[n-i]])
{
rest++;
vis[a[n-i]]=true;
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&w);
printf("%I64d\n",dp[w]);
}
}
return 0;
}

hdu 4455 Substrings(找规律&DP)的更多相关文章

  1. hdu 4455 Substrings(计数)

    题目链接:hdu 4455 Substrings 题目大意:给出n,然后是n个数a[1] ~ a[n], 然后是q次询问,每次询问给出w, 将数列a[i]分成若干个连续且元素数量为w的集合,计算每个集 ...

  2. HDU 2086 A1 = ? (找规律推导公式 + 水题)(Java版)

    Equations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2086 ——每天在线,欢迎留言谈论. 题目大意: 有如下方程:Ai = (Ai-1 ...

  3. hdu 5047 大数找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...

  4. Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)

    题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...

  5. hdu 4455 Substrings (DP 预处理思路)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. HDU 4455 Substrings[多重dp]

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU 4455 Substrings ( DP好题 )

    这个……真心看不出来是个DP,我在树状数组的康庄大道上欢快的奔跑了一下午……看了题解才发现错的有多离谱. 参考:http://www.cnblogs.com/kuangbin/archive/2012 ...

  8. HDU 4455.Substrings

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

随机推荐

  1. matlab如何写一个类

    类是一种数据类型,与普通的数据类型不同的是类不仅包含数据,还包含对数据的操作,类把数据和数据操作方法封装在一起,作为一个整体参与程序的运行.类具有可继承性,创建一个新的类的时候,可以在一个基类中添加成 ...

  2. JavaScript的日期处理

    很久前写的代码了,偶尔看到贴出来做个备忘,写得有点乱,懒得整理了. // 根据初始日期和滚动周期及滚动次数来计算终止日期,日期滚动周期,// 可以是每天(DAY).每周(WEEK).每月(MONTH) ...

  3. HTML界面JQuery ajax 返回200,但走Error方法

    原因是JSON拼装的有问题. 都需要放在双引号里面,或者修改dataType的类型为  "html". http://blog.csdn.net/imjcoder/article/ ...

  4. 望大神批评教育国庆无聊之作:ObjectValidator

    起因: 本人国庆无聊,不知道干嘛, 所以模仿FluentValidation写了个简化版的ObjectValidator 个人设想是能用类似fluent的方式创建验证规则,然后使用者缓存并验证自己的对 ...

  5. JAVA GC之标记 第五节

    JAVA GC之标记  第五节 OK,我们继续昨天最后留下的问题,什么是标记?怎么标记? 第一个问题相信大家都知道,标记就是对一些已死的对象打上记号,方便垃圾收集器的清理. 至于怎么标记,一般有两种方 ...

  6. CEvent,CSemaphore,CCriticalSection,CMutex

    一.用CEvent实现线程同步 事件对象(Event)是最简单的同步对象,它包括有信号和无信号两种状态.在线程访问某一资源之前,也许需要等待某一事件的发生,这时用事件对象最合适.例如,只有在通信端口缓 ...

  7. QT正则表达式

    QT正则表达式有一个问题,当初始状态是不符合正则表达式时,能够输入任意字符,若在输入过程中符合正则表达式,马上进入字符检测状态,即只能接受符合正则表达式的字符.

  8. 编程实现任意长度整数的加法(整数可以长度超出C++中int范围)

    #include <iostream> #include<string> using namespace std; string add(string s1,string s2 ...

  9. leetcode Minimum Depth of Binary Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  10. JavaWeb核心编程之(三.1)ServletHello

    Servlet简介Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servlet和客户的通信以及调用Servlet方法, 在这里Servlet ...