POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 19207 | Accepted: 7630 |
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over
the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ's goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5
100
400
300
100
500
101
400
Sample Output
500
Hint
题意是给出一系列数,将这些数按顺序分成M组,求每组和的最小值是多少。
枚举答案,left是一个数一个组,即最大值。right是所有数的和。
每次去和M比较来判断mid是多是少。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int N,M;
int value[100000]; int solve(int mid)
{
int i,sum=0,result=1;
if(mid==501)
i=1;
for(i=1;i<=N;i++)
{
if(value[i]+sum<=mid)
{
sum += value[i];
}
else
{
sum=value[i];
result++;
}
}
if(result>M)
{
return 1;
}
else
return -1;
} int main()
{
int i,sum,max_x;
cin>>N>>M; sum=0;
max_x=0;
for(i=1;i<=N;i++)
{
cin>>value[i];
sum += value[i];
max_x=max(max_x,value[i]);
} int mid=(sum+max_x)/2;
while(max_x<sum)
{
int temp=solve(mid);
if(solve(mid)==-1)
{
sum = mid-1;
}
else
{
max_x = mid+1;
}
mid = (max_x+sum)/2;
} cout<<mid<<endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊的更多相关文章
- 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(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- 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 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...
随机推荐
- linux磁盘空间挂载
(1)查看磁盘空间 df -hl (3)查看硬盘及分区信息 fdisk -l (4)格式化新分区 mkfs.ext3 /dev/xvdb (5)将磁盘挂载在/www/wwwroot/default目录 ...
- [PHP]PHP中申明 declare(strict_types=1)的作用
strict_types=1 针对参数类型开启严格模式,进行数据类型检验,默认是弱类型校验哪个文件写了declare,哪个文件中的所有代码就需要检查 declare(strict_types=1); ...
- IDEA 解决 Maven 依赖冲突的高能神器,这一篇够不够?
1.何为依赖冲突 Maven是个很好用的依赖管理工具,但是再好的东西也不是完美的.Maven的依赖机制会导致Jar包的冲突.举个例子,现在你的项目中,使用了两个Jar包,分别是A和B.现在A需要依 ...
- js中的原生Ajax和JQuery中的Ajax
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). js中的Ajax: 参数介绍: open(String method,Str ...
- 6 JavaScript函数&内置构造&函数提升&函数对象&箭头函数&函数参数&参数的值传递与对象传递
JavaScript函数:使用关键字function定义,也可以使用内置的JavaScript函数构造器定义 匿名函数: 函数表达式可以存储在变量中,并且该变量也可以作为函数使用. 实际上是匿名函数. ...
- Python数据类型-1 数据类型介绍
数据类型 在python这门语言中,数据类型分为两种. 内置的和自定义的. 内置的包括数字.字符串.布尔.列表.元组.字典.Bytes.集合这些常用的以及一些不太常用的数据类型.而自定义的,一般以类的 ...
- Python环境搭建-2 编译器和解释器
编译器与解释器 编译器/解释器:高级语言与机器之间的翻译官 都是将代码翻译成机器可以执行的二进制机器码,只不过在运行原理和翻译过程有不同而已. 那么两者有什么区别呢? 编译器:先整体编译再执行 解释器 ...
- luogu P3356 火星探险问题
本题很简单的费用流问题,有石头的点需要限制,那我们就可以拆点,capacity为1就可以限制,然后cost为-1,直接跑板子就可以了,注意输出的时候找残量网络的反向边
- Django 学习组件分页器与自定制分页器
一.Django 分页器 1.django的分页器基础版 (1)首先是基础数据分别为 from django.db import models # Create your models here. c ...
- IOS 错误原因
当xcode提示以下错误时,很可能的原因是由于ViewController中的View在Controller中连接了outlet,然后又删除了Controller中对应的属性,导致xcode找不到这个 ...