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 = 8,则5存在于8种子序列中。

2、若序列中出现了重复数字,那么左边直接处理到该重复数字之前出现的位置后即可。

eg:2 3 3 2

对于第二个3,在计算其存在于的子序列时,只需要考虑3(第二个3) 和 3 2 两个子序列即可。

原因:2 3 3 2的子序列有

2

3

2 3

3

3 3

2 3 3

2

3 2

3 3 2

2 3 3 2

我现在只要把这些各序列中不同的数字相加即可。

从左往右开始考虑,2存在于4个子序列中,所以将这4个子序列中的2先加起来,还剩下

2

3

2 3

3

3 3

2 3 3

2

3 2

3 3 2

2 3 3 2

同理,3(序列中第一个3)存在于6个子序列中,还剩下

2

3

2 3

3

3 3

2 3 3

2

3 2

3 3 2

2 3 3 2

现在,来考虑第2个3,按照“若序列中出现了重复数字,那么左边直接处理到该重复数字之前出现的位置后即可”,只需划掉3 和3 2两个序列中的3,还剩下

2

3

2 3

3

3 3

2 3 3

2

3 2

3 3 2

2 3 3 2

最后,划掉最后一个2存在的序列中的这个2,剩下

2

3

2 3

3

3 3

2 3 3

2

3 2

3 3 2

2 3 3 2

其实,求的就是这些划掉数字的和,因为序列中重复的数字不需要研究呀。

因此,对于序列中出现的第二个3,他出现的序列范围为啥不是从最左面开始选数字,而是从该数字最后一次出现的位置后选数字,

因为,从最后一次出现的位置前开始选数字,因为那个之前出现过的数字所包含于的序列,已经把那个之前出现过的数字加上了,没必要再加这个数字了,

eg:

这一段,这第二个3如果从最后一次出现的位置前开始选数字,假设从最左面取吧,那形成的序列是2 3 3,在研究第一个3时,该序列已经加过一个3了,(答案只需要该序列中加一个3就行呀)不需要再加第2个3了,所以没必要从最后一次出现的位置前选数字。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int last[MAXN];
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n, x;
        scanf("%d", &n);
        memset(last, -1, sizeof last);
        LL ans = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &x);
            ans += x * LL(i - last[x]) * (n - i);
            last[x] = i;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-08-25 09:19:49

ZOJ - 3872 Beauty of Array的相关文章

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(数学)

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 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&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 3842 Beauty of Array

Problem地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 根据题目的要求,需要算出所有连续子数组的the beauty的总和. 那么要求这个这个总和,刚开始最容易想到的就是这样: for( int i=1; i<=N; i++ ) { for( int j = 1; j<=i; j++ ) { ... //排除重复的数计算总和 } } 这样子的结果实际上是 Time Limit Exceeded 因此采取

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

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

12th浙江省省赛 B题 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