D - Beauty of Array

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-04-30)

Description

Edward has an array A with N integers. He defines the beauty of an array as the summation of all distinct integers in the array. Now Edward wants to know the summation of the beauty of all contiguous subarray of the array A.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

Output

For each case, print the answer in one line.

Sample Input

3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

Sample Output

105
21
38
题目描述: 求一段序列中连续子序列的和,如果这段连续子序列中有重复的话,(the summation of all distinct integers in the arra),这个序列中不相同的数的和,也就是说只计算一次。
如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;
我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;
,如果重复的话,假设1 2 3 4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(i,i-2),(i,i-3)。。。。(i,j+1)这些连续子序列的值可以加上a的值;
(i,j),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的i,j位置上的值,因为只需要算一次,所以不需要给这些以i
结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;
如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
#define M 100100
using namespace std;
LL dp[M];
LL visit[M];
void init()
{
memset(dp,0,sizeof(dp));
memset(visit,0,sizeof(visit));
}
void solve()
{
int n;
LL data;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&data);
dp[i]=dp[i-1] + data +(i-1-visit[data]) * data;
visit[data]=i;
}
LL answer=0;
for(int i=1;i<=n;i++)
answer+=dp[i];
printf("%lld\n",answer);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
solve();
}
// system("pause");
return 0;
}

  

zoj 3872的更多相关文章

  1. DP ZOJ 3872 Beauty of Array

    题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...

  2. ZOJ 3872 Beauty of Array

    /** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...

  3. ZOJ 3872 浙江2015年省赛试题

    D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu S ...

  4. ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )

    对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...

  5. ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...

  6. ZOJ 3872 Beauty of Array【无重复连续子序列的贡献和/规律/DP】

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

  7. ZOJ 3872 计算对答案的贡献

                                                   D - Beauty of Array Description Edward has an array A ...

  8. Beauty of Array ZOJ - 3872(思维题)

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

  9. ZOJ 3872 Beauty of Array 连续子序列求和

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

随机推荐

  1. CSS图片居中,多余隐藏

    /*外层DIV*/ div {position: relative;overflow:hidden;width: 显示宽度px;} /*left=50%刚好在中间,margin-left=往前移动图片 ...

  2. 几则js表达式

    过滤大段文本里的标签.标签格式 <...>,如下匹配标签然后替换成空 校验邮箱是否符合: 去掉行首行尾空格: 检测字符串是否包含中文:(utf8编码)

  3. PAT 组合数的和(15)

    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字.要求所有可能组合出来的2位数字的和.例如给定2.5.8,则可以组合出:25.28.52.58.82.85,它们的和为330. 输入 ...

  4. Django使用富文本编辑器

    1.下载kindeditor 网址:http://kindeditor.net/demo.php2.解压到项目中 地址:\static\js\kindeditor-4.1.103.删除没用的文件 例如 ...

  5. linux c编程:线程退出

    在线程创建的时候pthread_exit都是调用的固定参数,我们先来看下如果用自动变量作为pthread_exit的参数时出现的问题 typedef struct foo{ int a; int b; ...

  6. 电脑Svchost.exe 进程占CPU100% 的解决办法

    Windows Update诊断和修复修复工具 http://support.microsoft.com/mats/windows_update/zh-cn Svchost.exe占用CPU100%的 ...

  7. RLearning第1弹:初识R语言

    R作为一种统计分析软件,是集统计分析与图形显示于一体的.体积小.开源.很强的互动性.自从学了R本人就很少再用matlab了... 一.R语言由函数和赋值构成. R使用<-(最好养成使用习惯),而 ...

  8. Please install Android target

    今天在执行ionic build android时出现以下错误: [Error: Please install Android target: "android-22". Hint ...

  9. [笔记]几个简单有用的PHP函数

    收藏几个简单的PHP函数,分别用于对象到数组转换.json到php数组转换功能.curl模拟POST以及根据链接获取内容.不直接使用json_decode()的原因是php json_decode() ...

  10. Mysql的空值与NULL的区别

    Mysql数据库是一个基于结构化数据的开源数据库.SQL语句是MySQL数据库中核心语言.不过在MySQL数据库中执行SQL语句,需要小心两个陷阱. 陷阱一:空值不一定为空 空值是一个比较特殊的字段. ...