Monthly Expense (二分法)
作者:互联网
------------恢复内容开始------------
https://blog.csdn.net/u012469987/article/details/50897291 利用二分法求最小化最大值和最大化最小值
https://www.cnblogs.com/Sunnie69/p/5423817.html 思路从单点最大值到总和之间二分,保证右半区间正确的情况下减少左半区间的边界。
#include<iostream> #include<math.h> #include<string.h> #include<string> #include<stdio.h> using namespace std; int a[100005]; int m,n; bool ok(int x) { int ans=0; int res=1; for(int i=0; i<m; i++) { ans+=a[i]; if(ans>x) { res++; ans=a[i]; } } if(res<=n) return true; else return false; } int main() { while(~scanf("%d%d",&m,&n)) { memset(a,0,sizeof(a)); int maxn=a[0]; for(int i=0; i<m; i++) { scanf("%d",&a[i]); maxn=max(maxn,a[i]); } int left=maxn; int right=0x7fffffff; while(left<right) { int mid=left+(right-left)/2; //cout<<left<<" "<<mid<<" "<<right<<endl; if(ok(mid)) right=mid; else left=mid+1; } printf("%d\n",left); } }
------------恢复内容结束------------
标签:include,Monthly,res,二分法,int,https,ans,Expense 来源: https://www.cnblogs.com/RainzzZ/p/12109358.html