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 ...
随机推荐
- C++:标准I/O流
标准I/O对象:cin,cout,cerr,clog cout; //全局流对象 输出数据到显示器 cin; //cerr没有缓冲区 clog有缓冲区 cerr; //标准错误 输出数据到显示器 cl ...
- 集成多种协议、用于 USBC 端口的快充协议芯片IP2723
1. 特性 快充规格 集成 QC4/QC4+输出快充协议 - 兼容 QC2.0/QC3.0 - 支持 Class B 电压等级 集成 FCP 输出快充协议 集成 SCP 输出快充协议 集成 ...
- linux下安装zsh和p10k的详细过程
目录 下载zsh 下载oh-my-zsh 切换shell 下载p10k 下载zsh sudo apt-get install zsh sudo apt-get install git 下载oh-my- ...
- uni-app开发经验分享十六:发布android版App的详细过程
开发环境 1. Android Studio下载地址:Android Studio官网 OR Android Studio中文社区 2. HBuilderX(开发工具) 3. App离线SDK下载:最 ...
- Py数据类型—整形与字符串
数据类型 在指针的右边输入.可以触发功能列表: 数字(整形):也就是123之类的,不能是abcd和中文之类的,数据类型为int 1.强制字符转换 a="123" b=int(a) ...
- 性能测试WAS内存使用的探索和分析
性能测试中,CPU和内存是关注最多的两个性能指标.以我行应用最多的系统架构(WAS+Oracle)来说,CPU使用率高的问题多发生于数据库,比如索引不当引发的表扫描.绑定变量使用不当引发的硬解析.连接 ...
- 两种方式,花五分钟就能构建一个 Spring Boot 应用
前言 Spring Boot 的好处自然不必多说,对于想要从事 Java 工作的朋友们来说,可谓是必学的技能. 在我看来,它的优势就是多快好省. 功能多,很多常用的能力都有集成: 接入快,简单的几行代 ...
- JavaScript常用API
JavaScript常用API 节点属性 文档节点 事件监听.一出事件 获取元素方法
- 逃逸分析与栈、堆分配分析 escape_analysis
小结: 1.当形参为 interface 类型时,在编译阶段编译器无法确定其具体的类型.因此会产生逃逸,最终分配到堆上. 2.The construction of a value doesn't d ...
- 后台故障&性能分析常用工具
说明 本文是一个归纳总结,把常用的一些指令,及它们常用的option简单记录了一下,目的是当我们需要工具去定位问题的时候,能够从中找到合适的工具,具体的用法网上有很多博文了,当然还有man手册.参考了 ...