Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520

根据题目的要求,需要算出所有连续子数组的the beauty的总和。

那么要求这个这个总和,刚开始最容易想到的就是这样:

for( int i=1; i<=N; i++ ) {
for( int j = 1; j<=i; j++ ) {
... //排除重复的数计算总和
}
}

这样子的结果实际上是 Time Limit Exceeded

因此采取这种方法是不当的,那么继续思考:

先思考没有重复的情况

例如:1,2 可以有子数组 1(1), 1,2(2),2(2),三个子数组,括号内为该子数组的beauty,

如果后面在加上一个3呢?可以得到子数组1(1), 1,2(2),2(2),1,2,3(6),2,3(5),3(3),可见增加了3个3,之所以是3个3。是因为这个3处在第三个位置。

同理,如果再增加一个数,形成数组列1,2,3,4,那么和1,2,3相比,其总和应该增加4个4

可以得出,没有重复的长度为n的数组,如果在第n+1个位置,再增加一个不重复的数x,则与没有重复的长度为n的数组的总beauty要增加x*(n+1)

既然得出了这一点,那么考虑存在重复的情况:

在1,2,3,2,到第四个数,即2时,新增的序列为1,2,3,2   ,   2,3,2,  3,2,  2,但是和前一个3相比,以这个2为结尾的序列总beauty值增加了2*2,因为这个2与上一个2距离为2.

最后可以得出,每读进一个数,就可以根据前一个数算出以这个数为结尾的数列的beauty值。可以使默认每一个数的初始位置为0,之后进行更新。

最后计算总和就行了。

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; const int NUM_MAXN = 1000000 + 50; // the max number
long long value[ NUM_MAXN ]; // the sub ending with i position can totally get value[i] beauty
// Attention : long long
int pos[ NUM_MAXN ]; // the number i in the pos[i] position int main() {
int T;
cin >> T;
while( T -- ) {
int N;
scanf( "%d", &N );
memset( pos, 0, sizeof(pos) );
value[ 0 ] = 0;
int tmp;
for( int i=1; i<=N; i++ ) {
scanf( "%d", &tmp );
value[i] = value[ i-1 ] + ( i-pos[ tmp ] ) * tmp;
pos[ tmp ] = i;
}
long long sum = 0;
// Attention : long long
for( int i=1; i<=N; i++ ) {
sum = sum + value[ i ];
}
// Attention : long long
printf( "%lld\n", sum );
}
return 0;
}

Zoj 3842 Beauty of Array的更多相关文章

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

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

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

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

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

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

  7. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  8. ZOJ 3872: Beauty of Array(思维)

    Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...

  9. 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏

    Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...

随机推荐

  1. linux多线程示例

    #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h& ...

  2. iOS网络开发-打造自己的视频客户端

    一.展示实现 效果      客户端:                                      服务器端:            二.创建表 create table CourseV ...

  3. Android应用开发基础篇(9)-----SharedPreferences

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/27/2370319.html 一.概述 对于SharedPreferences,我吧它理解为一种 ...

  4. 【转载】Qt中的QString,QByteArray,Qchar, char*

    先要说的是QString.         之所以把QString单独拿出来,是因为string是很常用的一个数据结构,甚至在很多语言中,比如JavaScript,都是把string作为一种同int等 ...

  5. BZOJ 1050: [HAOI2006]旅行comf( 并查集 )

    将edge按权值排序 , O( m² ) 枚举边 , 利用并查集维护连通信息. ------------------------------------------------------------ ...

  6. load_library(linker.cpp:759): library "libmaliinstr.so" not found

    1.02-07 15:19:09.277: E/linker(16043): load_library(linker.cpp:759): library "libmaliinstr.so&q ...

  7. Asp.net Role manager tutorial

    It is very useful in .net we can user framework provided role manager, and easily configure in Web.C ...

  8. PSAM卡

    PSAM卡    终端安全控制模块,符合<中国金融集成电路(IC卡)PSAM卡规范>,   包括普通PSAM卡和高速PSAM卡. PSAM符合以下标准及规范: 识别卡,带触点的集成电路卡标 ...

  9. QString类的使用(无所不包,极其方便)

    Qt的QString类提供了很方便的对字符串操作的接口. 使某个字符填满字符串,也就是说字符串里的所有字符都有等长度的ch来代替. QString::fill ( QChar ch, int size ...

  10. 在qt下获取屏幕分辨率

    1,在Windows下可以使用 GetSystemMetrics(SM_CXSCREEN);GetSystemMetrics(SM_CYSCREEN) 获取.   2,在Linux下可以使用XDisp ...