Beauty of Array(思维)
Beauty of Array
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 Npositive 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
题解:找集合A中连续的连续子集内的不同元素的和;思维转化成每个元素在当前连续子集中需要加的个数就好了;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
const int MAXN = ;
int num[MAXN];
int pre[MAXN];
typedef long long LL;
LL sum[MAXN];
int main()
{
int T, N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
memset(pre, , sizeof(pre));
memset(sum, , sizeof(sum));
int x;
LL ans = ;
for(int i = ; i <= N; i++){
scanf("%d", &x);
sum[i] = sum[i - ] + (i - pre[x]) * x;//i - pre[i]代表x用到了多少次;
pre[x] = i;
ans += sum[i];
}
printf("%lld\n", ans);
}
return ;
}
Beauty of Array(思维)的更多相关文章
- 2015 浙江省赛 Beauty of Array (思维题)
Beauty of Array Edward has an array A with N integers. He defines the beauty of an array as the summ ...
- ZOJ 3872: Beauty of Array(思维)
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- 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 ...
- 第十二届浙江省大学生程序设计大赛-Beauty of Array 分类: 比赛 2015-06-26 14:27 12人阅读 评论(0) 收藏
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ...
- ZOJ 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
- 浙江省第十二届省赛 Beauty of Array(思维题)
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
- Beauty of Array ZOJ - 3872(思维题)
Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...
- Beauty of Array
Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...
随机推荐
- (转)25个增强iOS应用程序性能的提示和技巧--初级篇
在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...
- java第二周学习日记
day01 1.抽象类 (1)抽象类的应用场景: 我们在描述一类事物的时候,发现该种事物确实存在着某种行为,但是这种行为目前不是具体的,那么我们可以抽取这种行为的声明,但是不去实现该种行为,这时候这种 ...
- android——仿微拍贷滑动圆形菜单
一次偶然机会接触到微拍贷的app,瞬间被其圆形可滑动菜单吸引了.一直琢磨着给弄出来. 现在弄出来了.先看看效果吧 如果你也喜欢这个菜单.去我的github找源码吧.太忙了.没时间贴代码和讲解了. ht ...
- jquery mouseout和mouseleave区别
mouseout()和mouseleave() 都是鼠标移出元素时触发,但是这两者又有所区别,需要大家留意: 不论鼠标指针离开指定元素还是该元素子元素,都会触发 mouseout 事件. 只有在鼠标指 ...
- 导出word文档
string id = Request["id"]; if (string.IsNullOrEmpty(id)) { ...
- doGet和doPost的区别
1.doGet和doPost的区别,在什么时候调用,为什么有时doPost中套用doGet 2.提交的form method=Post就执行DOPOST,否则执行GOGET 套用是不管meth ...
- Gengxin讲STL系列目录
引言:有人催我写关于STL的博客#(滑稽) STL嘛,昨晚有人一直逼问我STL名字的由来——STL = Standard Template Library,标准模板库,惠普实验室开发的一 ...
- QT TCP/IP
QT 网络通信(TCP/IP) 服务端: 一.监听新的客户端接入(QTcpServer) 重写函数 incomingConnection(qintptr socketDescriptor) 二.服务端 ...
- [ofbiz]解决load-demo花费过长时间的问题
一直以来使用公司配置的hp-cq45笔记本,在初始化ofbiz的时候load-demo需要花费很长一段时间,比如90分钟,或者129分钟. 解决办法:安装官方驱动--intel快速存储驱动 ok,lo ...
- java的LinkedList的用法
http://blog.chinabyte.com/blog.php?do-showone-uid-135325-itemid-454704-type-blog.html 总结下,LinkedList ...