LightOJ 1076 - Get the Containers
2016-01-24 13:00
555 查看
题意:
有n个输奶管,内有一定牛奶,用m个存储罐装这些牛奶,不能分开装,多个管内的奶可以一起装,但一起是需按照顺序装,求满足的n个罐子中的最小容积。
思路:
确定号区间 的左右两边值后,二分搜索该区间内的值,满足则缩小范围,不满足则扩大范围,直到确定出最小值。
代码:
有n个输奶管,内有一定牛奶,用m个存储罐装这些牛奶,不能分开装,多个管内的奶可以一起装,但一起是需按照顺序装,求满足的n个罐子中的最小容积。
思路:
确定号区间 的左右两边值后,二分搜索该区间内的值,满足则缩小范围,不满足则扩大范围,直到确定出最小值。
代码:
#include <stdio.h> int num[1500]; int n, m; int judge(int x) { int sum = 0, count = 1; for (int i = 0; i < n; i++) { sum += num[i]; if (sum > x) sum = num[i], count++; } return count > m?1:0; } int main() { int Case,index; scanf("%d", &Case); while (Case--) { scanf("%d %d", &n, &m); int l = 0, r = 0; for (int i = 0; i < n; i++) { scanf("%d", &num[i]); if (num[i] > l) l = num[i]; r += num[i]; } while (r > l) { int mid = (r + l) / 2; if (judge(mid)) l = mid + 1; else r = mid; } printf("Case %d: %d\n", ++index, r); } return 0; }
相关文章推荐
- DP------Climbing Stairs
- HDU 1039 Easier Done Than Said?
- YTU 1011: Rails
- 深度理解链式前向星 http://blog.csdn.net/acdreamers/article/details/16902023
- teradata EXPLAIN 读懂关键字--读懂执行计划
- scala trait 另一种用法类混入
- 如何在.net4.0中使用.net4.5的async/await实现异步
- NSSearchPathDomainMask 详解
- JetBrains 系列软件
- Job for httpd.service failed. See 'systemctl status httpd.service'
- leetcode之Contains Duplicate II
- wait&while
- leetcode之 Factorial Trailing Zeroes
- 解决【RedHat】sendmail启动慢的问题
- leetcode219. Contains Duplicate II
- 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)
- codeforces 615B Longtail Hedgehog
- Leetcode 219. Contains Duplicate II
- 本地Jdev Run PG报严重: Socket accept failed错误
- 初学Rails