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 ...
随机推荐
- php 的设计模式
1.单例模式 单例模式顾名思义,就是只有一个实例.作为对象的创建模式, 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式的要点有三个: 一是某个类只能有一个实例: ...
- DirectX 初始化DirectX(手写和红龙书里面的方式)
上次介绍了如何初始化Direct3D,这次手写一次初始化代码,都是一样的方式不过看起来整洁一点. 创建一个Win32空项目添加一个空类增加以下代码即可. #include "CreateDe ...
- qt model/view 架构自定义模型之QFileSystemModel
# -*- coding: utf-8 -*- # python:2.x #QFileSystemModel """ Qt 内置了两种模型:QStandardItemM ...
- UILabel+Create
#import <UIKit/UIKit.h> @interface UILabel (Create) /** * 创建普通Label * * @param frame frame * @ ...
- FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
C Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...
- VBA清除Excelpassword保护,2003/2007/2010均适用
Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...
- [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data
Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...
- [Immutable + AngularJS] Use Immutable .List() for Angular array
const stores = Immutable.List([ { name: 'Store42', position: { latitude: 61.45, longitude: 23.11, }, ...
- iOS经常使用的加密算法
在iOS开发中,为了数据的安全经常对内容进行加密,在这儿我们对经常使用的加密算法进行了总结: 1.MD5 <span style="font-size:18px;">+ ...
- libxml两种换行方法
好久没上来留下一些记录了,可能是太忙,又或者是过于慵懒便疏于整理. libxml是一个开源的库,linux下解析xml文件经常用到,进行一些创读增删的操作. 最开始接触的时候,看到了一个简明易懂的&l ...