题解: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
For example,
You are given an array
The following process will be executed in a sequential loop until all numbers in
- Set
; - Let
be an array of size . Set for all , and then set for all .
Find the value of
我们将数组中的
例如,
给你一个大小为
下面的过程将依次循环执行,直到
- 设置
; - 设
为大小为 的数组。设 代表所有的 ,然后设 代表所有的 。
求过程结束后
Input
The first line contains an integer
For each test case:
- The first line contains an integer
( ) — the size of the array ; - The second line contains
integers ( ) — the elements of the array.
It is guaranteed that the sum of
输入
第一行包含一个整数
对于每个测试用例
- 第一行包含一个整数
( ) - 数组 的大小; - 第二行包含
个整数 ( ) - 数组的元素。
保证所有测试用例中
Output
For each test case, output the value of
输出
对于每个测试用例,在新行中输出
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,
In the first loop:
- Set
; - Set
, and then set .
After the first loop,
In the second test case,
After the first loop,
After the second loop,
After the third loop,
The value of
注
在第一个测试用例中,
在第一个循环中
- 设置
; - 设置
,然后设置 。
第一个循环后,
在第二个测试用例中,初始值为
第一个循环后,
第二个循环后,
第三次循环后,
进程结束后,
我的题解
首先,我的赛时提交是一个假做法(数据弱了)
(我用的是计数pair + deque)
想看的可以看->链接 幸好是赛后hack
正解
大家可以打个表
打完表之后可以发现
- 第一次循环完之后,数组已经是单调的了,但是部分数字的个数还是只有
个 - 第二次循环完之后,数组已经把所有只有一个的数字清除掉了
(每次循环记得更新 )
于是,我们就可以遍历了,假设从
这就是答案了
我的代码
#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 2024-07-21 13:14 Jiejiejiang 阅读(233) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」