poj3273 Monthly Expense(二分搜索)
https://vjudge.net/problem/POJ-3273
认真审题,代码仔细!!ans的初值应该是1
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 #include<map> 8 #define lson l, m, rt<<1 9 #define rson m+1, r, rt<<1|1 10 #define INF 0x3f3f3f3f 11 typedef unsigned long long ll; 12 using namespace std; 13 ll n, m, a[100010]; 14 ll C(ll x) 15 { 16 ll ans=1, sum=0;//认真审题啊!!ans的初值是1 17 for(int i = 0; i < n; i++){ 18 sum += a[i]; 19 if(sum > x){ 20 if(a[i]>x) return 0;//单个就> 21 sum = a[i]; 22 ans++; 23 } 24 if(ans>m){ 25 return 0; 26 } 27 } 28 return 1; 29 } 30 int main() 31 { 32 while(cin >> n >> m){ 33 for(int i = 0; i < n; i++){ 34 cin >> a[i]; 35 } 36 ll lb = 0, ub = INF; 37 while(ub-lb>1){ 38 ll mid = (lb+ub)>>1; 39 if(C(mid)){ 40 ub = mid; 41 } 42 else{ 43 lb = mid; 44 } 45 } 46 cout << ub << endl; 47 } 48 return 0; 49 }