当前位置:网站首页>Beauty Values

Beauty Values

2022-08-09 10:50:00 天T.

题目链接:题目地址
题目描述:
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;
}

原网站

版权声明
本文为[天T.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44859233/article/details/109275361