Beauty Values

题目链接:题目地址
题目描述:
Gromah and LZR have entered the second level. There is a sequence a1,a2,a3…an on the wall.

There is also a note board saying “the beauty value of a sequence is the number of different elements in the sequence”.

LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.

Please help them determine the password!

输入描述:
The first line contains one positive integer n , denoting the length of the sequence.

The second line contains n positive integers a1,a2,a3…an denoting the sequence.

1<=ai<=n<=100000

输出描述:
Print a non-negative integer in a single line, denoting the answer.

输入
4
1 2 1 3
输出
18

说明
The beauty values of subintervals [1,1], [2,2], [3,3], [4,4] are all 1

.
The beauty values of subintervals [1,2], [1,3], [2,3], [3,4] are all 2

.
The beauty values of subintervals [1,4], [2,4] are all 3

.
As a result, the sum of all beauty values are 1×4+2×4+3×2=18.

中文翻译:
Gromah和LZR进入了第二层。有一个序列a1 a2 a3…an一个在墙上。

还有一个留言板上写着“一个序列的美值是序列中不同元素的数量”。

LZR很快就会给出这个级别的密码,它是墙上的序列的所有连续子区间的美丽值的总和。

请帮助他们确定密码!

#include<iostream>
using namespace std;
typedef long long ll;
long long ans;
int n,a[100005],book[100005];
/*题目中的例子:1213
[1,2][2,2][3,3][4,4]--->1---是指1到1的区间,不同的数只有1;2到2区间,不同的数只有2....
[1,2][1,3][2,3][3,4]---->2---1到2下标区间,不同的数有1,2,有2个;1到3下标区间,不同的数有1,2,有2个;2到3下标区间,不同的数有2,1,有2个;3到4下标区间,不同的数有1,3,有2个 
[1,4][2,4]--->3---1到4下标区间,不同的数有1,2,3,有3个;2到4下标区间,不同的数有2,1,3,有3个
*/
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) 
		cin>>a[i];
	for(int i=1;i<=n;i++){
		if(book[a[i]]==0){
			ans+=(ll)(n-i+1)*i;
		}else{
			ans+=(ll)(n-i+1)*(i-book[a[i]]);
		}
		book[a[i]]=i;//有多少重复的数 
	}
	cout<<ans<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值