题解:Codeforces Round 960 (Div. 2) C

C. Mad MAD Sum

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

We define the MAD ⁡ \operatorname{MAD} MAD (Maximum Appearing Duplicate) in an array as the largest number that appears at least twice in the array. Specifically, if there is no number that appears at least twice, the MAD ⁡ \operatorname{MAD} MAD value is 0 0 0.

For example, MAD ⁡ ( [ 1 , 2 , 1 ] ) = 1 \operatorname{MAD}([1, 2, 1]) = 1 MAD([1,2,1])=1, MAD ⁡ ( [ 2 , 2 , 3 , 3 ] ) = 3 \operatorname{MAD}([2, 2, 3, 3]) = 3 MAD([2,2,3,3])=3, MAD ⁡ ( [ 1 , 2 , 3 , 4 ] ) = 0 \operatorname{MAD}([1, 2, 3, 4]) = 0 MAD([1,2,3,4])=0.

You are given an array a a a of size n n n. Initially, a variable s u m sum sum is set to 0 0 0.

The following process will be executed in a sequential loop until all numbers in a a a become 0 0 0:

  1. Set s u m : = s u m + ∑ i = 1 n a i sum := sum + \sum_{i=1}^{n} a_i sum:=sum+i=1nai;
  2. Let b b b be an array of size n n n. Set b i : =   MAD ⁡ ( [ a 1 , a 2 , … , a i ] ) b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i]) bi:= MAD([a1,a2,,ai]) for all 1 ≤ i ≤ n 1 \le i \le n 1in, and then set a i : = b i a_i := b_i ai:=bi for all 1 ≤ i ≤ n 1 \le i \le n 1in.

Find the value of s u m sum sum after the process.

我们将数组中的 MAD ⁡ \operatorname{MAD} MAD (最大重复出现数)定义为数组中至少出现两次的最大数字。具体来说,如果没有至少出现两次的数字, MAD ⁡ \operatorname{MAD} MAD 的值就是 0 0 0

例如, MAD ⁡ ( [ 1 , 2 , 1 ] ) = 1 \operatorname{MAD}([1, 2, 1]) = 1 MAD([1,2,1])=1 MAD ⁡ ( [ 2 , 2 , 3 , 3 ] ) = 3 \operatorname{MAD}([2, 2, 3, 3]) = 3 MAD([2,2,3,3])=3 MAD ⁡ ( [ 1 , 2 , 3 , 4 ] ) = 0 \operatorname{MAD}([1, 2, 3, 4]) = 0 MAD([1,2,3,4])=0

给你一个大小为 n n n 的数组 a a a 。初始化变量 s u m sum sum 设置为 0 0 0

下面的过程将依次循环执行,直到 a a a 中的所有数字都变成 0 0 0

  1. 设置 s u m : = s u m + ∑ i = 1 n a i sum := sum + \sum_{i=1}^{n} a_i sum:=sum+i=1nai
  2. b b b 为大小为 n n n 的数组。设 b i : =   MAD ⁡ ( [ a 1 , a 2 , … , a i ] ) b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i]) bi:= MAD([a1,a2,,ai]) 代表所有的 1 ≤ i ≤ n 1 \le i \le n 1in ,然后设 a i : = b i a_i := b_i ai:=bi 代表所有的 1 ≤ i ≤ n 1 \le i \le n 1in

求过程结束后 s u m sum sum 的值。

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104) — the number of test cases.

For each test case:

  • The first line contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the size of the array a a a;
  • The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain) — the elements of the array.

It is guaranteed that the sum of n n n over all test cases will not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输入

第一行包含一个整数 t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1t2104 ) - 测试用例的数量。

对于每个测试用例

  • 第一行包含一个整数 n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105 ) - 数组 a a a 的大小;
  • 第二行包含 n n n 个整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain ) - 数组的元素。

保证所有测试用例中 n n n 的总和不超过 2 ⋅ 1 0 5 2 \cdot 10^5 2105

Output

For each test case, output the value of s u m sum sum in a new line.

输出

对于每个测试用例,在新行中输出 s u m sum sum 的值。

Example

input

4
1
1
3
2 2 3
4
2 1 1 2
4
4 4 4 4

output

1
13
9
40

Note

In the first test case, a = [ 1 ] a=[1] a=[1] initially.

