D - Beauty of Array ZOJ - 3872

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);
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值