AndyQsmart ACM学习历程——ZOJ3872 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

这题直接想到了思路1A了。

假设f(n)表示以a[n]结尾的子区间互异数的和的和。对于f(n-1),可能会有两部分组成,一部分是包含a[n]这个值的A,一部分是不包含a[n]这个值的B,假设B由p个区间加和。那么可以得到:f(n) = A + B + p*a[n] = f(n-1) + p*a[n]。所以关键是求p。

而由于f(n-1)是以a[n-1]结尾的子区间互异数的和,必然的,假设a[i]是距离a[n]最近的值为a[n]的数,则p = n-i。于是只需要记录最近出现值k的脚标即可。这里采用了map,数据范围不是非常大,也可以直接开数组。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long

using namespace std;

int main()
{
    //freopen("test.in", "r", stdin);
    int T, n, v, k;
    LL ans, f;
    scanf("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        scanf("%d", &n);
        map<int, int> s;
        ans = 0;
        f = 0;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d", &v);
            f = f + (i-s[v])*v;
            s[v] = i;
            ans += f;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-10-26 03:05:12

AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)的相关文章

ACM学习历程—HDU1041 Computer Transformation(递推 &amp;&amp; 大数)

Description A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, afte

ACM学习历程——HDU4472 Count(数学递推) (12年成都区域赛)

Description Prof. Tigris is the head of an archaeological team who is currently in charge of an excavation in a site of ancient relics.        This site contains relics of a village where civilization once flourished. One night, examining a writing r

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l

ACM学习历程—HDU2068 RPG的错排(组合数学)

Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿,G是月野兔:第二次猜:R是草儿,P是月野兔,G是公主:第三次猜:R是草儿,P是公主,G是月野兔:......可怜的野骆驼第六次终于把RPG分清楚了.由于RPG的带动,做ACM的女生越来越多,我们的野骆驼想都知道她们,可现在有N多人,他要猜的次数可就多了,为了不为难野骆驼,女生们只要求他答对一半或以

hdu-5496 Beauty of Sequence(递推)

题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 813    Accepted Submission(s): 379 Problem Description Sequence is beautiful and the beauty of an integer sequence is def

ACM学习历程—HDU5587 Array(数学 &amp;&amp; 二分 &amp;&amp; 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后从0到末尾,每一个都加上1. 例如:a0, a1, a2 => a0, a1, a2, 1, a0+1, a1+1, a2+1 题解中是这么说的:“ 其实Ai为i二进制中1的个数.每次变化A{k+2^i}=A{k}+1,(k<2^?i??)不产生进位,二进制1的个数加1.然后数位dp统计前m个数二