首页 > 其他分享 >题解:Codeforces Round 960 (Div. 2) C

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

时间:2024-07-21 13:17:51浏览次数:12  
标签:le 960 int 题解 sum leq MAD Div operatorname

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 \(\operatorname{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 \(\operatorname{MAD}\) value is \(0\).

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

You are given an array \(a\) of size \(n\). Initially, a variable \(sum\) is set to \(0\).

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

  1. Set \(sum := sum + \sum_{i=1}^{n} a_i\);
  2. Let \(b\) be an array of size \(n\). Set \(b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i])\) for all \(1 \le i \le n\), and then set \(a_i := b_i\) for all \(1 \le i \le n\).

Find the value of \(sum\) after the process.

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

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

给你一个大小为 \(n\) 的数组 \(a\) 。初始化变量 \(sum\) 设置为 \(0\) 。

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

  1. 设置 \(sum := sum + \sum_{i=1}^{n} a_i\) ;
  2. 设 \(b\) 为大小为 \(n\) 的数组。设 \(b_i :=\ \operatorname{MAD}([a_1, a_2, \ldots, a_i])\) 代表所有的 \(1 \le i \le n\) ,然后设 \(a_i := b_i\) 代表所有的 \(1 \le i \le n\) 。

求过程结束后 \(sum\) 的值。

Input

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

For each test case:

  • The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the size of the array \(a\);
  • The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) — the elements of the array.

It is guaranteed that the sum of \(n\) over all test cases will not exceed \(2 \cdot 10^5\).

输入

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

对于每个测试用例

  • 第一行包含一个整数 \(n\) ( \(1 \leq n \leq 2 \cdot 10^5\) ) - 数组 \(a\) 的大小;
  • 第二行包含 \(n\) 个整数 \(a_1, a_2, \ldots, a_n\) ( \(1 \leq a_i \leq n\) ) - 数组的元素。

保证所有测试用例中 \(n\) 的总和不超过 \(2 \cdot 10^5\) 。

Output

For each test case, output the value of \(sum\) in a new line.

输出

对于每个测试用例,在新行中输出 \(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]\) initially.

In the first loop:

  1. Set \(sum := sum + a_1 = 0+1=1\);
  2. Set \(b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0\), and then set \(a_1 := b_1\).

After the first loop, \(a=[0]\) and the process ends. The value of \(sum\) after the process is \(1\).

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

After the first loop, \(a=[0,2,2]\) and \(sum=7\).

After the second loop, \(a=[0,0,2]\) and \(sum=11\).

After the third loop, \(a=[0,0,0]\) and \(sum=13\). Then the process ends.

The value of \(sum\) after the process is \(13\).

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

在第一个循环中

  1. 设置 \(sum := sum + a_1 = 0+1=1\) ;
  2. 设置 \(b_1 :=\ \operatorname{MAD}([a_1])=\ \operatorname{MAD}([1])=0\) ,然后设置 \(a_1 := b_1\) 。

第一个循环后, \(a=[0]\) 和进程结束。进程结束后, \(sum\) 的值为 \(1\) 。

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

第一个循环后, \(a=[0,2,2]\) 和 \(sum=7\) 。

第二个循环后, \(a=[0,0,2]\) 和 \(sum=11\) 。

第三次循环后, \(a=[0,0,0]\) 和 \(sum=13\) 。然后进程结束。

进程结束后, \(sum\) 的值为 \(13\) 。

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

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

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

于是,我们就可以遍历了,假设从 \(0\) 开始存储的数组,一直到 \(n-1\) ,遍历数组,对于第 \(i\) 个数字 \(a_i\),\(sum\) 加上 \(a_i×(n-i)\)( 如果是从 \(1\) 开始存储的那就是 \(a_i×(n-i+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;
}

标签:le,960,int,题解,sum,leq,MAD,Div,operatorname
From: https://www.cnblogs.com/jiejiejiang2004/p/18314382

相关文章

  • NOI2024 集合 题解
    给个链接:集合。很神秘的题目。基本上看到之后就可以想到哈希。首先想到一个比较神秘的暴力。就是对于每个询问,扫一遍所有\(a\)中的数出现的位置,把它弄成一个哈希值(具体怎么弄随意)存到set里,然后看看是不是和\(b\)中的数出现的位置这样操作后的集合完全相等。事实上就是判断......
  • Codeforces Round 960 (Div. 2) A - D
    link好图好图qwq这次也是终于装上插件codeforcesbetter!了,妈妈再也不用担心我的英语啦A-SubmissionBaitA先手,B后手,优先往奇偶性的方向想一开始我只是单纯考虑A总是选最大值的数的奇偶性,样例过了?交上去发现wa2然后恼...瞎搞了个33344,发现A可以先选3......
  • B3956 [GESP202403 三级] 字母求和 题解
    B3956[GESP202403三级]字母求和题解当时在考试,3分钟A了,结果第二题T了。#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+2;constintN1=1e3+2;typedeflonglongll;typedefunsignedlonglongull;#definefo(i,n,m)for(inti=n;i<=m;i++)......
  • 【科大讯飞笔试题汇总】2024-07-20-科大讯飞秋招提前批(研发岗)-三语言题解(Cpp/Java/
    ......
  • 题解:Codeforces Round 960 (Div. 2) B
    B.ArrayCrafttimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputForanarray\(b\)ofsize\(m\),wedefine:themaximumprefixpositionof\(b\)isthesmallestindex\(i\)thatsat......
  • Codeforces Round 960 (Div.2) 补题
    A非常容易观察到性质,注意Alice为先手,发现当\(a_{\max}\)的个数为奇数时显然能win,但如果\(a_{\max}\)的个数为偶数且有一个数具有奇数个可以作为跳板,那么也能win,否则就lose。B结论:\(presum_x\geq2+presum_{y-1}\geq2+\min{presum_i}\geq1+\max{presum_i}......
  • 洛谷 P1162 填涂颜色题解
    题目链接填涂颜色题目描述由数字\(0\)组成的方阵中,有一任意形状的由数字\(1\)构成的闭合圈。现要求把闭合圈内的所有空间都填写成\(2\)。例如:\(6\times6\)的方阵(\(n=6\)),涂色前和涂色后的方阵如下:如果从某个\(0\)出发,只向上下左右\(4\)个方向移动且仅经过其他\(0\)......
  • 题解:Codeforces Round 960 (Div. 2) A
    A.SubmissionBaittimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAliceandBobareplayingagameinanarray\(a\)ofsize\(n\).Theytaketurnstodooperations,withAlicestarting......
  • Codeforces Round 960 (Div. 2) 补题记录(A~D)
    打的稀烂,但是还是上分了(A考虑对值域做一个后缀和。若某一个后缀和的值是奇数那么先手就可以获胜。否则就不可以获胜。(我才不会告诉你我这题吃了一次罚时的)#pragmaGCCoptimize(3)#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;intmysqrt(intx){......
  • 使用牛顿法近似整数的 sqrt,ZeroDivisionError
    Sqrt(x)给定一个非负整数x,返回x的平方根,向下舍入到最接近的整数。返回的整数也应该是非负数。不得使用任何内置指数函数或运算符。例如,不要在c++中使用pow(x,0.5)或在c++中使用x**0.5python.示例1:输入:x=4输出:2解释:4的平方根是2,所......