码迷,mamicode.com
首页 > 其他好文 > 详细

DP ZOJ 3872 Beauty of Array

时间:2015-04-26 13:39:49      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 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 }

 

DP ZOJ 3872 Beauty of Array

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4457608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!