首页 > 代码库 > POJ 3273 Monthly Expense (二分)

POJ 3273 Monthly Expense (二分)

题目地址:POJ 3273

水题。居然错了两次,sad、、

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;
int a[110000];
int main()
{
        int n, m, i, cnt, sum;
        while(scanf("%d%d",&n,&m)!=EOF) {
                for(i=0; i<n; i++) {
                        scanf("%d",&a[i]);
                }
                int low=0, high=1e9, mid, ans;
                while(low<=high) {
                        mid=low+high>>1;
                        cnt=1;
                        sum=0;
                        for(i=0; i<n; i++) {
                                if(a[i]>mid) {
                                        cnt=m+1;
                                        break;
                                }
                                if(sum+a[i]>mid) {
                                        sum=a[i];
                                        cnt++;
                                } else   sum+=a[i];
                                if(cnt>m)  break;
                        }
                        if(cnt<=m) {
                                ans=mid;
                                high=mid-1;
                        } else  low=mid+1;
                }
                printf("%d\n",ans);
        }
        return 0;
}


POJ 3273 Monthly Expense (二分)