UVa 714 - Copying Books(二分查找)
2015-07-25 19:07
831 查看
在挑战程序设计竞赛上做过类似的,二分答案判断是否合理。
#include <cstdio> #include <stack> using namespace std; typedef long long LL; const int maxn = 510; LL a[maxn]; stack <LL> p[maxn]; int m,k; bool ok(LL ans) { int pos=m,t=k; while(t--){ LL cur=ans; while(cur>a[pos]){ cur-=a[pos--]; if(!pos) return true; } } return !pos; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d", &m, &k); LL l = 0, r = 6000000000; for(int i = 1; i <= m; ++i) scanf("%lld", &a[i]), l = max(l, a[i]); LL mid = (l + r) / 2; while(l < r) { mid = l + (r - l) / 2; if(ok(mid)){ if(!ok(mid - 1)){--mid; break;} else r = mid + 1; } else l = mid; } if(ok(l)) mid = l; int pos = m; for(int i = k; i > 0; --i){ while(!p[i].empty()) p[i].pop(); LL t = mid; while(t >= a[pos] && pos) { t-=a[pos], p[i].push(a[pos--]); if(pos < i) break; } } for(int i = 1; i <= k; ++i) { while(!p[i].empty()){ printf("%lld", p[i].top()), p[i].pop(); if(!p[i].empty()) printf(" "); } printf(i == k ? "\n" : " / "); } } return 0; }
相关文章推荐
- 基于Linux的智能家居的设计(4)
- Linux引导:历史和未来
- linux设备驱动那点事儿之平台设备理论篇
- Linux lvm 管理
- Apache基础
- 二分搜索 POJ 3258 River Hopscotch
- linux_shell_轮询触发启动脚本
- Vuforia开发技巧三-如何调整VideoPlayback中video的尺寸
- Openstack Tokyo Summit投票
- PHP如何解决网站大流量与高并发
- nginx 安装 php
- Linux 中多个namespace的情况下,查看对应的namespace下面的路由信息
- linux常用命令大全(转)
- opencv像素基本操作及图像遍历at
- 浅谈tomcat的配置及数据库连接池的配置
- linux系统编程:线程同步-互斥量(mutex)
- LINK : fatal error LNK1104: cannot open file "mfc42u.lib"
- ubuntu上创建账户和samba用户
- 深入理解Linux内核-定时测量
- Linux运维常用命令之sed