In the first loop:

  1. Set s u m : = s u m + a 1 = 0 + 1 = 1 sum := sum + a_1 = 0+1=1 sum:=sum+a1=0+1=1;
  2. Set b 1 : =   MAD ⁡ ( [ a 1 ] ) =   MAD ⁡ ( [ 1 ] ) = 0 b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0 b1:= MAD([a1])= MAD([1])=0, and then set a 1 : = b 1 a_1 := b_1 a1:=b1.

After the first loop, a = [ 0 ] a=[0] a=[0] and the process ends. The value of s u m sum sum after the process is 1 1 1.

In the second test case, a = [ 2 , 2 , 3 ] a=[2,2,3] a=[2,2,3] initially.

After the first loop, a = [ 0 , 2 , 2 ] a=[0,2,2] a=[0,2,2] and s u m = 7 sum=7 sum=7.

After the second loop, a = [ 0 , 0 , 2 ] a=[0,0,2] a=[0,0,2] and s u m = 11 sum=11 sum=11.

After the third loop, a = [ 0 , 0 , 0 ] a=[0,0,0] a=[0,0,0] and s u m = 13 sum=13 sum=13. Then the process ends.

The value of s u m sum sum after the process is 13 13 13.

在第一个测试用例中, a = [ 1 ] a=[1] a=[1] 初始化。

在第一个循环中

  1. 设置 s u m : = s u m + a 1 = 0 + 1 = 1 sum := sum + a_1 = 0+1=1 sum:=sum+a1=0+1=1
  2. 设置 b 1 : =   MAD ⁡ ( [ a 1 ] ) =   MAD ⁡ ( [ 1 ] ) = 0 b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0 b1:= MAD([a1])= MAD([1])=0 ,然后设置 a 1 : = b 1 a_1 := b_1 a1:=b1

第一个循环后, a = [ 0 ] a=[0] a=[0] 和进程结束。进程结束后, s u m sum sum 的值为 1 1 1

在第二个测试用例中,初始值为 a = [ 2 , 2 , 3 ] a=[2,2,3] a=[2,2,3]

第一个循环后, a = [ 0 , 2 , 2 ] a=[0,2,2] a=[0,2,2] s u m = 7 sum=7 sum=7

第二个循环后, a = [ 0 , 0 , 2 ] a=[0,0,2] a=[0,0,2] s u m = 11 sum=11 sum=11

第三次循环后, a = [ 0 , 0 , 0 ] a=[0,0,0] a=[0,0,0] s u m = 13 sum=13 sum=13 。然后进程结束。

进程结束后, s u m sum sum 的值为 13 13 13

我的题解
首先,我的赛时提交是一个假做法(数据弱了)
(我用的是计数pair + deque)
想看的可以看->链接 幸好是赛后hack
被hack

正解
大家可以打个表
打完表之后可以发现

  • 第一次循环完之后,数组已经是单调的了,但是部分数字的个数还是只有 1 1 1
  • 第二次循环完之后,数组已经把所有只有一个的数字清除掉了
    (每次循环记得更新 s u m sum sum

于是,我们就可以遍历了,假设从 0 0 0 开始存储的数组,一直到 n − 1 n-1 n1 ,遍历数组,对于第 i i i 个数字 a i a_i ai s u m sum sum 加上 a i × ( n − i ) a_i×(n-i) ai×(ni)( 如果是从 1 1 1 开始存储的那就是 a i × ( n − i + 1 ) a_i×(n-i+1) ai×(ni+1)

这就是答案了

我的代码

#include <bits/stdc++.h>
#define int long long

const int N = 1e7 + 10;
int t;
int a[N];

void solve() {
    int n;
    std::cin >> n;
    for(int i = 1 ; i <= n ; i ++) std::cin >> a[i];
    int sum = 0;;

    for(int q = 0 ; q < 2 ; q ++){
        int mad = 0;
        std::map<int,int> mp;
        for(int i = 1 ; i <= n ; i ++) {
            sum += a[i];
            mp[a[i]] ++;
            if(mp[a[i]] > 1) mad = std::max(mad,a[i]);
            a[i] = mad;
        }
    }

    for(int i = 1 ; i <= n ; i ++) {
        sum += a[i] * (n-i+1);
    }
    std::cout  << sum << "\n";
}

signed main() {
    std::cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

搬运自https://www.cnblogs.com/jiejiejiang2004/p/18314382
博主已同意,我就是博主

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值