Codeforces Round #526 (Div. 2) B. Kvass and the Fair Nut

Whiteying

2018-12-12 11:11:44

Personal

# C类 # 题目链接: https://codeforces.com/contest/1084/problem/B # 原题: **Problem Statement** The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are vi liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. **Input** The first line contains two integers n and s (1≤n≤103, 1≤s≤1012) — the number of kegs and glass volume. The second line contains n integers v1,v2,…,vn (1≤vi≤109) — the volume of i-th keg. **Output** If the Fair Nut cannot pour his glass by s liters of kvass, print −1. Otherwise, print a single integer — how much kvass in the least keg can be. **Examples** **input** 3 3 4 3 5 **output** 3 **input** 3 4 5 3 4 **output** 2 **input** 3 7 1 2 3 **output** -1 **Note** In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is −1. ------------ # 题意: 你有 n 桶水,第 i 桶水里初始时有 vi 升水,你需要从这 n 桶水中取出 s 升水,问是否能取 s 升水出来,如果可以的话让 n 桶水剩下水最少的那桶水尽量的多。 # 思路: 二分暴力 # AC代码: ``` #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; #include<algorithm> #include<queue> typedef long long ll; #include<vector> #define cin(n) scanf("%lld",&(n)) #define cout(n) printf("%lld",(n)) #define couc(c) printf("%c",(c)) #define coutn printf("\n") #define cout_ printf(" ") #define debug() printf("haha\n") const int MAXN= 1e6 + 5 ; ll t; ll n,k; ll a[MAXN]; ll minn = 0x3f3f3f3f; ll maxx = 0; bool check(ll v) { ll m=0; for(int i=1;i<=n;i++) { if(a[i]<v) return false; else m+=a[i] - v; } return m>=t; } int main() { cin(n); cin(t); for(int i=1;i<=n;i++) { cin(a[i]); k+=a[i]; maxx=max(maxx,a[i]); } if(k<t) { puts("-1"); return 0; } ll l=0,r=maxx; ll ret = -1; while(l<=r) { ll mid = l + r >>1 ; if(check(mid)) ret = mid, l =mid+1; else r=mid-1; } cout(ret); return 0; } ```