zoukankan      html  css  js  c++  java
  • ZOJ 3872 计算对答案的贡献

                                                   D - Beauty of Array

    Description

    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

    题意:给你一个数组,找出所有任意连续子序列中美数和,一段子序列美数和定义为:互异数的和
    题解:计算对答案的贡献
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    const int N=1010;
    const int MAX=151;
    const int MOD=1000000007;
    const int INF=1000000000;
    const double EPS=0.00000001;
    typedef long long ll;
    int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int num[1000100];
    int main()
    {
        int a,i,n,T;
        ll ans;
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &n);
            ans=0;
            memset(num,0,sizeof(num));
            for (i=1;i<=n;i++) {
                scanf("%d", &a);
                ans+=(ll)(i-num[a])*(n-i+1)*a;
                num[a]=i;
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
    代码
  • 相关阅读:
    ZooKeeper基本原理
    Ubuntu上部署C# 网站 步骤简单记录
    代码生成助手
    微信授权封装,欢迎使用
    c#微信开发,使用JS-SDK自定义分享功能,分享朋友圈,分享给朋友等
    ab.exe使用
    【分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
    SVN服务器搭建(一)
    MVC四大筛选器—ActionFilter&ResultedFilter
    MySQL参数化查询的IN 和 LIKE
  • 原文地址:https://www.cnblogs.com/zxhl/p/4865476.html
Copyright © 2011-2022 走看看