当前位置:网站首页>【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
2022-06-10 19:26:00 【solego】
题意:
给定一个长度为 n n n 的序列 a a a,以及一个数 k k k 。
每次你可以选择以下两个操作之一:
- 操作 1:选择一个元素 a i = a i − 1 a_i=a_i-1 ai=ai−1
- 操作 2:选择两个元素 a i a_i ai 和 a j a_j aj,使得 a i = a j a_i=a_j ai=aj
问至少多少次操作可以使得 ∑ i = 1 n a i ≤ k \sum\limits_{i=1}^n a_i \leq k i=1∑nai≤k
数据范围:
1 ≤ n ≤ 2 × 1 0 5 1\leq n\leq 2\times 10^5 1≤n≤2×105
1 ≤ k ≤ 1 0 15 1\leq k\leq 10^{15} 1≤k≤1015
1 ≤ a i ≤ 1 0 9 1\leq a_i\leq 10^9 1≤ai≤109
题解:
考虑如果选择操作 1,一定是将最小的 a i a_i ai 减 1 1 1 ,这样在之后进行操作 2 时,其他元素可以变得更小。因为操作 1 和操作 2 的次数是互相影响的,我们可以枚举其中一个操作的次数,然后计算出另一个操作的次数。
如果枚举操作 1 的次数, 1 ≤ a i ≤ 1 0 9 1\leq a_i\leq 10^9 1≤ai≤109,必定超时。
考虑枚举操作 2 的次数,即枚举除了最小的元素,其余 n − 1 n-1 n−1 个元素有多少个进行了操作 2 。设定有 m m m 个元素被赋值为最小元素,这些元素和最小的元素一起下降到 x x x ,使得最终有 x × ( m + 1 ) + 未 被 操 作 元 素 ≤ k x\times (m+1)+未被操作元素 \leq k x×(m+1)+未被操作元素≤k。
这里优先操作大的数,这样使得总和下降的更快。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
int n;
ll a[N], k;
ll pre[N];
void solve() {
scanf("%d%lld", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1, greater<int>());
for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + a[i];
if (pre[n] <= k) {
puts("0");
return ;
}
k = pre[n] - k;
ll ans = k;
for (int i = 1; i < n; ++i) {
// 这里先将 i 个元素修改成 a[n],然后这 i 个元素以及 a[n] 还需要如下公式的次数才可以不大于 k
ll y = max(0ll, k - pre[i] + a[n] * i + i) / (i + 1);
ans = min(ans, i + y);
}
printf("%lld\n", ans);
}
int main()
{
int T = 1;
scanf("%d", &T);
for (int i = 1; i <= T; ++i) {
solve();
}
return 0;
}
边栏推荐
- How to stack double and float in the bottom layer of C language
- Zabbix_ Monitoring ssh/crond Service - wechat alarm
- Solution to the problem that JLINK CDC UART driver cannot be installed normally under win7 system
- Dock/rancher2 deploy redis:5.0.9
- Routine solution - the problem of horse walking on the chessboard
- 云原生社区 大佬博客
- Analysis of epidemic situation in Shanghai based on improved SEIR model
- 推开混合云市场大门,Lenovo xCloud的破局之道
- When can Flink support the SQL client mode? When can I specify the applicati for submitting tasks to yarn
- 国家先进计算产业创新(宜昌)中心正式落地 中科曙光、升哲科技联合运营
猜你喜欢

Spark ShuffleManager

【录入课本latex记录】
![[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service](/img/9e/9ce804d84fb8ec9221b7c10bbd6c36.jpg)
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service

KP522201A采用 SOT23-6 封装的 4.5V 至 17V 输入、2A 输出、600kHz 同步降压转换器

2022 Devops roadmap | medium

企业级存储发展趋势谈:开源存储的冷思考

HM3416H降压IC芯片PWM/PFM 控制 DC-DC 降压转换器

FPGA state machine

Solution to the problem that JLINK CDC UART driver cannot be installed normally under win7 system

Key points of lldp protocol preparation
随机推荐
MySQL backup and manual execution of shell scripts are OK, and crontab scheduled execution fails
Tidb - quick start, cluster setup
[observation] shengteng Zhixing: scene driven, innovation first, press the "acceleration key" for Intelligent Transportation
Mongodb 唯一索引
江波龙 FORESEE XP2000 PCIe 4.0 SSD 多重加密功能,锁定数据安全
终于有人说清楚了Cookie、Session、Token的区别。详解,面试题。
站在今天这样一个时间节点上,或许对产业互联网有一个更加明晰的认识
Rotated Sorted Array旋转排序数组相关题
Redis cluster form - sentry mode cluster and high availability mode cluster - redis learning notes 003
On the development trend of enterprise storage: cold thoughts on open source storage
hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618
FS4521恒压线性充电IC
功耗开发经验分享:设计功耗大板
老程序员说:别再直译这大千世界了,开发人员应该回归程序设计
Solution to the problem that JLINK CDC UART driver cannot be installed normally under win7 system
玩艺术也得学数学?
SBC chip 35584 data manual pre regulator translation
[FAQ] summary of common problems and solutions during the use of rest API interface of sports health service
Basic instructions for ads and AXD
Zabbix_ Principle Architecture - installation and deployment - Custom monitoring