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天里面花费最多的那一天 如 ...
随机推荐
- 【网摘】JS 或 jQuery 获取当前页面的 URL 信息
1.设置或获取对象指定的文件名或路径. window.location.pathname 2.设置或获取整个 URL 为字符串. window.location.href 3.设置或获取与 URL 关 ...
- ha-wordy-Write-up
信息收集 下载地址:点我 bilibili:点我 ➜ ~ nmap -sn 192.168.116.1/24 Starting Nmap 7.80 ( https://nmap.org ) at 20 ...
- 每个 JavaScript 开发者都该懂的 Unicode
英文原文标题:what-every-javascript-developer-should-know-about-unicode (译者注:本文含有Unicode辅助平面的特殊字符,部分浏览器可能无法 ...
- 第九届蓝桥杯B组决赛 调手表(完全背包)
问题描述 M78 星云的一个小时有 n 分钟. 大家都知道,手表只有一个按钮可以把当前的数加一.在调分钟的时候,如果当前显示的数是 0 ,那么按一下按钮就会变成 1,再按一次变成 2 .如果当前的数是 ...
- Mac. 修改bash_file
https://www.cnblogs.com/mokey/p/3542389.html
- python爬虫(六) Cookie
什么是Cookie 在网站中,http的请求通常是无状态的(第一个和服务器连接并且登录之后,此时服务器知道是哪个用户,但是当第二次请求服务器时,服务器依然不知道当前请求的是哪个用户),cookie就是 ...
- spring mvc注解之@RequestBody和@RequestParm
@RequestBody http://localhost:8080/user/login { "username":"jack", "passwar ...
- 第1节 Scala基础语法:14、15、list集合练习
package cn.itcast.collect /** *作业题 */object ListTest { def main(args: Array[String]): Unit = { //创建一 ...
- Windows密码安全性测试
一.本地管理员密码如何直接提取 1.1直接通过mimikatz读取管理员密码 (不能交互式,不能在webshell下用,图形化界面很好用) 第一条:privilege::debug ...
- Hive的存储和MapReduce处理——数据清洗(Part3)
日期:2019.11.17 博客期:118 星期日 这几天在写程序的时候虚拟机崩了,无语~所以重新从最初的状态开始配环境,重新整理之前的所有代码程序.