POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和
看题目看了好久不懂题意,最后还是看了题解
二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天
如果mid>=m,说明mid偏小,l=mid+1,
如果mid<m,说明mid偏大,r=mid,
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int a[maxn];
int n,m,k; int ok(int v){
int ans=;
int cnt=;
for(int i=;i<=n;i++){
ans+=a[i];
if(ans>v){
ans=a[i];
cnt++;
}
}
if(cnt>=m) return ;
return ;
} int main(){
while(scanf("%d %d",&n,&m)!=EOF){
int minn=-,maxx=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
maxx+=a[i];
minn=max(minn,a[i]);
} LL l=minn,r=maxx,mid;
while(l<r){
mid=(l+r)/;
if(ok(mid)) r=mid;
else l=mid+;
// printf("l=%d\n",l);
// printf("r=%d\n",r);
// printf("mid=%d\n",mid);
} printf("%I64d\n",l);
}
return ;
}
POJ 3273 Monthly Expense 【二分答案】的更多相关文章
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分)
//最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
随机推荐
- 远程桌面连接Windows Azure中的Ubuntu虚拟机
默认情况下,通过Windows Azure创建的ubuntu虚拟机是不能直接连接远程桌面的,只能通过SSH终端连接. 在Windows Azure Portal中创建Ubuntu虚拟机,创建完成后添加 ...
- 蓝牙音箱BluetoothA2dp
package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...
- 浅谈AVL树,红黑树,B树,B+树原理及应用
背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上说mysql的存储引擎InnoDB采用的索引类型是B+Tree,那么,大家有没有产生这样一个疑问,对于数据索引,为什么要使 ...
- spring使用注解开发
1.准备工作(1)导入common-annotations.jar(2)导入schema文件 文件名为spring-context-2.5.xsd(3)在xml的beans节点中配置 service层 ...
- CF992E Nastya and King-Shamans_线段树
Code: #include<cstdio> #include<algorithm> using namespace std; const int maxn = 200000 ...
- tcpsock for Golang
前记:本文所述的 tcpsock 库托管在 Github. Golang 中的 net 标准库已对 TCP 网络编程作了简洁(却很不简单)的封装,基本上,可直接通过引用其提供的相关接口开发简易的网络应 ...
- Mysql干货收集
mysql优化:https://www.cnblogs.com/duanxz/tag/mysql/default.html?page=1
- 原生JS中 callback,promise,generator,async-await 的简介
callback,promise,generator,async-await 的简介 javascript异步的发展历程. ES6 以前: 回调函数(callback):nodejs express ...
- 【codeforces 234F】Fence
[题目链接]:http://codeforces.com/problemset/problem/234/F [题意] 你有n块板要凃油漆; 然后每块板有高度h[i];(宽度都为1) 然后每块板只能凃同 ...
- java源码之HashSet
1,HashSet介绍 1)HashSet 是一个没有重复元素的集合.2)它是由HashMap实现的,不保证元素的顺序,而且HashSet允许使用 null 元素.3)HashSet是非同步的.如果多 ...