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 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.
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 题意就是给你一个数组,让你求1~1,1~2,1~3,1~4.... 1~n,
2~2,2~3, 2~4.... 2~n
....................
n-1~n-1 n-1~n
n~n
这些区间内不同数字的和,然后求总和。 思路:就是从右往左遍历,找某个数右边第一次出现这个数的位置,如果是第一次出现那么这个位置就是n
然后加加乘乘就行了 代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e6+;
const int NN=1e5+;
int pos[N],a[NN];
int main()
{
int t;
int n;
scanf("%d",&t);
long long ans,tans;
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=-;//标记每个数都没出现过
}
ans=,tans=;
for(int i=n-;i>=;i--)
{
if(pos[a[i]]==-)
tans+=a[i]*(n-i);
else
tans+=a[i]*(pos[a[i]]-i);
pos[a[i]]=i;
ans+=tans;
}
printf("%lld\n",ans);
}
return ;
}
Beauty of Array ZOJ - 3872(思维题)的更多相关文章
- 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 3872 Beauty of Array
/** Author: Oliver ProblemId: ZOJ 3872 Beauty of Array */ /* 需求: 求beauty sum,所谓的beauty要求如下: 1·给你一个集合 ...
- 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 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(思维)
Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integ ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- ZOJ 3872 浙江2015年省赛试题
D - Beauty of Array Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu S ...
随机推荐
- CVPR2015一些文章整理
简单看了一部分CVPR2015的文章.整理了一下. 当中我决定把精彩的文章加粗. 主要是认为有些文章仅仅读了一遍,没有发现非常多非常有道理的point(虽然我承认他们的工作都花了非常大的功夫.可是没有 ...
- 利用rman自己主动备份转储spfile
利用rman自己主动备份转储spfile [情景简单介绍] 生产环境丢失了server的參数文件,rman已开启自己主动备份设置. [操作过程简述] ----启动rman $rman target / ...
- HDFS03
=====================HDFS数据块(block)===================== 文件被切分成固定大小的数据块 -------------> √默认数据块大小为6 ...
- 布局技巧1:创建可重用的UI组件(include标签)
Android平台提供了大量的UI构件,你可以将这些小的视觉块(构件)搭建在一起,呈现给用户复杂且有用的画面.然而,应用程序有时需要一些高级的视觉组件.为了满足这一需求,并且能高效的实现,你可以把多个 ...
- uboot流程分析--修改android启动模式按键【转】
本文转载自:http://blog.csdn.net/dkleikesa/article/details/9792747 本人用的android平台用的bootloader用的是uboot,貌似大多数 ...
- MyEclipse个性设置
MyEclipse个性设置 (1)Myeclipse 打开 jsp 的默认编辑器不好,会同时打开预览.所以做如下更改 Windows–>Perferences–>General–>E ...
- 编译android4.4 报错error: call to '__property_get_too_small_error' declared with attribute 的处理 (转载)
转自:http://blog.csdn.net/syhost/article/details/14448899 完整的报错为: system/core/include/cutils/propertie ...
- E20170624-ts
stateless adj. 无国家的,无国籍的; groupware 群件 cookie n. 饼干; 小甜点; 吸引人的年轻妇女; 甜面包; session n. 开会,会议; (法庭的) 开 ...
- JS连续滚动幻灯片:原理与实现
什么是连续滚动幻灯片?打开一些网站的首页,你会发现有一块这样的区域:一张图片,隔一段时间滑动切换下一张:同时,图片两端各有一个小按钮,供你手动点选下一张:底部有一排小圆圈,供你选定特定的某帧图片.这就 ...
- String,StringBuffer和StringBuilder
在执行速度方面的比较:StringBuilder > StringBuffer StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们对字符串做操 ...