POJ3273-Monthly Expense (最小化最大值)
题目链接: cid=80117#problem/E">click here~~
【题目大意】
农夫JF在n天中每天的花费,要求把这n天分作m组。每组的天数必定是连续的。要求分得各组的花费之和应该尽可能地小,最后输出各组花费之和中的最大值
【解题思路】:
经典的最小化最大值问题,要求连续的m个子序列,子序列的和最大值的最小,枚举满足条件的m的最小值即为答案。因此二分查找。
1.能否把序列划分为每一个序列之和不大于mid的m个子序列,
2.通过用当前的mid值能把天数分成几组,
3.比較mid和t的大小,从而确定mid
详细见代码吧:
//#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=1e5+10;
int n,m,num[N];
bool is_part(int mid)
{
int t=0,s=0; //t==当前mid值能把n天分成的组数(初始把所有天数作为0组)
bool ok=true;
for(int i=0; i<n; i++)//遍历每一天的花费
{
if(num[i]>mid){//假设某个值大于,显然不符合
ok=false;
break;
}
if(s+num[i]>mid){//不能再把当前元素加上了
t++;
s=num[i]; //把前i天作为一组
if(t>m-1){ //t=m时退出。即在最后一个元素之前都已经用了m条划分线
ok=false;
break;
}
}
else s+=num[i];//把当前元素与前面的元素加上。以便尽量往右划分,贪心究竟
}
return ok;
}
void judge() //二分查找
{
int ll=0,rr=1e8;
while(ll<rr)
{
int mid=(ll+rr)>>1;
if(is_part(mid)) rr=mid;
else ll=mid+1;
}
printf("%d\n",ll);
}
int main()
{
int maxx=0,sum=0;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++){
scanf("%d",&num[i]);
sum+=num[i];
maxx=max(maxx,num[i]);
}
if(m==n||m==1) printf("%d\n",maxx);
else judge();
return 0;
}
POJ3273-Monthly Expense (最小化最大值)的更多相关文章
- POJ-3273 Monthly Expense---最小化最大值
题目链接: https://cn.vjudge.net/problem/POJ-3273 题目大意: 给N个数,划分为M个块(不得打乱数顺序).找到一个最好的划分方式,使得块的和的最大值 最小 解题思 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- Monthly Expense(二分--最小化最大值)
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- OJ 21658::Monthly Expense(二分搜索+最小化最大值)
Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...
- POJ_3273_Monthly_Expense_(二分,最小化最大值)
描述 http://poj.org/problem?id=3273 共n个月,给出每个月的开销.将n个月划分成m个时间段,求m个时间段中开销最大的时间段的最小开销值. Monthly Expense ...
- POJ_3104_Drying_(二分,最小化最大值)
描述 http://poj.org/problem?id=3104 n件衣服,第i件衣服里面有水a[i],自然风干每分钟干1个水,用吹风机每分钟干k个水,但是同时只能对一件衣服使用吹风机,求干完所有衣 ...
- POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
随机推荐
- Shipping Transactions > Error: The action can not be performed because the selected records could not be locked.
Shipping Transactions > Action: Launch Pick Release (B: Go) Error: The action can not be performe ...
- Java数据库增删改查
数据库为MySQL数据库,Oracle数据库类似: create database db_test;--创建数据库 ';--创建用户 grant all privileges on db_test.* ...
- bzoj1426
偷个懒,转自hzwer [“这种煞笔题怎么总有人问”,被吧主D了... 用f[i]表示已经拥有了i张邮票,则期望还需要购买的邮票数 则f[n]=0 f[i]=f[i]*(i/n)+f[i+1]*((n ...
- UVa 1451 (数形结合 单调栈) Average
题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...
- bzoj1015:1015: [JSOI2008]星球大战starwar
应该是全部读入之后再添加边用并查集就可以了. yyl用空间换时间.u[]v[]等将边预存起来. #include<cstdio> #include<cstring> #incl ...
- mysql中增加某一时间段内的时间数据(包含:时间、年、月、日、第几周、季度)
创建表dim_date: create table `dim_date` ( `year` int (20), `month` int (20), `day` int (20), `week` int ...
- class属性添加多个类
<html> <head> <style type="text/css"> h1.intro { color:blue; text-align: ...
- windows版本git的下载地址
最后编辑时间 2016年09月01日13:13 首先需要下载msysgit,下载最新版本即可 https://git-for-windows.github.io/ 这个是源代码 https://git ...
- java 多线程下载
import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 把一 ...
- VTL说明文档
关于这个指南 这个指南是针对Velocity模版语言(VTL)的说明.更多其它的信息,请参考Velocity用户指南(http://velocity.apache.org/engine/release ...