题目大意:不废话,最小化最大值
还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意
联动3258
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 5 using namespace std; 6 7 static int money_set[100010]; 8 9 void Search(const int, const int, const int); 10 bool C(const int, const int, const int); 11 12 int main(void) 13 { 14 int days, set_sum; 15 while (~scanf("%d%d", &days, &set_sum)) 16 { 17 for (int i = 0; i < days; i++) 18 scanf("%d", &money_set[i]); 19 Search(days, set_sum, 100000); 20 } 21 return 0; 22 } 23 24 void Search(const int days, const int set_sum,const int max_m) 25 { 26 int lb = 0, rb = max_m, mid; 27 28 while (rb - lb > 1) 29 { 30 mid = (rb + lb) / 2; 31 if (C(mid, set_sum, days))//二分逼近,最后的rb即为所求 32 rb = mid; 33 else 34 lb = mid; 35 } 36 printf("%d\n", rb); 37 } 38 39 bool C(const int x, const int set_sum, const int days) 40 { 41 //这次是最小化最大值,注意变通 42 int tmp_m, pos = 0, uesd; 43 44 for (uesd = 0; pos < days && uesd < set_sum; uesd++) 45 { 46 for (tmp_m = 0; pos < days && money_set[pos] + tmp_m <= x; pos++) 47 { 48 tmp_m += money_set[pos]; 49 if (x < money_set[pos]) 50 return false; 51 } 52 } 53 if (uesd < set_sum ||(uesd == set_sum && pos == days)) 54 return true; 55 else return false; 56 }
时间: 2024-11-10 01:03:07