This commit is contained in:
Your Name committed 2026-10-31 23:26:53 +08:00
1 parent bf08016dee
commit 8b21377d1f
1 file changed
+20 -10
+20 -10
View File
@@ -3,17 +3,25 @@ using namespace std;
int n;
long long m;
long long a[500005];
long long check(long long now) {
int tot = lower_bound(a + 1, a + n + 1, now) - a - 1;
// printf("%d\n", tot);
long long qsum[500005];
bool checkb(int mid, int now, int tot) {
return mid * now - (qsum[tot] - qsum[tot - mid]) <= m;
}
long long check(long long now, int tot) {
long long nowm = m;
long long ans = 0;
while(a[tot + 1] == now && tot + 1 <= n) tot++;
while(nowm >= (now - a[tot]) && tot >= 1) {
nowm -= now - a[tot], ans++, tot--;
// printf("%lld %d\n", nowm, tot);
int l = 1, r = tot, mid = (l + r) >> 1;
while(l < r) {
mid = (l + r) >> 1;
if(checkb(mid, now, tot)) {
l = mid + 1;
} else {
r = mid;
}
return ans;
}
while(!checkb(mid, now, tot) && mid >= 1) mid--;
while(checkb(mid + 1, now, tot) && mid + 1 <= tot) mid++;
return mid;
}
int main() {
freopen("crystal.in", "r", stdin);
@@ -21,12 +29,14 @@ int main() {
scanf("%d%lld", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; ++i) qsum[i] = qsum[i - 1] + a[i];
long long ans = 0;
for(int i = 1; i <= n; ++i) {
ans = max(ans, check(a[i]));
if(a[i + 1] == a[i]) continue;
ans = max(ans, check(a[i], i));
// printf("%d %lld\n", i, check(a[i], i));
}
printf("%lld\n", ans);
// printf("%lld\n", check(4));
return 0;
}
/*