您的位置:首页 > 产品设计 > UI/UE

2019牛客暑期多校训练营(第八场) B Beauty Values

2019-08-10 21:04 1481 查看

题目描述
Gromah and LZR have entered the second level. There is a sequence a_1, a_2, \cdots, a_na1​,a2​,⋯,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_{}n​, denoting the length of the sequence.

The second line contains n_{}n​ positive integers a_1, a_2, \cdots, a_na1​,a2​,⋯,an​, denoting the sequence.

1 \le a_i \le n \le 10^51≤ai​≤n≤105

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

题目大意:
给出一个序列。让求出所有的子序列中含有不同数字的个数,然后求和

分析:
当处理第一个1的时候,此时i=1,那么这个1会对区间[1,1],[1,2],[1,3],[1,4]有影响,那么第一轮的答案就加4
当i=2时,需要判断2这个数前一次出现的位置,对样例来说,是没有出现过的,那么2对[2,2],[2,3],[2,4],[1,2],[1,3],[1,4]这些区间有贡献值的
当i=3时,这个1就不会对前一个1所贡献的区间有影响了,那么当前这个1所贡献的区间是[3,3],[3,4],[2,3],[2,4]
当i=4时,贡献区间时[4,4],[3,4],[2,4],[1,4]

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
int a[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n;
scanf("%d",&n);
ll ans=0;
rep(i,1,n)
{int val;
scanf("%d",&val);
//求原序列有多少个子区间包含至少一个该数字,相当于求该数字对多少个区间有贡献
ans+=1ll*(i-a[val])*(n-i+1);//本身的与前一次出现的区间所构成的贡献值
cout<<ans<<endl;
a[val]=i;
}
printf("%lld",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: