poj 3273
来源:互联网 发布:安装阿里旺旺mac版 编辑:程序博客网 时间:2024/06/08 04:33
题目描述:
n天分为m组,要求这m组尽可能每组钱数之和都很小,求所有分组方式里钱数之和最大的钱数。
题目类别:
二分法应用。通过指定最大钱数之和判断组数是否符合要求,如果不符合,根据条件对low 和 high 进行赋值。
源代码:
#include<iostream>#include<stdio.h>using namespace std;int n; //天数int m; //组数 bool Judge(int mid,int money[]) //判断当前组数是多还是少{int i;int sum=0;int count=1;for(i=1;i<=n;i++){if(sum+money[i]<=mid) //钱数之和小于mid的可以算是一组内的{sum=sum+money[i];}else{sum=money[i]; //钱数之和大于mid要分两组count++;}}if(count>m)return false; //组分多了,说明mid 较小elsereturn true;}int main(){int i;int low,high,mid;while(scanf("%d%d",&n,&m)!=EOF){high=0;low=0;int *money=new int [n+1];for(i=1;i<=n;i++){scanf("%d",&money[i]);high=high+money[i]; //上限是只分一组,即所有的数之和if(money[i]>low) low=money[i]; //下限是分为n组,每天一组,取最大的 }mid=(low+high)/2;while(low<high){if(Judge(mid,money)==false) //mid值较小low=mid+1;elsehigh=mid-1;mid=(high+low)/2;}cout<<mid<<endl;delete money;}return 0;}
此题是二分法的另一种应用,以后类似题目皆可采用。
- POJ 3273
- poj 3273
- poj 3273
- Poj 3273
- POJ 3273
- POJ-3273
- poj 3273
- POJ 3273
- poj 3273
- poj 3273
- POJ 3273
- POJ 3273
- 【POJ】3273
- poj 3273 Monthly Expense
- poj 3273 (二分)
- poj 3273(二分。。。很水。。。。)
- poj-3273 Monthly Expense
- poj 3273 二分法
- test code read xml
- UniCode 下char*转CString ,利用MultiByteToWideChar进行转换,中文乱码的解决
- Linux进程间通讯之消息队列
- 本人二手图书资源淘宝店已经开张,欢迎选购
- 关于NS2多接口多信道扩展方案
- poj 3273
- liunx 命令 之 mkdir 与 touch
- [INS-08106] Unexpected error occurred while loading the view 'GridPlugPlayInfoUI'
- libgdx 的 FreeTypeFontGenerator在android项目下的使用
- arcgis ios 10.0.1 sdk 中比例尺 scale 和服务器不一致的问题。
- openfiler+oracle linux enterprise 6.2+oracle11gR2安装手册
- 大一总结
- OSM 软件分析
- windows solaris 修改 路由 命令