zoukankan      html  css  js  c++  java
  • 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
    ", sum);
    43     }
    44 
    45 
    46     return 0;
    47 }
    编译人生,运行世界!
  • 相关阅读:
    模拟22
    模拟21
    模拟20
    模拟19
    晚测11
    Redis 内存模型
    Redis AOF重写
    基础省选题选做
    八年级上 期中考试祭
    P2049 魔术棋子 题解
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4457608.html
Copyright © 2011-2022 走看看