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 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)的更多相关文章
- ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )
对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- ZOJ 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
- ZOJ 3872: Beauty of Array(思维)
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...
- ZOJ 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...
- 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 ...
- zoj 3706 Break Standard Weight(dp)
Break Standard Weight Time Limit: 2 Seconds Memory Limit: 65536 ...
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
- 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 ...
随机推荐
- Django-http协议
Http协议:超文本传输协议(应用层程序).它是客户端和服务端请求和应答的标准.Http的请求响应模型:1.客户端连接到web服务器一个http客户端,与web服务器的http端口(默认是80)建立了 ...
- API服务接口签名代码与设计,如果你的接口不走SSL的话?
在看下面文章之前,我们先问几个问题 rest 服务为什么需要签名? 签名的几种方式? 我认为的比较方便的快捷的签名方式(如果有大神持不同意见,可以交流!)? 怎么实现验签过程 ? 开放式open ap ...
- 电脑打不开gitHub的解决方法
电脑打不开gitHub的解决方法 方法:修改本地的hosts文件 因为Github是国外网站,所以经常会遇到打不开的问题,并且有时能打开但是网速好慢 解决这个问题的方法是 : C:\Windows ...
- GRPC Health Checking Protocol Unavailable 14
https://github.com/grpc/grpc/blob/master/doc/health-checking.md GRPC Health Checking Protocol Health ...
- (006)每日SQL学习:关于to_char函数
to_char函数的官方文档说明: 详细to_char请移步:https://www.cnblogs.com/reborter/archive/2008/11/28/1343195.html 需求:n ...
- 等待 Redis 应答 Redis pipeline It's not just a matter of RTT
小结: 1.When pipelining is used, many commands are usually read with a single read() system call, and ...
- http2 http1 对比
RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2) https://tools.ietf.org/html/rfc7540#page-4 ...
- JAD 反编译
自动拆装箱 对于基本类型和包装类型之间的转换,通过xxxValue()和valueOf()两个方法完成自动拆装箱,使用jad进行反编译可以看到该过程: public class Demo { publ ...
- loj10173
炮兵阵地 司令部的将军们打算在 N×M 的网格地图上部署他们的炮兵部队.一个 N×M的地图由 N 行 M 列组成,地图的每一格可能是山地(用 H 表示),也可能是平原(用 P表示),如下图.在每一格平 ...
- SpringCloud及其组件详解
SpringCloud及其组件详解 1.Spring Cloud 1.1 Spring Cloud和Dubbo的区别图解 1.2 微服务的技术栈 2.Spring Cloud 概述 2.1 Sprin ...