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. 设计模式入门之桥接模式Bridge

    Abstraction:抽象部分的父类,定义须要实现的接口.维护对实现部分的引用,从而把实现桥接到Implementor中去 Implementor:实现部分的接口 RefinedAbstractio ...

  2. 优化器的使用oracle ---explain plan

    如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...

  3. .NET 多语言支持解决方案 (转)

    asp.net 2.0中的App_GlobalResources可以用来解决本地化的问题,程序会根据浏览器的语言首选项自动判断显示出本地化的界面. 首先在App_GlobalResources新建re ...

  4. ThinkPHP第十五天(setField、setInc、setDec、关联模型)

    1.ThinkPHP中的比较特殊连贯操作 如果要更新某个字段可以用setField方法,比如M('user')->where('id=1')->setField('username','T ...

  5. 如何实现HTTPSERVER

    Write your own http server author : Kevin Lynx Why write your own? 看这个问题的人证明你知道什么是http server,世界上有很多 ...

  6. A Byte of Python 笔记(2)基本概念:数、字符串、转义符、变量、标识符命名、数据类型、对象

    第4章 基本概念 字面意义上的常量 如5.1.23.9.23e-3,或者 'This is a string'."It's a string!" 字符串等 常量,不能改变它的值 数 ...

  7. <转> Python的优雅技巧

    枚举 不要这么做: 全选复制放进笔记 i = 0 for item in iterable: print i, item i += 1 而是这样: 全选复制放进笔记 for i, item in en ...

  8. IOS 特定于设备的开发:UIDevice

    UIDevice类展示了一些关键的特定于设备的属性,包括使用的iPhone ,Ipad或iPod Touch型号.设备名称.以及OS名称和版本. 他是一种一站式解决方案,用于提取出某些系统详细信息.每 ...

  9. IOS 学习笔记(1) 视图UIViewController

    1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...

  10. QT 获取文件MD5值

    /* 方法1 */ QFile theFile(fileNamePath); theFile.open(QIODevice::ReadOnly); QByteArray ba = QCryptogra ...