ZOJ-3872-Beauty of Array-思维
ZOJ-3872-Beauty of Array
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872
参考:https://blog.csdn.net/u013050857/article/details/45285515
题意:
定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有子序列的Beauty和
1 <= N <= 100000
【解题思路】由于数据比较大,常规方法求子序列和肯定是行不通的,我们不妨这样想:
因为要区别不同的数,可以看成序列里的数是一个一个加进去的,每次加入一个数,统计前面序列里第一次出现新加入的这个数的序列的dp(就是找到前面最近的相同数,从这个位子后面开始计算),
注意到每次多的x,一定是在所在序列独一无二的,不然相当于没有加上,考虑完独一无二,再考虑重复的,只用加上前一项的dp即可。
举个例子:
1 2 3 1
定义dp(当前元素前面(包括自己)所有包含自己的子序列的和)
定义sum(当前元素前面所有子序列的和,包括此元素)
//输入 1 2 3 1
//dp 1 5 14 14+3
//sum 1 6 20 37
//a[i] 1 2 3
理解了思路,代码很容易实现,也是比较短,精髓都在for循环里,因为只用了一个for循环,每次新加入一个元素,就可以求出当前所有子序列的Beauty和,所以复杂度为O(n).
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
int a[maxn]; int main(){
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(a,,sizeof(a));
long long dp=,sum=;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
dp+=(i-a[x])*x;
sum+=dp;
a[x]=i;
}
printf("%lld\n",sum);
}
return ;
}
ZOJ-3872-Beauty of Array-思维的更多相关文章
- 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【无重复连续子序列的贡献和/规律/DP】
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- 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 3872 Beauty of Array DP 15年浙江省赛D题
也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉 What a pity... 题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和 ...
- ZOJ 3872 Beauty of Array (The 12th Zhejiang Provincial Collegiate Programming Contest )
对于没有题目积累和clever mind的我来说,想解这道题还是非常困难的,也根本没有想到用dp. from: http://blog.csdn.net/u013050857/article/deta ...
- Beauty of Array(思维)
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integ ...
- Zoj 3842 Beauty of Array
Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the be ...
- 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 The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...
随机推荐
- 【转载】C/C++中long long与__int64的区别
在C99标准(详情请猛击:C语言的发展及其版本)中,增加了对64位长整型数据的支持,它的类型就是 long long,占用8个字节. 由于C99标准发布较晚,一些较老的C/C++编译器不支持,新编译器 ...
- 我的ubuntu kylin中mentohust的使用历程
1首先下载mentohus 最新版下载(包括源码):http://code.google.com/p/mentohust/downloads/list 2打开终端(Ctrl+Alt+T) 输入sudo ...
- java基础学习_io流之FileInputStream
一.FileInputStream属性: /* File Descriptor - handle to the open file */private final FileDescriptor fd; ...
- 深入理解JVM-java字节码文件结构剖析(练习解读字节码)
public class MyTest2 { String str = "Welcome"; private int x = 5; public static Integer in ...
- v-text,v-html等区别
首先我们知道vue中有很多自定义指令,以v- 开头,例如:v-text,v-bind,v-model, v-if,等 在这些指令中,部分指令之间是很容易被混淆,所以今天决定自己总结一下以下几个相似指令 ...
- spring aop 解决模糊查询参数 % - /等特殊符号问题
import com.hsq.common.utils.StringUtil;import org.aspectj.lang.ProceedingJoinPoint;import org.aspect ...
- Android使用com.google.android.cameraview.CameraView进行拍照
import android.Manifest;import android.annotation.SuppressLint;import android.content.Context;import ...
- 给你的SpringBoot做埋点监控--JVM应用度量框架Micrometer
JVM应用度量框架Micrometer实战 前提 spring-actuator做度量统计收集,使用Prometheus(普罗米修斯)进行数据收集,Grafana(增强ui)进行数据展示,用于监控生成 ...
- java Timer工具类实现定时器任务
第一 schedule 方法 三个参数 按照顺序 (执行的任务方法,开始执行时间,多少时间后循环去执行) 代码可用 public class TestScheedule { public stati ...
- docker/kubernetes国内源/镜像源解决方式
最近在使用kubeadm时,被各种连接不上搞到崩溃.费了很多力气,基本都解决了.这里统一整理了国内的一些镜像源,apt源,kubeadm源等,以便查阅. 国内镜像源 Azure China提供了目前用 ...