当前位置:网站首页>C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
C. Set or Decrease-Educational Codeforces Round 120 (Rated for Div. 2)
2022-06-23 17:21:00 【Qin Sanma】
C. Set or Decrease
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an integer array a1,a2,…,ana1,a2,…,an and integer kk.
In one step you can
- either choose some index ii and decrease aiai by one (make ai=ai−1ai=ai−1);
- or choose two indices ii and jj and set aiai equal to ajaj (make ai=ajai=aj).
What is the minimum number of steps you need to make the sum of array ∑i=1nai≤k∑i=1nai≤k? (You are allowed to make values of array negative).
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105; 1≤k≤10151≤k≤1015) — the size of array aa and upper bound on its sum.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the array itself.
It's guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.
Output
For each test case, print one integer — the minimum number of steps to make ∑i=1nai≤k∑i=1nai≤k.
Example
input
Copy
4 1 10 20 2 69 6 9 7 8 1 2 1 3 1 2 1 10 1 1 2 3 1 2 6 1 6 8 10
output
Copy
10 0 2 7
Note
In the first test case, you should decrease a1a1 1010 times to get the sum lower or equal to k=10k=10.
In the second test case, the sum of array aa is already less or equal to 6969, so you don't need to change it.
In the third test case, you can, for example:
- set a4=a3=1a4=a3=1;
- decrease a4a4 by one, and get a4=0a4=0.
As a result, you'll get array [1,2,1,0,1,2,1][1,2,1,0,1,2,1] with sum less or equal to 88 in 1+1=21+1=2 steps.
In the fourth test case, you can, for example:
- choose a7a7 and decrease in by one 33 times; you'll get a7=−2a7=−2;
- choose 44 elements a6a6, a8a8, a9a9 and a10a10 and them equal to a7=−2a7=−2.
As a result, you'll get array [1,2,3,1,2,−2,−2,−2,−2,−2][1,2,3,1,2,−2,−2,−2,−2,−2] with sum less or equal to 11 in 3+4=73+4=7 steps.
It is obviously a greedy problem , We sort only the smallest ones -1 operation , The rest of the numbers are transformed to minimize the number of times .
There are two unknowns here , One is that the minimum value decreases x, One is a transformed number , What we know is that the numbers we transform must start from the back , We've chosen m The next one
So we have the relation
(m+1)(a1-x) + sum[n-m]-sum[1] <= k
After the change , You can find , If we enumerate m, So for x In fact, there is a value range , Here we call ceil Function takes an upper bound to get a definite m Minimum x, Enumerate each m, Take the minimum value of sum .
# include<iostream>
# include<algorithm>
# include<math.h>
using namespace std;
typedef long long int ll;
double a[200000+10],sum[200000+10];
int main()
{
int t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+a[i];
}
ll ans=0x7f7f7f7f7f7f7f7f;
for(ll m=0;m<n;m++)
{
ll x=max((ll)0,(ll)ceil(a[1]-(k+sum[1]-sum[n-m])/(double)(m+1)));
ans=min(ans,m+x);
}
cout<<ans<<endl;
}
return 0;
}边栏推荐
- [go] calling Alipay to scan code for payment in a sandbox environment
- 数学分析_证明_第1章:可数个可数集之并为可数集
- R language uses timeroc package to calculate the multi time AUC value of survival data in the case of no competition, uses Cox model, adds covariates, and visualizes the multi time ROC curve of surviv
- Freemark uses FTL files to generate word
- Tupu software builds smart city with lightweight modeling
- 时间戳90K是什么意思?
- Troubleshooting of datanode entering stale status
- Apache基金会正式宣布Apache InLong成为顶级项目
- The Google play academy team PK competition is in full swing!
- 内网渗透令牌窃取
猜你喜欢

数字经济加速落地,能为中小企业带来什么?

面渣逆袭:MySQL六十六问!建议收藏
NLP paper reading | improving semantic representation of intention recognition: isotropic regularization method in supervised pre training

Counter attack by flour dregs: MySQL 66 question! Suggested collection

接口的所有权之争

使用Jmeter进行性能测试及性能监控平台搭建

Digital twin excavator of Tupu software realizes remote control
![[untitled] Application of laser welding in medical treatment](/img/c5/9c9edf1c931dfdd995570fa20cf7fd.png)
[untitled] Application of laser welding in medical treatment
![Leetcode: interview question 08.13 Stacking bin [top-down DFS + memory or bottom-up sorting + DP]](/img/22/220e802da7543c2b14b7057e4458b7.png)
Leetcode: interview question 08.13 Stacking bin [top-down DFS + memory or bottom-up sorting + DP]

Here comes the official zero foundation introduction jetpack compose Chinese course!
随机推荐
[today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online
C # connection to database
Date to localdatetime
R language uses colorblinr package to simulate color blind vision, and uses edit to visualize the image of ggplot2_ The colors function is used to edit and convert color blindness into visual results
ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
Redis cluster operation method
Jetpack Compose 与 Material You 常见问题解答
The evolution of social structure and capital system brought about by the yuan universe
ABAP随笔-物料主数据界面增强
如何选择券商?手机开户安全么?
什么是抽象类?怎样定义抽象类?
谈谈redis缓存击穿透和缓存击穿的区别,以及它们所引起的雪崩效应
IFLYTEK neuroimaging disease prediction program!
R language plot visualization: plot visualization adds bar chart with error bars with plot in R
电感参数有哪些?怎么选择电感?
qYKVEtqdDg
网络远程访问树莓派(VNC Viewer)
Three minutes to learn how to retrieve the MySQL password
Network remote access raspberry pie (VNC viewer)
读书郎通过上市聆讯:平板业务毛利率走低,2021年利润同比下滑11%