ZOJ 3872 Beauty of Array

时间:2022-06-10 18:39:08

ZOJ 3872 Beauty of Array

ZOJ 3872 Beauty of Array

 /**
Author: Oliver
ProblemId: ZOJ 3872 Beauty of Array
*/
/*
需求:
求beauty sum,所谓的beauty要求如下:
1·给你一个集合,然后把所有子集合的美丽和求出来;
2·上例子,2.3.3.->2. 3. 3. 2.3. 3. 2.3.
思路:
1·既然要连续的,暴力也会爆,那么自然而然的优化,朝着递推想一下;
2·哥们把之前的算好了,那我们是不是可以用算好的值来求现在的的需要呢;
3·想好了,但是过程我不造该怎么说;
步骤: 。。。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXM = +;
const int MAXN = +;
int F[MAXN],a[MAXM];
int main()
{
int n,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(F,,sizeof F);
long long ans=,sum=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(F[a[i]])sum-=F[a[i]]*a[i];
ans+=sum+=i*a[i]; F[a[i]]=i;
}
printf("%lld\n",ans);
}
}