Set or Decrease(1600)

 1 /**\
 2 https://codeforces.com/problemset/problem/1622/C
 3 贪心:
 4 先将数组排序,要么减小a[1]值,要么从后面开始变后面的数->a[1],枚举x y就可
6 后附公式证明
5 \**/ 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define fi first 9 #define se second 10 #define go continue 11 #define woc puts(""); 12 #define int long long 13 #define PII pair<int, int> 14 #define sf(x) scanf("%lld",&x) 15 #define ytz int _; sf(_); while(_--) 16 #define fory(i,a,b) for(int i = a; i <= b; ++i) 17 #define forl(i,a,b) for(int i = a; i >= b; --i) 18 #define debug(a) cout << #a << " = " << a <<endl; 19 const int N = 2e5 + 10; 20 int n, k; 21 int a[N], s[N]; 22 inline void solve() 23 { 24 int ans = 0x3f3f3f3f; 25 sf(n), sf(k); 26 fory(i, 1, n) sf(a[i]); 27 sort(a + 1, a + 1 + n); 28 fory(i, 1, n) s[i] = s[i - 1] + a[i]; 29 if(s[n] <= k) 30 { 31 puts("0"); 32 return; 33 } 34 for(int y = 0; y < n; ++y) 35 { 36 int tmp = k - s[n - y] + s[1] - a[1]*y - a[1]; 37 int x = ceil(-1.0*tmp/(y + 1)); 38 if(x <= 0) x = 0; 39 ans = min(ans, x + y); 40 } 41 printf("%lld\n",ans); 42 } 43 signed main() 44 { 45 ytz 46 { 47 solve(); 48 } 49 return 0; 50 }

 

posted @ 2022-03-01 23:42  std&ice  阅读(78)  评论(0编辑  收藏  举报