标签:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const int maxn = 105; int n,m,s,c[maxn]; void input(){ cin>>n>>m; s = 0; for(int i = 1;i <= n;i++){ scanf("%d",&c[i]); if(s < c[i]) s = c[i]; } s += m; } bool check(int t){ int add = 0; for(int i = 1;i <= n;i++){ if(t - c[i] > 0) add += t - c[i]; if(t < add || m < add) return false; } return true; } void div(){ int lans = 0,rans = s,mans; while(lans <= rans){ mans = (lans + rans) >> 1; if(check(mans)){ lans = mans + 1; }else{ rans = mans - 1; } } if(check(mans))cout<<mans; else cout<<mans-1; } int main(){ input(); div(); return 0; }
还有一种做法,在保证joker数量满足的情况下,确保前面的需求不大于当前牌的数量就可以,如果前面的需求后面能满足,那么后面的需求前面就一定能满足
证明:
假设当前判定牌的种类的数量为j,补充此类牌后,不满足要求的牌类为i,j之前的牌的需求,牌组数t
j满足要求,即有j >= add
有add + (t - j) - (t - i)> i
add - j + i > i
j < add
与“j满足要求”的前提矛盾,等证前文的结论成立
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const int maxn = 105; int n,m,s,c[maxn]; void input(){ cin>>n>>m; s = 0; for(int i = 1;i <= n;i++){ scanf("%d",&c[i]); if(s < c[i]) s = c[i]; } s += m; } bool check(int t){ int add = t - c[1]; if(add < 0) add = 0; for(int i = 2;i <= n;i++){ if(c[i] < add || m < add) return false; else{ if(t - c[i] > 0) add += t - c[i]; } } if(m < add) return false; return true; } void div(){ int lans = 0,rans = s,mans; while(lans <= rans){ mans = (lans + rans) >> 1; if(check(mans)){ lans = mans + 1; }else{ rans = mans - 1; } } if(check(mans))cout<<mans; else cout<<mans-1; } int main(){ input(); div(); return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5655820.html