题意:经典的最大值最小问题……给FJ在n天里每天花的钱数,问如果将n天分成m组,每组内的天数连续,最少每组给多少钱可以让每组的钱够花。
解法:二分。现在看来是个挺简单的二分……然而我二分一向都是理论AC……写出来跟屎一样……QAQ……贴个代码当模板好了……
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include<iomanip> #define LL long long #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 using namespace std; int a[100005]; int n, m; bool judge(int mid) { int cnt = 1; int sum = 0; for(int i = 0; i < n; i++) { if(a[i] > mid) return false; if(sum + a[i] > mid) { cnt++; sum = a[i]; } else { sum += a[i]; } } return cnt <= m; } int main() { while(~scanf("%d%d", &n, &m)) { for(int i = 0; i < n; i++) scanf("%d", &a[i]); int l = 0, r = 1000000000; int mid = (l + r) >> 1; while(l < r) { mid = (l + r) >> 1; if(judge(mid)) r = mid; else l = mid + 1; } cout << l << endl; } return 0; }