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

<h4< dd="">Output

For each case, print the answer in one line.

<h4< dd="">Sample Input

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

<h4< dd="">Sample Outpu1021

38

这题题意的理解有一定难度,本人借鉴了一位大神的博客,得知大致的题意如下:
这个题的意思就是给n个数,求这n个数的子序列中不算重复的数的和。
比如第二个样例他的子序列就是{2},{2,3},{2,3,3},{3},{3,3},{3};
但每个子序列中重复的元素不被算入,所以他们的总和就是2+5+5+3+3+3=21;
但是这题的数据是1e5,用暴力肯定会超时,又因为每个子序列之间都有一定的递推关系,每个序列可以分解为无数个子序列。
依此,可以推断该题用dp写。
dp转移方程怎么推?用一个例子比较好解释,还用上面的例子给定4个数,分别是2 3 3 2,存入nu[Max]数组中,用dp[Max]记录子序列和(即前缀和)。
从第一个数往后推每一种可能性,则是:
                          2
                 3       3 2
          3     3 3     3 3 2
    2    2 3    2 3 3    2 3 3 2
   dp[1]  dp[2]   dp[3]     dp[4]

由上可知,dp[i]=dp[i-1]+nu[i]*i;
但该题要求去掉子序列中的重复元素,该怎么去呢?
我们继续利用上例的数据进行去重处理,可得:
                        2
               3       3 2
        3      3     3 2
  2    2 3    2 3     3 2
 dp[1]  dp[2]   dp[3]     dp[4]

如上,每一个序列中都没有重复的元素了
由上两图对比可知,可以想到用一个book[Max*10]数组记录每一个元素最后出现的位置,把nu[i]*i改成nu[i]*(i-book[nu[i]])
即转移方程改为:dp[i]=dp[i-1]+nu[i]*(i-book[nu[i]]);然后把dp[n]中的值相加即可;

这题还有个坑点,对dp[n]求和会爆int,应用long long;

代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int M = 111111;
long long book[M*10],dp[M],nu[M];
int main(){
int t;
cin>>t;
while(t--){
long long ans=0;
memset(book,0,sizeof(book));
memset(dp,0,sizeof(dp));
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>nu[i]; }
for(int i=1;i<=n;i++){
dp[i]=dp[i-1]+nu[i]*(i-book[nu[i]]);
book[nu[i]]=i;
ans+=dp[i];
}
cout<<ans<<endl;
}
return 0;
}

参考博客:http://blog.csdn.net/zhao5502169/article/details/70037098  



zoj-3872 Beauty of Array (dp)的更多相关文章

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

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

  2. DP ZOJ 3872 Beauty of Array

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

  3. ZOJ 3872 Beauty of Array

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

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

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

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

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

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

  7. zoj 3706 Break Standard Weight(dp)

    Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 ...

  8. Codeforce 1155D Beautiful Array(DP)

    D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...

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

随机推荐

  1. windows下部署Grafana +prometheus平台监控

      1.Prometheus简介 Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖.用户只需要下载对应平台包,解压并且添加基本的配置即可正常启Prometheus S ...

  2. 浅析Linux启动流程

    Linux系统启动流程 Linux 系统的启动,从计算机开机通电自检开始,一直到登陆系统,需要经历多个过程.了解 Linux 的启动过程,有助于了解 Linux 系统的结构,也对系统的排错有很大的帮助 ...

  3. mysql事务测试

    mysql事务测试 打开mysql的命令行,将自动提交事务给关闭 --查看是否是自动提交 1表示开启,0表示关闭 select @@autocommit; --设置关闭 set autocommit ...

  4. 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录。

    1 import java.text.SimpleDateFormat; 2 3 import org.apache.hadoop.fs.FSDataOutputStream; 4 import or ...

  5. 初识JavaScript和jQuery

    JavaScript 1.特性 ①脚本语言.JavaScript是一种解释型的脚本语言,C.C++.Java等语言先编译后执行, 而JavaScript是在程序的运行过程中逐行进行解释. ②基于对象. ...

  6. 深度漫谈数据系统架构——Lambda architecture

    https://mp.weixin.qq.com/s/whmhm2yzug2WVdH3dTq8hg

  7. all header field names in both HTTP requests and HTTP responses are case-insensitive.

    https://tools.ietf.org/html/rfc6455#section-4.2.1 Please note that according to [RFC2616], all heade ...

  8. 理解和运用 ClassLoader 该篇文章就够了

    定义 根据<深入理解Java虚拟机>提到"通过一个类的全限定名(packageName.ClassName)来获取描述此类的二进制字节(class文件字节)这个动作的代码模块就叫 ...

  9. Spring Cloud与Eureka

    Spring Cloud与Eureka 一.使用SpringCloud注册中心Eureka 1.1 Eureka和Zookeeper对比 1.1.1 Zookeeper保证CP 1.1.2 Eurek ...

  10. Java面试(解答题二)

    1.一个用户具有多个角色,请查询出该表中具有该用户的所有角色的其他用户.备注:用户表:tb,角色字段为role,主键为id.请写出sql语句. 解答: 2.概述MVC体系结构 解答: MVC包括三类对 ...