jiejiejiang2004

题解: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 (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 value is 0.

For example, MAD([1,2,1])=1, MAD([2,2,3,3])=3, 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+i=1nai;
  2. Let b be an array of size n. Set bi:= MAD([a1,a2,,ai]) for all 1in, and then set ai:=bi for all 1in.

Find the value of sum after the process.

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

例如, MAD([1,2,1])=1MAD([2,2,3,3])=3MAD([1,2,3,4])=0

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

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

  1. 设置 sum:=sum+i=1nai
  2. b 为大小为 n 的数组。设 bi:= MAD([a1,a2,,ai]) 代表所有的 1in ,然后设 ai:=bi 代表所有的 1in

求过程结束后 sum 的值。

Input

The first line contains an integer t (1t2104) — the number of test cases.

For each test case:

  • The first line contains an integer n (1n2105) — the size of the array a;
  • The second line contains n integers a1,a2,,an (1ain) — the elements of the array.

It is guaranteed that the sum of n over all test cases will not exceed 2105.

输入

第一行包含一个整数 t ( 1t2104 ) - 测试用例的数量。

对于每个测试用例

  • 第一行包含一个整数 n ( 1n2105 ) - 数组 a 的大小;
  • 第二行包含 n 个整数 a1,a2,,an ( 1ain ) - 数组的元素。

保证所有测试用例中 n 的总和不超过 2105

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+a1=0+1=1;
  2. Set b1:= MAD([a1])= MAD([1])=0, and then set a1:=b1.

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+a1=0+1=1
  2. 设置 b1:= MAD([a1])= MAD([1])=0 ,然后设置 a1:=b1

第一个循环后, 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 开始存储的数组,一直到 n1 ,遍历数组,对于第 i 个数字 aisum 加上 ai×(ni)( 如果是从 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;
}

posted on   Jiejiejiang  阅读(233)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」

导航

统计信息

点击右上角即可分享
微信分享提示