CF EDU 120 C - Set or Decrease
作者:互联网
C - Set or Decrease
一定是先对最小的元素使用 -1 操作
再从大到小把元素变为最小值
这样一定是最少操作将序列和减到 k 一下
操作数 = 操作 1 的个数 + 操作 2 的个数,操作 1 可能有很多次,操作 2 是将较大的元素变为最小值,所以可能是 [0, n - 1] 次,所以可以枚举操作 2 的个数
推公式
先对 a 数组从大到小排序,求出前缀和,设操作 2 有 i 次,就是将前 i 大的元素变为最小值,设操作 1 有 t 步
设一共要减少 sub = s[n] - k
- a[n] -> a[n] - t
- s[i] -> i * (a[n] - t)
所以这种情况减少了 t + s[i] - i * (a[n] - t) >= sub -> \(t >= \frac {sub + i*a[n]-s[i]}{i+1}\)
求出最小的 t,这种情况的答案就是 t + i
注意 t 可能小于 0,要令 t = max(t, 0)
枚举 i 求出最小值即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
ll a[N], s[N], k, sub;
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1, greater<ll>());
for (int i = 1; i <= n; i++)
s[i] = s[i-1] + a[i];
ll sub = s[n] - k;
if (sub <= 0)
{
cout << 0 << endl;
continue;
}
if (n == 1)
{
cout << sub << endl;
continue;
}
ll ans = 1e18;
for (int i = 0; i < n; i++)
{
ll t = (sub + i * a[n] - s[i] + i) / (i + 1);
t = max(t, 0ll);
ans = min(ans, t + i);
}
cout << ans << endl;
}
return 0;
}
二分
关键思路相同,更好写一点
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
ll a[N], s[N], k, sub;
bool check(ll x)
{
for (int i = 0; i <= min(x, n - 1ll); i++)
{
ll minn = a[n] - (x - i);
ll sum = i * minn + s[n-1] - s[i] + minn;
if (sum <= k)
return true;
}
return false;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1, greater<ll>());
for (int i = 1; i <= n; i++)
s[i] = s[i-1] + a[i];
ll sub = s[n] - k;
if (sub <= 0)
{
cout << 0 << endl;
continue;
}
if (n == 1)
{
cout << sub << endl;
continue;
}
ll l = 0, r = 1e18;
while(l + 1 != r)
{
ll mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid;
}
cout << r << endl;
}
return 0;
}
标签:Set,sub,int,ll,cin,120,Decrease,操作,include 来源: https://www.cnblogs.com/hzy717zsy/p/16264126.html