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>
11 #include <iostream>
12 #include <cstring>
13 #include <string>
14 #include <map>
15 #include <set>
16 using namespace std;
17
18 const int MAXN = 1e5 + 10;
19 const int INF = 0x3f3f3f3f;
20 int a[MAXN];
21
22 int main(void)        //ZOJ 3872 Beauty of Array
23 {
24     //freopen ("D.in", "r", stdin);
25
26     int t, n;
27     scanf ("%d", &t);
28     while (t--)
29     {
30         memset (a, 0, sizeof (a));
31         scanf ("%d", &n);
32
33         int x;    long long dp = 0, sum = 0;
34         for (int i=1; i<=n; ++i)
35         {
36             scanf ("%d", &x);
37             dp = (i - a[x]) * x + dp;
38             sum += dp;
39             a[x] = i;
40         }
41
42         printf ("%lld\n", sum);
43     }
44
45
46     return 0;
47 }
时间: 2024-12-09 22:52:48

DP ZOJ 3872 Beauty of Array的相关文章

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(数学啊)

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

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