ZOJ 3872 Beauty of Array

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 indicating 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.

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

这种考智商的题可能不太适合我,,,,,光看懂别人的代码就用了好长时间的说;;;
思路是:
序列末尾每多一个数就和前面的子序列比较, 如果前面的子序列不含有这个数, 那么就把这个数贴到子序列的后面, 并求和;
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 100005

int main()
{
    int n, i, j, T;
    long long dp, sum;
    int a[N];
    scanf("%d", &T);
    while(T--)
    {
        memset (a, 0, sizeof (a));
        scanf ("%d", &n);
        int x;
        dp = 0;
        sum = 0;
        for(i=1; i<=n; i++)
        {
            scanf ("%d", &x);
            dp = (i - a[x]) * x + dp;
            sum += dp;
            a[x] = i;
        }
        printf ("%lld\n", sum);
    }
    return 0;
}

 

posted on 2017-05-02 18:41  小春天  阅读(125)  评论(0编辑  收藏  举报

导航