ZOJ 3872 Beauty of Array(数学啊)

题目链接: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;

}

时间: 2025-01-16 17:31:08

ZOJ 3872 Beauty of Array(数学啊)的相关文章

DP ZOJ 3872 Beauty of Array

题目传送门 1 /* 2 DP:dp 表示当前输入的x前的包含x的子序列的和, 3 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: 4 sum 表示当前输入x前的所有和: 5 a[x] 表示id: 6 详细解释:http://blog.csdn.net/u013050857/article/details/45285515 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cmath>

ZOJ 3872 Beauty of Array(数学)

Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

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

ZOJ 3872 Beauty of Array&amp;&amp;ZOJ 3870 Team Formation

3872链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 题目大意:给你n个数,问所有的连续的子序列中的所有元素的和(子序列中有相同元素只计算一次)(n<100000). 即若序列为1  2  3,则组成1,2,3,1 2,2 3,1 2 3,和为20: 若序列为1 2 2,则组成1,2,2,1 2,2 2,1 2 2,和为13: 解题思路:求不重复的序列和很简单,关键是去重. 现在看一个序列: 3  4 

ZOJ - 3872 Beauty of Array

题意:给定一个含有N个数的序列S,定义序列的魅力值为序列中不同数字之和,求出该序列所有子序列的魅力值之和. 分析:每个数乘以它出现的次数,求和即可. 如何求每个数出现的次数? 1.对于一个各数字完全不同的序列, eg:3 5 2 6 8 对于5来说,确定其存在于的子序列 (1)其右面,可选0个数字---5  可选1个数字---3 5 (2)其右面,可选0个数字---5  可选1个数字---5 2  可选2个数字---5 2 6        可选3个数字---5 2 6 8 因此,2 * 4 =

Zoj 3842 Beauty of Array

Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the beauty的总和. 那么要求这个这个总和,刚开始最容易想到的就是这样: for( int i=1; i<=N; i++ ) { for( int j = 1; j<=i; j++ ) { ... //排除重复的数计算总和 } } 这样子的结果实际上是 Time Limit Exceeded 因此采取

zoj 3872

D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3872 Appoint description:  System Crawler  (2015-04-30) Description Edward has an array A with N integers. He defines the beauty

ZOJ 3872 计算对答案的贡献

                                               D - Beauty of Array 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

12th浙江省省赛 B题 Beauty of Array

Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB 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