首页 > 其他 > 详细

ZOJ 3872 Beauty of Array(数学啊)

时间:2015-04-28 22:49:25      阅读:455      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520


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

Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest


题意:

给出一个集合,求每个子集合非重复元素的总和。

PS:

dp += (i-w[x])*x;

dp 表示当前输入的x前(包含x)的子序列的和;

每次新加一个x,那么他和上一次出现的同一个x之间的就是新增的,

w[x] = i; 记录的是前面一次x最后出现的位置;

模拟一下就知道了:

输入   1     2     3

dp      1     5     14

sum   1     6      20

w[i]     1     2      3


代码如下:

#include <cstdio>
#include <cstring>
int main()
{
    int t;
    int n;
    int w[100017];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int x;
        memset(w,0,sizeof(w));
        long long sum = 0, dp = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&x);
            dp += (i-w[x])*x;
            sum+=dp;
            w[x] = i;
        }
        printf("%lld\n",sum);
    }
    return  0;
}

ZOJ 3872 Beauty of Array(数学啊)

原文:http://blog.csdn.net/u012860063/article/details/45343683

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!