C. Make It Permutation

我们希望尽可能少地进行操作可以使代价最小,我们如果要排列的话,那些重复的元素我们无论如何都要进行删除的,所以我们可以先把去重的代价计算出来,然后依次枚举排列的位数是多少,也就是枚举去重后的数组,其中的代价我们可以一次计算出来,当我们枚举第i个a时,前面1有 i - 1个数,而1~ai - 1所有数都需要有,所以一共需要补ai - i个数,而i后面所有数都需要删除需要删m - i个数,代价我们可以通过O(1)的时间复杂度计算出来,然后枚举i更新答案即可。

时间复杂度:O(n)

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int c[N]; void run()
{
int n, a, b;
scanf("%d%d%d", &n, &a, &b);
LL val = 0, ans = 2e18;
set<int>s;
for(int i = 0; i < n; ++ i)
{
int x; scanf("%d", &x);
if(s.find(x) == s.end()) s.insert(x);
else val += a;
}
int m = 0;
for(auto it : s) c[++ m] = it; for(int i = 1; i <= m; ++ i)
{
LL k = 1LL * (c[i] - i) * b + 1LL * (m - i) * a;
ans = min(k, ans);
}
ans = min(ans, 1LL * m * a + b);
printf("%lld\n", val + ans);
} int main()
{
// freopen("1.in", "r", stdin);
int t;cin >> t;
while(t --) run();
return 0;
}

CodeTON Round 4 (Div. 1 + Div. 2)C的更多相关文章

  1. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  4. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  5. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  8. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  9. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  10. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

随机推荐

  1. ChatGPT变笨了,好在还有自知之明

    大家好,我是老章 好久没有写文章了,顺便向大家汇报一下最近在忙的事情(多数无疾而终): 1 开发了一个IMG2Latex工具(截图一个公式,自动把latex代码塞进剪贴板) 2 开发了一个播客转文字稿 ...

  2. Linux 设置 VI 快捷键 -- 在多个打开的文件中切换

    场景 部署完一系列服务后,想要查看所有服务的 catelina.out 日志: vi $(find /data/http | grep catalina.out | grep -v bak) 这个命令 ...

  3. quarkus实战之七:使用配置

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<quarkus实战>系列 ...

  4. Robot Framework 自动化测试部署常见问题及处理方法(一)

    1.在Python>>Scripts中运行python ride.py时报错 现象: 1 Traceback (most recent call last): 2 File "E ...

  5. 一篇博客带你上手Git

    概述 安装Git 下载官方网站,下载后安装包样式:双击安装,安装成功后右键文件会有如下选项证明安装成功. 基本配置 设置用户信息,桌面右键,选择Git bash here hecheng@LAPTOP ...

  6. linux测试ipv6

    前言 操作系统版本:centos 7.6 curl版本:7.87(centos 7自带的curl版本是7.29,测ipv6会有问题) 系统开启ipv6 centos 7默认开启 ipv6,可检查net ...

  7. python中将时间转换为时间戳

    某平台url中的时间格式为时间戳,将时间变量传入url前,需要将固定格式的时间转换为时间戳.使用python中的time模块,对时间的几种格式进行转换. strptime(),将时间字符串转换成 结构 ...

  8. 快速解决 const 与 typedef 类型组合时 ,const修饰谁的问题

    C++使用typedef 给复合类型定义别名时,与const结合会产生看似"令人困惑"的类型推定,例如 typedef char* pstring; const pstring c ...

  9. Health Kit基于数据提供专业方案,改善用户睡眠质量

    什么是CBT-I? 中国社科院等机构今年发布的<中国睡眠研究报告2023>内容显示,2022年,受访者的每晚平均睡眠时长为7.40小时,近半数受访者的每晚平均睡眠时长不足8小时(47.55 ...

  10. datetime获取当前日期前十二个月份

    from dateutil.parser import parse from dateutil.relativedelta import relativedelta # 当前日期前十二个月 time_ ...