Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11196 | Accepted: 4587 |
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 题意简化后的意思就是给出N个数,将这N个数分成M份,每份必须是连续的一个或几个数,要求分的各份的数之和尽可能小,求出M份和中的最大值;
注意,也许在low == high 之前已分成m组,但可能不是最优的,当while(low < high)结束后得到的low肯定是最优结果;
#include<stdio.h>
#include<string.h>
const int N = ;
int n,m;//把n个数分成m组;
int money[N+];
//计算当前mid值能把n个数分成的组数并与m比较;
bool judge(int mid)
{
int sum = money[];
int group = ; for(int i = ; i < n; i++)
{
if(sum + money[i] <= mid)
{
sum += money[i];
}
else
{
group++;
sum = money[i];
}
} if(group > m)
return false;
else return true;
} int main()
{
int low = ,high = ;//low为下界,high为上界;
int mid;
scanf("%d %d",&n,&m);
for(int i = ; i < n; i++)
{
scanf("%d",&money[i]);
high += money[i];//high初始化为n个数的和,相当于把n个数分成一组;
if(low < money[i])
low = money[i];//low初始化为n个数中的最大值,相当于把n个数分成n组;
} mid = (low+high)/;
while(low < high)
{
if(!judge(mid))//如果mid把n个数分得的组数大于m,说明mid偏小,更新low;
{
low = mid+;
}
else high = mid-;//否则说明mid偏大,更新high;
mid = (low+high)/;
}
printf("%d\n",mid);
return ;
}
Monthly Expense(二分)的更多相关文章
-
【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
-
POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
-
Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
-
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 ...
-
POJ3273 Monthly Expense —— 二分
题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Tota ...
-
POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19207 Accepted: 7630 ...
-
POJ3273:Monthly Expense(二分)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
-
POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
随机推荐
-
【WP开发】不同客户端之间传输加密数据
在上一篇文章中,曾说好本次将提供一个客户端之间传输加密数据的例子.前些天就打算写了,只是因一些人类科技无法预知的事情发生,故拖到今天. 本示例没什么技术含量,也没什么亮点,Bug林立,只不过提供给有需 ...
-
Microsoft Azure 云存储服务概念
本文包括了以下几点内容: 什么是Azure云存储服务? 云存储服务分类 云存储服务的优势 什么是Azure云存储服务? Azure 云存储服务可以说是Azure 上最重要的SAAS服务了. 在Azur ...
-
Topcoder SRM 683 Div2 - C
树形Dp的题,根据题意建树. DP[i][0] 表示以i为根节点的树的包含i的时候的所有状态点数的总和 Dp[i][1] 表示包含i结点的状态数目 对于一个子节点v Dp[i][0] = (Dp[v] ...
-
C++嵌入Python,以及两者混用
以前项目中是C++嵌入Python,开发起来很便利,逻辑业务可以放到python中进行开发,容易修改,以及功能扩展.不过自己没有详细的研究过C++嵌入python的细节,这次详细的研究一下.首先我们简 ...
-
IDEA打war包
一 第一步创建一个web application:expolded 选择当前项目 二 新建一个web application: Archive 选择刚刚新建的Expoded “For ‘...... ...
-
asp.net中分页与存储过程的一些总结
一.接上文,使用的是jquery AJAX 进行分页 分页存储过程代码如下: ALTER PROCEDURE [dbo].[USP_GetAlbumByPage] @pageIndex int,--当 ...
-
《JavaScript网页特效经典300例-高级篇》
<Javascript网页经典特性300例> 高级篇 第18章:ajax应用 Ajax传输JSON数据实例定义一套自己的Ajax框架 第19章:面向对象的特性 定义一个类利用prototy ...
-
Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
(1)下边的一个Java类包含了所有Map.Set.List.数组.属性集合等这些容器,主要用于演示spring的注入配置: package com.lc.collection; import jav ...
-
kafka 重新分配partition
登陆kafka manager 进入相关topic 点击generate partition assignments 点击reassign partirons
-
神经网路-SGD-1
SGD神经网络以及python中实现 1.SGD(stochastic gradient descend):<1>数据抽取:<2>计算梯度;<3>参数更新:< ...