From 8b21377d1fa3208ef8d27b7592e91d1dbf8a40fc Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 31 Oct 2026 23:26:53 +0800 Subject: [PATCH] 23:26 --- crystal/crystal.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/crystal/crystal.cpp b/crystal/crystal.cpp index 346b7fd..9270efa 100644 --- a/crystal/crystal.cpp +++ b/crystal/crystal.cpp @@ -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; } /*