zoj 3872

D - Beauty of Array

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status Practice ZOJ 3872

Appoint description: 
System Crawler  (2015-04-30)

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题目描述: 求一段序列中连续子序列的和,如果这段连续子序列中有重复的话,(the summation of all distinct integers in the arra),这个序列中不相同的数的和,也就是说只计算一次。如果要求的是一段序列中连续子序列的个数,那么如果定义d[i]为以i结尾的连续子序列的个数,d[i]=d[i-1]+1;我们定义d[i]为以i结尾的连续子序列的和,那么如果不重复d[i]=d[i-1]+a*i;,如果重复的话,假设1 2 3  4 5 6 7。。。。。i,如果在第j位,那么(i i-1),(i,i-2),(i,i-3)。。。。(i,j+1)这些连续子序列的值可以加上a的值;(i,j),(i,j-1),(i,j-2),(i,1),这些值都会包含重复的i,j位置上的值,因为只需要算一次,所以不需要给这些以i结尾的子序列加上a,这些子序列的个数,总共有j个,所以我们只需要用一个数组A标记上A[a]=i;那么d[i]=d[i-1]+a+(i-1-A[a])*a;如果a之前没有出现过,那么A[a]等于0;如果a之前出现过,减去包含重复值的子序列的个数,也就是A[a]。
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <set>
#include <algorithm>
#define LL long long
#define  M 100100
using namespace std;
LL dp[M];
LL visit[M];
void init()
{
    memset(dp,0,sizeof(dp));
    memset(visit,0,sizeof(visit));
}
void solve()
{
    int n;
    LL data;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&data);
        dp[i]=dp[i-1] + data +(i-1-visit[data]) * data;
        visit[data]=i;
    }
    LL  answer=0;
    for(int i=1;i<=n;i++)
        answer+=dp[i];
    printf("%lld\n",answer);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
     {
       init();
       solve();
     }
    // system("pause");
    return 0;
}

  

时间: 2024-10-11 19:07:15

zoj 3872的相关文章

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>

ZOJ 3872 Beauty of Array&amp;&amp;ZOJ 3870 Team Formation

3872链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3872 题目大意:给你n个数,问所有的连续的子序列中的所有元素的和(子序列中有相同元素只计算一次)(n<100000). 即若序列为1  2  3,则组成1,2,3,1 2,2 3,1 2 3,和为20: 若序列为1 2 2,则组成1,2,2,1 2,2 2,1 2 2,和为13: 解题思路:求不重复的序列和很简单,关键是去重. 现在看一个序列: 3  4 

ZOJ - 3872 Beauty of Array

题意:给定一个含有N个数的序列S,定义序列的魅力值为序列中不同数字之和,求出该序列所有子序列的魅力值之和. 分析:每个数乘以它出现的次数,求和即可. 如何求每个数出现的次数? 1.对于一个各数字完全不同的序列, eg:3 5 2 6 8 对于5来说,确定其存在于的子序列 (1)其右面,可选0个数字---5  可选1个数字---3 5 (2)其右面,可选0个数字---5  可选1个数字---5 2  可选2个数字---5 2 6        可选3个数字---5 2 6 8 因此,2 * 4 =

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

ZOJ 3872 Beauty of Array(数学啊)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 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

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

ZOJ 3872 Beauty of Array(数学)

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

ZOJ 3872 (思维)

题意: 给你一个串,  串和为所有不相同数字之和,  然后让你求该串所有连续子串总和为多少. 题解: 从前往后扫一次   ,  记录当前数字出现次数,  计算和的时候减去该数字乘出现次数 代码: #include<stdio.h> #include<iostream> #include<map> using namespace std; map<int, long long int> mark; int main() { int T, a, n; scanf

ZOJ3872 Beauty of Array---规律 | DP| 数学能力

传送门ZOJ 3872 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 t