Title link: https://ac.nowcoder.com/acm/problem/19916
Topic
- n kinds of ordinary cards, ci cards of each kind, m universal cards
- There are a total of n cards in a deck, that is, one for each of the n kinds of ordinary cards. The universal card can replace any of the n kinds of ordinary cards, and only one card can be used in a deck.
- How many sets of cards can be composed of these cards
- 范围:2< = n < = 50, 0 < = m, ci <= 500,000,000
Ideas
- Two points.
- Assuming that a total of x sets of cards can be formed, then calculate the amount owed by each card and add it up to cnt.
- First, cnt must be less than or equal to m, and universal cards must be enough; then, each deck of cards can appear at most once, so at most x cards. So cnt<=x&&cnt<=m.
- The cnt here needs to open long long, because the maximum is 5e8*50 over 2e10, which bursts int
ac code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[105], n, m;
bool check(int x){
ll cnt = 0;
for(int i = 1; i <= n; i ++){
cnt += max(0, x - a[i]);
}
return cnt <= x && cnt <= m;
}
int main(){
int l = 0, r = 1e9, ans = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++){
scanf("%d", &a[i]);
}
while(l <= r){
int mid = l + r >> 1;
if(check(mid)) l = mid + 1, ans = mid;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}