|洛谷|二分|P1182 数列分段Section II

来源:互联网 发布:linux常用命令实例详解 编辑:程序博客网 时间:2024/05/22 11:57

http://www.luogu.org/problem/show?pid=1182

用二分答案,check即可

#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#define ms(i,j) memset(i,j, sizeof i);using namespace std;int n,m;int a[100005];bool check(int x)//大于m return 1;{int ans = 1;int tot = 0;for (int i=1;i<=n;i++){if (tot+a[i]<=x) tot+=a[i];else {ans++; tot = a[i];}}if (ans>m) return 1;else return 0;}int main(){scanf("%d%d", &n ,&m);int l = 0,r = 0;for (int i=1;i<=n;i++) {scanf("%d", &a[i]); r+=a[i]; l = max(l, a[i]);}while (l<r){int mid = (l+r)/2;if (check(mid)) l = mid+1;else r = mid;}printf("%d\n", l);    return 0;}


0 0
原创粉丝点击