[USACO07MAR]每月的费用Monthly Expense
题目: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的更多相关文章
- bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense
P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...
- 洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense
https://www.luogu.org/problemnew/show/P2884 题目描述 Farmer John is an astounding accounting wizard and ...
- P2884 [USACO07MAR]每月的费用Monthly Expense
题目描述 Farmer John is an astounding accounting wizard and has realized he might run out of money to ru ...
- POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...
- BZOJ【1639】: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 700 Solved: ...
- POJ3273 Monthly Expense —— 二分
题目链接:http://poj.org/problem?id=3273 Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Tota ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
随机推荐
- oracle 11g rac for linux add node (oracle 11g rac 节点添加)
说明: Adding Oracle RAC to Nodes with Oracle Clusterware Installed步骤来自ORACLE 官方文档: https://docs.oracle ...
- BZOJ 2194 FFT
思路: 题目中给的是差值一定的 那么就把b数组倒一下 不就变成 i+j=k(k为定值的了嘛) 嗯 然后搞个FFT //By SiriusRen #include <cstdio> #inc ...
- C语言学习小记
2013年1月31日 今天试着编程为报文去头去尾. #include #include #define MAX_LENTH 1024 int main() { char *path = &quo ...
- if switch
一.基本if结构: 1.语法:if (条件){ 代码块 } 2.执行顺序:先判断条件,条件成立则行{}内的代码,不成立则跳出if结构快既不执行{}内的代码. 3.什么情况下要用基本if选择结构:当需要 ...
- Java数据库连接——jdbc-odbc桥连接方式及汉字乱码问题
jdbc-odbc桥连接方式操作数据库SU(Course),其中Course属性有Cno,Cname,Cpno,Ccredit. 步骤: 1.配置数据源 控制面板下搜索管理工具->ODBC数据源 ...
- opengl问题
1.glCreateShader(GL_VERTEX_SHADER)失败原因: 在调用glewInit()初始化之后才能create shader;
- Eclipse schema xml提示
步骤一:确定xsd文件位置 spring-framework-3.2.0.RELEASE\schema\beans 步骤二:复制路径 步骤三:搜索“xml catalog” 步骤四:添加约束提示 ...
- MingW和cygwin的区别(转)
个人总结:读完这段文字需要5分钟 总结: MingW https://zh.wikipedia.org/wiki/MinGW Cygwin https://zh.wikipedia.org/wiki/ ...
- 【CS-4476-project 6】Deep Learning
AlexNet / VGG-F network visualized by mNeuron. Project 6: Deep LearningIntroduction to Computer Visi ...
- state.sls web.apache
[root@master01 web]# salt 'node02' state.sls web.apache node02: ---------- ID: apache-inst ...