POJ 3273 Monthly Expense【二分答案】
来源:互联网 发布:骂淘宝客服有什么后果 编辑:程序博客网 时间:2024/06/08 17:53
题意:输入n,m,接着输入接下来n天的花费,为这些天数分组,求每组的最少花费【限制组数不能大于m】
二分目标钱数,再模拟分组,组数太少就是钱不够,+钱,反之减钱
赶脚理解题意比做题还难,轻松1Y
//79MS#include <iostream>#include <cstdio>#include <cmath>using namespace std;int m,n;int a[100010];int too(int mid){ int cnt = 0, res = 1; for(int i=0;i<n;i++) { if( a[i] > mid)//钱不够用,肯定不行,要增钱,l=mid+1; return 1; if( cnt + a[i] <= mid ) { //模拟分组 cnt += a[i]; }else { cnt = a[i]; res++; } } return res > m;}int main(){ // freopen("in.txt","r",stdin); int r,l,mid; while(scanf("%d%d",&n,&m)!=EOF) { int sum = 0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); sum+=a[i]; } l = 0,r = sum; while( l < r) { mid = (l + r)/2; if(too(mid)) l = mid+1; else r = mid ; } printf("%d\n",l); } return 0;}
三组数据
2 1191 1219 19101079812559395106474输出10,2,123
0 0
- POJ 3273 Monthly Expense【二分答案】
- POJ 3273 Monthly Expense (二分答案)
- POJ 3273 Monthly Expense(二分答案)
- poj 3273/1064/2456 二分答案(Monthly Expense)
- POJ 3273 USACO 2007 Mar Monthly Expense 二分答案
- Monthly Expense+POJ+典型的二分答案
- poj 3273 二分 Monthly Expense
- poj 3273 二分 monthly expense
- poj-3273-Monthly Expense-二分
- POJ 3273 Monthly Expense(二分)
- POJ 3273 Monthly Expense(二分)
- poj 3273 Monthly Expense (二分)
- POJ 3273 Monthly Expense (二分)
- POJ - 3273 Monthly Expense 二分
- POJ 3273 Monthly Expense(二分)
- POJ 3273:Monthly Expense(二分)
- 【POJ 3273】 Monthly Expense (二分)
- poj 3273 Monthly Expense 【二分】
- LeetCode OJ - Climbing Stairs
- java swing JFileChooser 锁定指定目录
- Swift研究之编程高级教程(二)属性,存储属性详解
- Eclipse保存时删除每行尾随空格
- pexpect模块
- POJ 3273 Monthly Expense【二分答案】
- HDU 1166 敌兵布阵 线段树
- urllib模块和requests模块
- 1023. Have Fun with Numbers (20)
- 鼻部整形各种手术方法
- MySQLdb模块
- 郭斯特微博评论中的一句话
- Struts2 注解配置 (struts.convention-plugn) struts.xml
- 括号配对问题