题目:POJ3273、洛谷P2884。

题目大意:有n个数,要分成m份,每份的和要尽可能小,求这个情况下和最大的一份的和。

解题思路:二分答案,对每个答案进行贪心判断,如果最后得出份数>m,则说明答案在[mid+1,r]内,反之在[l,mid]内。我的二分上界是所有数的总和,所以时间复杂度$O(n\log (\sum_{i=1}^{n}a[i]))$。

结果我贪心没考虑全,爆了一发O(≧口≦)O

C++ Code:

#include<cstdio>
using namespace std;
int n,m,a[100005];
long long s;
bool ok(int x){
int p=1;
long long ns=0;
for(int i=1;i<=n;++i){
if(ns+a[i]<=x)ns+=a[i];
else{
++p;
ns=a[i];
if(ns>x)return false;
}
}
return p<=m;
}
int main(){
scanf("%d%d",&n,&m);
s=0;
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
s+=a[i];
}
long long l=0,r=s,ans;
while(l<=r){
long long mid=l+r>>1;
if(ok(mid)){
ans=mid;
r=mid-1;
}else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}

[USACO07MAR]每月的费用Monthly Expense的更多相关文章

  1. bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense

    P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...

  2. 洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense

    https://www.luogu.org/problemnew/show/P2884 题目描述 Farmer John is an astounding accounting wizard and ...

  3. P2884 [USACO07MAR]每月的费用Monthly Expense

    题目描述 Farmer John is an astounding accounting wizard and has realized he might run out of money to ru ...

  4. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  5. BZOJ【1639】: [Usaco2007 Mar]Monthly Expense 月度开支

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 700  Solved: ...

  6. POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273   Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  7. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  8. Divide and Conquer:Monthly Expense(POJ 3273)

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  9. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

随机推荐

  1. JQuery中的find、filter和each方法学习

    find() 概述 搜索所有与指定表达式匹配的元素.这个函数是找出正在处理的元素的后代元素的好方法. 所有搜索都依靠jQuery表达式来完成.这个表达式可以使用CSS1-3的选择器语法来写. 参数 e ...

  2. SpringBoot(四) Web开发 --- Thymeleaf、JSP

    Spring Boot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及Spring MVC的依 ...

  3. 基本数据类型(dict)

    1.定义 dict => {"key":'value',"a":1} 字典是无序的,字典是可变的 字典的键 => 可哈希(不可变),唯一 字典的值 ...

  4. sql拼接

    with t as( select 'Charles' parent, 'William' child union select 'Charles', 'Harry' union select 'An ...

  5. 强化学习(3)-----DQN

    看这篇https://blog.csdn.net/qq_16234613/article/details/80268564 1.DQN 原因:在普通的Q-learning中,当状态和动作空间是离散且维 ...

  6. caioj 1152 快速求模 (快速幂)

    (1)开long long,不然中间结果会溢出 (2)注意一开始的初始化,保险一点. #include<cstdio> #include<cctype> #include< ...

  7. Windows和Linux的编译理解

    Windows一般编译出来的x86的软件,就是只能在x86的系统上才能运行,同理,在x64系统上也是一样的道理. Linux利用gcc编译器编译,可以在Linux上面运行,但是想要在嵌入式系统上运行的 ...

  8. POJ 1975 Median Weight Bead

    Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...

  9. mysql同步复制报Slave can not handle replication events with the checksum that master 错误

    slave服务器,查看状态时,发现下面的错误: Last_IO_Error: Got fatal error 1236 from master when reading data from binar ...

  10. ASP.NET-缓存outputcache参数

    给Index加一个60秒的缓存,应该缓存在IIS服务器里面(我猜的) 只对变化的参数page不进行缓存,其他参数返回相同的内容 根据接受的语言的不同不进行缓存 设定缓存的位置 依赖于数据库变化的缓存 ...