zoukankan      html  css  js  c++  java
  • ZOJ 3872 Beauty of Array 连续子序列求和

    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.

    <h4< dd="">Output

    For each case, print the answer in one line.

    <h4< dd="">Sample Input

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

    <h4< dd="">Sample Output

    105
    21
    38

    题目的意思是求一个个连续子序列的和

    以 2 3 3 2 为例讲一下大概吧

    首先以2结尾 那就是 2
    然后以第一个3结尾 这时他们的和为: 2 + 3*2(以3结尾有两种可能:3 2,3) = 8
    然后是以第二个3结尾,这时他们的和为: 2 + 3*2 + (3-2)*3 = 11
    最后是以第二个2结尾,这时他们的和为: 2 + 3*2 + (3-2)*3 + (4-1)*2 = 17 乘以的3,4是因为他们排三四位所以连续序列的可能为3,4种
    将所有的和相加得到最后的和 38

    注意处理时要有技巧,别傻傻的全部相加,注意到前面的就是上面的和,你只需要加每个序列的最后一位的可能和就行

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #define ls (u<<1)
    #define rs (u<<1|1)
    #define maxn 100010
    #define ll long long
    #define INF 1e9
    using namespace std;
    #define max(a,b) (a)>(b)?(a):(b)
    #define min(a,b) (a)<(b)?(a):(b)
    int a[maxn];
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            int n;
            memset(a,0,sizeof(a));
            scanf("%d",&n);
            int t;
            ll num=0,sum=0;
            for(int i=1;i<=n;i++){
                scanf("%d",&t);
                num = (i-a[t])*t+num;
                sum += num;
                a[t]=i;
            }
            printf("%lld
    ",sum);
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    【转】浅谈一个网页打开的全过程(涉及DNS、CDN、Nginx负载均衡等)
    【转】1.2 CDN的基本工作过程
    【转】 最新版chrome谷歌浏览器Ajax跨域调试问题
    【转】网段,子网掩码,网络标识,IP划分
    【转】默认网关有什么用?我应当怎么填写默认网关和DNS呢
    【转】DHCP工作过程详解
    【转】WINS服务器与DNS服务器有什么区别?
    46. Permutations 排列数
    30. Substring with Concatenation of All Words
    29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/l609929321/p/7295896.html
Copyright © 2011-2022 走看看