您的位置:首页 > 其它

D - Beauty of Array ZOJ - 3872

2017-04-30 13:42 393 查看
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 Tindicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

<h4< dd="">
Output
For each case, print the answer in one line.

<h4< dd="">
Sample Input
3
5
1 2 3 4 5
3
2 3 3
4
2 3 3 2

<h4< dd="">
Sample Output
105
21
38


Hint

题意:

给你N个数让你求他的连续子序列的和若一个连续子序列中出现重复项就不算。。子序列2 3 3 的值为5

eg:

 1 2 3

dp是从前慢慢往后走的。。。别急啊。。

dp[1]=包含 1  :1

dp[2]=包含 2  : 2     | 1 2

dp[3]=包含 3  : 3    | 2 3 | 1 2 3

ans=dp[1]+dp[2]+dp[3];

dp[i]=a[i]*i+dp[i-1];///i*a[i]代表a[i]出现i次的总和(因为能和前边的i-1个数组合和自己本身一次就是i次组合了)

至于重复的呢 

2 3 2 3

我们直接看A【4】=3

dp[4]=3 | 2 3| 3 2 3(重复去掉)| (2) (3) 2 3(重复去掉)

重复的时候减去。。

dp[4]=dp[3]+a[i]*i- val[a[i]]*a[i];

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
long long dp[100003];
int v[1000003];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
memset(v,0,sizeof(v));
scanf("%d",&n);
long long ans=0;
dp[0]=0;
for(int i=1;i<=n;i++)
{
int a;
cin>>a;
if(v[a]==0)
dp[i]=dp[i-1]+a*i;
else dp[i]=dp[i-1]+a*i-a*v[a];
v[a]=i;
ans+=dp[i];
}
printf("%lld\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: