poj3273 Monthly Expense(二分搜索)

时间:2023-03-08 20:01:22
poj3273 Monthly Expense(二分搜索)

https://vjudge.net/problem/POJ-3273

认真审题,代码仔细!!ans的初值应该是1

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
ll n, m, a[];
ll C(ll x)
{
ll ans=, sum=;//认真审题啊!!ans的初值是1
for(int i = ; i < n; i++){
sum += a[i];
if(sum > x){
if(a[i]>x) return ;//单个就>
sum = a[i];
ans++;
}
if(ans>m){
return ;
}
}
return ;
}
int main()
{
while(cin >> n >> m){
for(int i = ; i < n; i++){
cin >> a[i];
}
ll lb = , ub = INF;
while(ub-lb>){
ll mid = (lb+ub)>>;
if(C(mid)){
ub = mid;
}
else{
lb = mid;
}
}
cout << ub << endl;
}
return ;
}