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 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4457608.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值