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 ...
随机推荐
- 30分钟带你了解「消息中间件」Kafka、RocketMQ
消息中间件的应用场景 主流 MQ 框架及对比 说明 Kafka 优点 Kafka 缺点 RocketMQ Pulsar 发展趋势 各公司发展 Kafka Kafka 是什么? Kafka 术语 Kaf ...
- 六个你不知道的PR快捷键,拯救你的剪辑效率
5G时代到来,会剪辑视频的人,无论在校园还是未来步入职场都很吃香.对于普通人来说,视频处理也成为了一个通用技能.PR是我们大多数人剪辑中,经常会用到的剪辑工具,之前的文章中已经给大家总结了pr的一些提 ...
- 路由协议-RIP协议
一.路由协议的发展历程和分类 距离矢量路由协议--听信"谣言",使用跳数作为度量值,最大16(0-15)跳:RIP 链路状态路由协议--"地图"路由协议:OSP ...
- ChannelNets: 省力又讨好的channel-wise卷积,在channel维度进行卷积滑动 | NeurIPS 2018
Channel-wise卷积在channel维度上进行滑动,巧妙地解决卷积操作中输入输出的复杂全连接特性,但又不会像分组卷积那样死板,是个很不错的想法 来源:晓飞的算法工程笔记 公众号 论文: C ...
- celery 动态配置定时任务
How to dynamically add or remove tasks to celerybeat? · Issue #3493 · celery/celery https://github.c ...
- C语言中使用类似awk的功能
awk实在是太强大了, 非常复杂的任务,几行代码就可以解决, awk经常需要用到, 但是在c语言里面, 调用system不太优雅, 能不能直接在c语言中直接调用呢,简单实现了一些功能, 但大多数情况, ...
- loj2587铁人两项
无向图,图中选出定点三元组(a,b,c),a->b->c的路径没有重复边.问方案有多少? -------------------------------------------- 首先求出 ...
- Excel 一张表最多能装下多少行多少列数据?
一个工作簿可以装下255张,那么每张工作表可以装下多少行多少列数据呢? 1.任意打开或新建一个Excel文档. 2.在文档中,找到其左上角的"文件"按钮,点击选择"选项& ...
- JVM类加载与双亲委派机制被打破
前言 前文已经讲了虚拟机将java文件编译成class文件后的格式:JVM虚拟机Class类文件研究分析 java文件经过编译,形成class文件,那么虚拟机如何将这些Class文件读取到内存中呢? ...
- jackson学习之五:JsonInclude注解
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...