ZOJ - 3872
Beauty of Array
Time Limit: 2000MS | | Memory Limit: 65536KB | | 64bit IO Format: %lld & %llu |
SubmitStatus
Description
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
The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N
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
Hint
Source
The 12th Zhejiang Provincial Collegiate Programming Contest
//题意:一个序列的漂亮值的定义为:这个序列中所有不重复的数的和。
给你一个含有n个数的序列,让你找出这个序列的所有连续子序列的漂亮值的和。
//思路:
是一道挺好的题,比赛是队友花了好长时间终于把它A了。
因为n的值很大,所以不可能用两重for循环(肯定会TML),所以就想着用一重for实现。
用sum[i]数组存放前i个数的所有可能的组合的情况的和,用pre[x]数组标记x最后一次出现的位置,通过它来计算x会被累加几次,有了这个思路就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
long long sum[100000+100],pre[100000+100];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(sum,0,sizeof(sum));
memset(pre,0,sizeof(pre));
long long ans=0;
for(long long i=1;i<=n;i++)
{
long long x;
scanf("%lld",&x);
sum[i]=sum[i-1]+(i-pre[x])*x;
pre[x]=i;
ans+=sum[i];
}
printf("%lld\n",ans);
}
return 0;
}