Fork me on GitHub

POJ3273 Monthly Expense

 

  查看原题

  边界,就是边界和思维,怎么有效的判断中间值是大了还是小了,以及准确的找到边界!一个<写成<=就前功尽弃,还特别难找到错误!

 1 #include <cstdio>
 2 #include <algorithm>
 3 const int maxN = 100005;
 4 int N, M;
 5 int A[maxN];
 6 using namespace std;
 7 int main(void) {
 8     while (2 == scanf("%d%d", &N, &M)) {
 9         int u = 0, l = 0, m, sum, cnt, i;
10         for (int i = 0; i < N; i++) {
11             scanf("%d", &A[i]);
12             u += A[i];
13             l = max(l, A[i]);
14         }
15         while (u > l) {
16             m = (l+u)>>1;
17             sum = cnt = 0;
18             for (i = 0; i < N; i++) {
19                 sum += A[i];
20                 if (sum > m) {
21                     cnt++;
22                     sum = A[i];
23                 }
24             }
25             if (cnt < M) u = m;
26             else l = m+1;
27         }
28         printf("%d\n", l);
29     }
30 }
View Code

 

posted @ 2016-08-13 18:07  赵裕(vimerzhao)  阅读(132)  评论(0编辑  收藏  举报