zoukankan      html  css  js  c++  java
  • ZOJ 3872 Beauty of Array DP 15年浙江省赛D题

    也是一道比赛时候没有写出来的题目,队友想到了解法不过最后匆匆忙忙没有 A 掉

    What a pity...

    题意:定义Beauty数是一个序列里所有不相同的数的和,求一个序列所有字序列的Beauty和

      1 <= N <= 100000

    解题思路:由于数据比较大,常规方法求字序列和肯定是行不通的,我们不妨这样想:因为要区别于不同的数

    ,可以看成序列里的数是一个一个加进去的,每次加入一个数,统计前面序列里第一次出现新加入的这个数的位置,表达的不好,

    举个例子:

    1 2 3

    定义dp(当前元素前面(包括自己)所有包含自己的字序列的和)

    定义sum(当前元素前面所有字序列的和,包括此元素)

    //输入   1     2     3

    //dp      1     5     14

    //sum   1     6      20

    //a[i]      1     2      3

    理解了思路,代码很容易实现,也是比较短,精髓都在for循环里,因为只用了一个for循环,每次新加入一个元素,就可以求出当前所有字序列的Beauty和,所以复杂度为O(n),弱弱把代码贴上,对于没有过掉的可以看一下思路,秒过掉的大神们可以直接无视~~

    Source Code:

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <string>
    #include <map>
    #include <set>
    #include <list>
    #include <queue>
    #include <vector>
    #include <algorithm>
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    #define MOD 1000000007
    #define pi acos(-1.0)
    
    using namespace std;
    
    typedef long long           ll      ;
    typedef unsigned long long  ull     ;
    typedef unsigned int        uint    ;
    typedef unsigned char       uchar   ;
    
    template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
    template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}
    
    const double eps = 1e-7      ;
    const int N = 210            ;
    const int M = 1100011*2      ;
    const ll P = 10000000097ll   ;
    const int MAXN = 10900000    ;
    
    ll a[100010];
    
    int main () {
       int i, j, k, n, m, x, u, v;
       cin >> n;
       while (n--) {
           cin >> m;
           memset (a, 0, sizeof(a));
           ll sum = 0, dp = 0;
           for (i = 1; i <= m; ++i) {
               cin >> x;
               dp = (i - a[x]) * x + dp;
               sum += dp;
               a[x] = i;
           }
           cout << sum << endl;
       }
       return 0;
    }
  • 相关阅读:
    PHP保留小数的相关方法
    ASP.NET Core MVC 之过滤器(Filter)
    ASP.NET Core MVC 之控制器(Controller)
    ASP.NET Core MVC 之视图组件(View Component)
    ASP.NET Core MVC 之局部视图(Partial Views)
    标签助手(TagHelper)
    ASP.NET Core MVC 之布局(Layout)
    ASP.NET Core MVC 之视图(Views)
    ASP.NET Core MVC 之模型(Model)
    九卷读书:淘宝从小到大的发展 -重读《淘宝技术这十年》
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4485103.html
Copyright © 2011-2022 走看看