Beauty of Array

本文介绍了一个关于数组美的定义及其求解方法。Edward 定义数组的美为数组中所有不同整数的和,并希望通过算法计算出给定数组所有连续子数组美的总和。文章提供了一段 C 语言实现的代码示例,该算法能够有效地解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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 <stdio.h>
#include <string.h>
#include <stdlib.h>

int p[1123456];
long long x[112345];

int main(void)
{
    int t, n;

    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        memset(p, 0, sizeof(p));
        for (int i = 1; i <= n; i++)
            scanf("%lld", &x[i]);
        long long sum = 0;
        for (int i = 1; i <= n; i++)
        {
            sum += (n - i + 1) * (i - p[x[i]]) * x[i];
            p[x[i]] = i;
        }
        printf("%lld\n", sum);
    }
    return 0;
}
   There is an empty matrix M of size n×m . Zhongkao examination is over, and Daniel would like to do some puzzle games. He is going to fill in the matrix M using permutations of length m . That is, each row of M must be a permutation of length m† . Define the value of the i -th column in M as vi=MEX(M1,i,M2,i,…,Mn,i)‡ . Since Daniel likes diversity, the beauty of M is s=MEX(v1,v2,⋯,vm) . You have to help Daniel fill in the matrix M and maximize its beauty. † A permutation of length m is an array consisting of m distinct integers from 0 to m−1 in arbitrary order. For example, [1,2,0,4,3] is a permutation, but [0,1,1] is not a permutation (1 appears twice in the array), and [0,1,3] is also not a permutation (m−1=2 but there is 3 in the array). ‡ The MEX of an array is the smallest non-negative integer that does not belong to the array. For example, MEX(2,2,1)=0 because 0 does not belong to the array, and MEX(0,3,1,2)=4 because 0 , 1 , 2 and 3 appear in the array, but 4 does not. DeepL 翻译    有一个大小为 n×m 的空矩阵 M 。 中考结束了,丹尼尔想做一些益智游戏。他打算用长度为 m 的排列填充矩阵 M 。也就是说, M 的每一行都必须是长度为 m† 的排列。 将 M 中 i /th列的值定义为 vi=MEX(M1,i,M2,i,…,Mn,i)‡ 。因为丹尼尔喜欢多样性,所以 M 的美是 s=MEX(v1,v2,⋯,vm) 。 你必须帮助丹尼尔填入矩阵 M ,并最大化它的美感。 † 长度为 m 的排列是由 m 个不同的整数组成的数组,这些整数从 0 到 m−1 按任意顺序排列。例如, [1,2,0,4,3] 是一个排列,但 [0,1,1] 不是一个排列( 1 在数组中出现了两次), [0,1,3] 也不是一个排列( m−1=2 ,但数组中有 3 )。 ‡ 数组的 MEX 是不属于数组的最小非负整数。例如, MEX(2,2,1)=0 是因为 0 不属于数组,而 MEX(0,3,1,2)=4 是因为 0 、 1 、 2 和 3 出现在数组中,但 4 不属于数组。    Input The first line of input contains a single integer t (1≤t≤1000 ) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers n and m (1≤n,m≤2⋅105 ) — the size of the matrix. It is guaranteed that the sum of n⋅m over all test cases does not exceed 2⋅105 . DeepL 翻译    输入 输入的第一行包含一个整数 t ( 1≤t≤1000 ) - 测试用例的数量。测试用例说明如下。 每个测试用例的唯一一行包含两个整数 n 和 m ( 1≤n,m≤2⋅105 ) - 矩阵的大小。 保证所有测试用例
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值