POJ 3273 Monthly Expense(二分)
来源:互联网 发布:中国矿工软件 编辑:程序博客网 时间:2024/06/04 18:08
题目链接:点击打开链接
题目大意是说给n个数,要求分成m组,必须连续的数才能合并成一个组,求满足ans大于等于每一组的和的最小ans(每个组可以只有1个数)
显然二分查找最小的ans
//Must so#include<iostream>#include<algorithm>#include<string>#include<sstream>#include<queue>#include<vector>#include<set>#include<map>#include<cstring>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define sqrt(n) sqrt((double)n)#define pow(a,b) pow((double)a,(int)b)#define inf (1<<29)#define NN 100005using namespace std;const double PI = acos(-1.0);typedef long long LL;//一样的思想,暴力的遍历每一个可能的答案,check是否满足,找到满足的最小的,于是二分int n,m;//n个数分m组int a[NN];bool ok(int ans)//判断答案能否分m组{ //........分组连续......贪心,只要连续的加起来没有超过ans就可以分到一个组 int k = 1;//傻了... for (int i = 0,sun = 0;i < n;i++) { if (sun + a[i] <= ans) sun += a[i]; else { k++; if (k > m) return 0;//剪枝 sun = a[i]; } } return k <= m;//能分6组的话分7组是绰绰有余滴,小于m组的也可以分成m组}int main(){ while (~scanf("%d%d",&n,&m)) { int l = 0,r = 0; for (int i = 0;i < n;i++) { scanf("%d",a+i); r += a[i]; l = max(l,a[i]); } int mid = (l+r)>>1; while (l < r) { if (ok(mid))//OK只说明mid满足 但是不一定最小 r = mid - 1;//尝试更小的mid else l = mid + 1;//不满足就尝试更大的mid mid = (l+r)>>1; } cout<<mid<<endl; } return 0;}
0 0
- 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 【二分】
- poj 3273 Monthly Expense (二分)
- POJ 3273 Monthly Expense 二分
- POJ 3273 Monthly Expense(二分)
- 【poj 3273】 Monthly Expense 二分
- POJ - 3273 Monthly Expense(二分)
- POJ-3273 Monthly Expense(二分)
- android基础回顾(三)基础UI组件
- Android Fragment完全解析
- cf#345-D - Image Preview-二分
- 面试经典题库:常见http状态码及意义
- C++中数据类型字节数和机器字长关系
- POJ 3273 Monthly Expense(二分)
- 同一设置UINavigationBar
- php有用链接
- 好男人不能淫
- ARC下NSException有可能会引起内存泄露
- intent和intentfilter
- dubbo工作原理,集群容错,负载均衡
- iOS-UIDynamic物理仿真-捕捉-UISnapBehavior
- String 构造函数,析构函数,拷贝构造函数和赋值函数