ZOJ 3872 2015省赛D 技巧题

D - Beauty of Array
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  ZOJ 3872

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


。。。以我现在的水平还是想不到的


题目大意:

给一个集合,问集合中所有子集的和(重复元素就当做一个)。


表示我看了半天还是不会做,看了一下别人的,瞬间发现,好吧,原来我只是把子集给横着看了,如果斜着看那就对了

思路:

首先想到的就是从第一个开始,不断地枚举
例如2 3 3 2
2 23 233 2332
3 33 332
3 32
2
那么去掉重复的元素以后就是
2 23 23 23
3 3  32
3 32
2
根据规律可以找出,每一次往其中添加一个数(斜着看),
都相当于加上这个数现在的位置减去这个数之前出现的位置
然后把这些列(斜着)都加在一起就好了



#include<bits stdc="" h=""> using namespace std; const int maxn = 100000 + 5; int n; long long a[maxn]; int place[maxn]; void init(){ memset(place, 0, sizeof(place)); memset(a, 0, sizeof(a)); scanf("%d", &n); for (int i = 1; i <= n; i++){ scanf("%lld", a + i); } } void solve(){ long long res = 0, dp = 0; for (int i = 1; i <= n; i++){ dp += (i - place[a[i]]) * a[i]; place[a[i]] = i; res += dp; } printf("%lld\n", res); } int main(){ int t; scanf("%d", &t); while (t--){ init(); solve(); } return 0; } </bits>

你可能感兴趣的:(ZOJ 3872 2015省赛D 技巧题)