AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)
Description
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 T indicating 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.
Output
For each case, print the answer in one line.
Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2
Sample Output
105
21
38
这题直接想到了思路1A了。
假设f(n)表示以a[n]结尾的子区间互异数的和的和。对于f(n-1),可能会有两部分组成,一部分是包含a[n]这个值的A,一部分是不包含a[n]这个值的B,假设B由p个区间加和。那么可以得到:f(n) = A + B + p*a[n] = f(n-1) + p*a[n]。所以关键是求p。
而由于f(n-1)是以a[n-1]结尾的子区间互异数的和,必然的,假设a[i]是距离a[n]最近的值为a[n]的数,则p = n-i。于是只需要记录最近出现值k的脚标即可。这里采用了map,数据范围不是非常大,也可以直接开数组。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long using namespace std; int main()
{
//freopen("test.in", "r", stdin);
int T, n, v, k;
LL ans, f;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
scanf("%d", &n);
map<int, int> s;
ans = 0;
f = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &v);
f = f + (i-s[v])*v;
s[v] = i;
ans += f;
}
printf("%lld\n", ans);
}
return 0;
}
AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)的更多相关文章
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
- ACM学习历程—ZOJ3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- ACM学习历程——HDU4472 Count(数学递推) (12年长春区域赛)
Description Prof. Tigris is the head of an archaeological team who is currently in charge of an exca ...
- AndyQsmart ACM学习历程——ZOJ3870 Team Formation(位运算)
Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...
- ACM学习历程—BestCoder Round #75
1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- ACM学习历程—HDU2068 RPG的错排(组合数学)
Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿 ...
随机推荐
- Java 9 模块解耦的设计策略
1. 概述 Java 平台模块系统 (Java Platform Module System,JPMS)提供了更强的封装.更可靠且更好的关注点分离. 但所有的这些方便的功能都需要付出代价.由于模块化的 ...
- matlab中文显示乱码:控制台上的,编辑器的,图片中的
问题:matlab脚本与函数文件的中文注释显示乱码. 环境:matlab R2016a.Windows 10 home. 解决方案: step1 检查locale值 matlab命令行键入命令 fea ...
- 一致性哈希算法(c#版)
最近在研究"一致性HASH算法"(Consistent Hashing),用于解决memcached集群中当服务器出现增减变动时对散列值的影响.后来 在JAVAEYE上的一篇文章中 ...
- Spark源码分析之一:Job提交运行总流程概述
Spark是一个基于内存的分布式计算框架,运行在其上的应用程序,按照Action被划分为一个个Job,而Job提交运行的总流程,大致分为两个阶段: 1.Stage划分与提交 (1)Job按照RDD之间 ...
- PHP正则匹配6到16位字符组合(且只能为数字、字母、下划线)
php正则匹配6到16位的字符串. 只允许包含数字.字母.下划线组成的6到16位字符,符合返回ture,否则返回false. 解答: 6到16位,正则可以这样写:{6,16}. 任意的字符6到16位的 ...
- typeof 与 instanceof 区别
typeof typeof 是一元运算符,返回值是字符串,且只能是number,string,boolean,object,function,undefined typeof用来判断一个值是否存在 i ...
- opensearch空查询
query子句不支持为空的查询,可以使用filter子句:filter=area="" 或者 filter=filedlen(area)=0 可以使用相关性函数实现:https ...
- 【BZOJ1150】[CTSC2007]数据备份Backup 双向链表+堆(模拟费用流)
[BZOJ1150][CTSC2007]数据备份Backup Description 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此 ...
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:使用BootstrapPagination以分页形式展示数据信息
上一篇介绍通过接口来获取数据,本篇将介绍如何以分页形式展示出接口获取到的数据 获取到的数据往往会很多,为了追去页面的美观和方便用户的检索,需要进行分页的展示: EasyNVR可接如多通道,当我们的通道 ...
- Regularization: how complicated the model is? Regularization, measures complexity of model 使预测准确、平稳 predictive stable
http://www.kdd.org/kdd2016/papers/files/rfp0697-chenAemb.pdf https://homes.cs.washington.edu/~tqchen ...