• zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array


    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496

    The 12th Zhejiang Provincial Collegiate Programming Contest - D
    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 all contiguous subarray of the array A.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (1 <= N <= 100000), which indicates the size of the array. The next line contains N positive integers separated by spaces. Every integer is no larger than 1000000.

    Output

    For each case, print the answer in one line.

    Sample Input

    3
    5
    1 2 3 4 5
    3
    2 3 3
    4
    2 3 3 2
    

    Sample Output

    105
    21
    38

    分析:

    水题、

     AC代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <stack>
     5 #include <queue>
     6 #include <map>
     7 #include <set>
     8 #include <vector>
     9 #include <math.h>
    10 #include <algorithm>
    11 using namespace std;
    12 #define ls 2*i
    13 #define rs 2*i+1
    14 #define up(i,x,y) for(i=x;i<=y;i++)
    15 #define down(i,x,y) for(i=x;i>=y;i--)
    16 #define mem(a,x) memset(a,x,sizeof(a))
    17 #define w(a) while(a)
    18 #define LL long long
    19 const double pi = acos(-1.0);
    20 #define Len 1000005
    21 #define mod 786433
    22 #define exp 1e-5
    23 const int INF = 0x3f3f3f3f;
    24 
    25 LL dp[Len];
    26 int hsh[Len];
    27 
    28 int main()
    29 {
    30     int t,n,a;
    31     scanf("%d",&t);
    32     w(t--)
    33     {
    34         memset(hsh,0,sizeof(hsh));
    35         scanf("%d",&n);
    36         dp[0]=0;
    37         for(int i=1;i<=n;i++)
    38         {
    39             scanf("%d",&a);
    40             dp[i]=dp[i-1]+a+(i-1-hsh[a])*a;
    41             hsh[a]=i;
    42         }
    43         LL ans=0;
    44         for(int i=1;i<=n;i++)
    45             ans+=dp[i];
    46         printf("%lld
    ",ans);
    47     }
    48 }
    View Code
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <queue>
     7 #include <stack>
     8 #include <vector>
     9 #include <map>
    10 #include <set>
    11 #include <deque>
    12 #include <cctype>
    13 #define LL long long
    14 #define INF 0x7fffffff
    15 using namespace std;
    16 long long int dp[100005];
    17 int a[100005];
    18 int main() {
    19     int t;
    20     scanf("%d",&t);
    21     while(t--){
    22         int n;
    23                 
    24         scanf("%d",&n);
    25         map<int,int> num; //num记录前一个a[i]的位置 
    26         
    27         for(int i = 0;i < n;i++){
    28             scanf("%d",&a[i]);
    29             num[a[i]] = 0;     
    30         }
    31         dp[0] = 0;
    32         dp[1] = a[0];
    33         num[a[0]] = 1;
    34         
    35         for(int i = 1;i < n;i++){
    36             /*
    37                 dp[i] 为前i个美丽的数组的和
    38                 前i个和包括前i-1个的和 dp[i-1] 还有由第i个数组成的和 dp[i] - dp[i-1] + a[i] * (i+1 - num[a[i]]);
    39                 其中的i+1 - num[a[i]] 是用于去除重复的a[i],重复的只计算一次 
    40             */ 
    41             dp[i+1] = dp[i] + dp[i] - dp[i-1] + a[i] * (i+1 - num[a[i]]);
    42             num[a[i]] = i+1; //将新的位置记录下来 
    43         }
    44         cout << dp[n] << endl;
    45     }
    46     return 0;
    47 }
    View Code

    另外附上本次比赛的终榜:(1-104)

  • 相关阅读:
    postgresql创建表
    PG查询数据库大小
    unicode字符集范围
    删除mysql数据中的空格和换行符
    无法生成模型:“System.Data.StrongTypingException: 表“TableDetails”中列“IsPrimaryKey”的值为 DBNull
    ubuntu 16.04 64位 coreseek
    linux wps 缺失字体问题解决
    误删除/var/lib/dpkg解决办法
    Microsoft Office CVE-2017-8570
    mariadb ==> 开机自启动
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4456587.html
Copyright © 2020-2023  润新知