ZOJ 3872: 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 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
题意
给定一串数字,求所有连续的子序列的和,如果一个数字出现多次,则只计算一次
Solve
去统计每个数字被加的次数即可,注意数据范围
对于一个连续的序列来说,如果有重复的数字,那么后面的数字就可以忽视掉。所以对于一个数字xxx,只需要去计算以这个xxx为结尾和开头的序列,两者相乘即为该数字被加的次数
Code
/*************************************************************************
> Author: WZY
> School: HPU
> Created Time: 2019-04-09 17:07:29
************************************************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=(1<<30);
const ll INF=(1LL*1<<60);
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int vis[maxn];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
int n,x;
ll ans;
while(t--)
{
ms(vis,0);
ans=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;
ans+=1LL*x*((i-vis[x])*(n-i+1));
vis[x]=i;
}
cout<<ans<<endl;
}
return 0;
}
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 The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...
随机推荐
- javaWeb - 1 — servlet — 更新完毕
1.先来聊一些javaWeb相关的知识 简单了解一下:web的发展史 1).web就是网页的意思嘛 2).web的分类 (1).静态web 使用HTML.CSS技术,主要包括图片和文本 优点:简单,只 ...
- 分布式服务治理框架Dubbo的前世今生及应用实战
Dubbo的出现背景 Dubbo从开源到现在,已经出现了接近10年时间,在国内各大企业被广泛应用. 它到底有什么魔力值得大家去追捧呢?本篇文章给大家做一个详细的说明. 大规模服务化对于服务治理的要求 ...
- 巩固javaweb第十六天
巩固内容: 下拉框 在注册功能中,地区的选择使用了下拉框,可以从地区选项中选择一个地区.在这个 例子中,只允许选择一个,而在有些情况下,下拉框可以进行多选.所以,从功能上来说, 下拉框具有单选按钮和复 ...
- Hive(十一)【压缩、存储】
目录 一.Hadoop的压缩配置 1.MR支持的压缩编码 2.压缩参数配置 3.开启Mapper输出阶段压缩 4.开启Reduceer输出阶段 二.文件存储 1.列式存储和行式存储 2.TextFil ...
- HTML样式 背景
当浏览器读到一个样式表,就会按照这个格式表来对文档进行格式化.有以下三种方式来插入样式表: 1.外部样式表 当样式需要用到很多页面的时候,外部样式是理想的选择.使用外部样式表,就可以听过更改一个文件来 ...
- 【编程思想】【设计模式】【行为模式Behavioral】备忘录模式Memento
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/memento.py #!/usr/bin/env pyt ...
- 【C/C++】从矩阵左上角走到右下角
tx的笔试,但是只过了10%,就离谱 #include <bits/stdc++.h> using namespace std; const int maxn = 1010; long d ...
- Mysql从头部署多个版本
目录 一.环境准备 二.下载安装包 三.Mysql-5.6单独部署 四.Mysql-5.7单独部署 五.添加到多版本控制 六.muliti使用 一.环境准备 系统:centos7.3一台 软件版本:m ...
- pwnable_start
第一次接触这种类型的题,例行检查一下 题目是32位 没有开启nx保护可以通过shellocode来获得shell 将题目让如ida中 由于第一次碰到这种题,所以我会介绍的详细一点, 可以看到程序中调用 ...
- MM函数(Excel函数集团)
此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...