POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和
看题目看了好久不懂题意,最后还是看了题解
二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天
如果mid>=m,说明mid偏小,l=mid+1,
如果mid<m,说明mid偏大,r=mid,
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int a[maxn];
int n,m,k; int ok(int v){
int ans=;
int cnt=;
for(int i=;i<=n;i++){
ans+=a[i];
if(ans>v){
ans=a[i];
cnt++;
}
}
if(cnt>=m) return ;
return ;
} int main(){
while(scanf("%d %d",&n,&m)!=EOF){
int minn=-,maxx=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
maxx+=a[i];
minn=max(minn,a[i]);
} LL l=minn,r=maxx,mid;
while(l<r){
mid=(l+r)/;
if(ok(mid)) r=mid;
else l=mid+;
// printf("l=%d\n",l);
// printf("r=%d\n",r);
// printf("mid=%d\n",mid);
} printf("%I64d\n",l);
}
return ;
}
POJ 3273 Monthly Expense 【二分答案】的更多相关文章
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- 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 二分枚举
题目: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 (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
随机推荐
- vue 2.x axios 封装的get 和post方法
import axios from 'axios' import qs from 'qs' export class HttpService { Get(url, data) { return new ...
- Java对象、Json、Xml转换工具Jackson使用
在Java项目中將一个对象转换成一段Json格式的字符串是非常常见的,能够实现这种需求的工具包也比较多,例如Gson.JSON-lib.Jackson等等.本文主要介绍Jackson的使用,Jacks ...
- (转载)Android项目实战(三十二):圆角对话框Dialog
Android项目实战(三十二):圆角对话框Dialog 前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...
- 数据库SQL语句错误
Caused by: android.database.sqlite.SQLiteException: near "where": syntax error(Sqlite co ...
- HDU 1052 Tian Ji -- The Horse Racing【贪心在动态规划中的运用】
算法分析: 这个问题很显然可以转化成一个二分图最佳匹配的问题.把田忌的马放左边,把齐王的马放右边.田忌的马A和齐王的B之间,如果田忌的马胜,则连一条权为200的边:如果平局,则连一条权为0的边:如果输 ...
- VMware虚拟机共享文件夹
安装好虚拟文件夹后,第二次开机时发现/mnt/hgfs目录下找不到共享的文件夹,原因是vmfg-fuse服务没有开启. 在root的配置文件中添加如下代码,设置开机自启: /usr/bin/vmhgf ...
- Python 函数部分练习题
函 数 基 础 1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作2.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 3.写函数,判断用户传入的 ...
- C IO programming test code
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl ...
- vue svg的使用
项目要求: 需要把websocket推送的数据进行展示.不停地刷掉旧的数据.但是需要根据数据坐标圈出来对应的车辆. 开始使用的是canvas进行画图,思路是使用absolute定位,for循环,在图片 ...
- 变量命名规范及str类型
变量命名规范: 1.单词之间用_分开 add_num() 2.全局变量,大写 PI,NUMBER() 3.实例变量,以_开头 _example() 4.私有实例变量 __private() 5.普通函 ...