UVA 714(p244)----Copying Books
2016-02-25 23:17
411 查看
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define debu using namespace std; const int maxn=1e7+50; typedef long long LL; int v[maxn]; LL sum,maxx; LL n,m,a[maxn],l,r,mid; int check(LL limit) { int i=0,num=1; LL sum=0; while(i<n) { LL tmp=sum+a[i]; if(tmp<=limit) sum+=a[i]; else { num++; sum=a[i]; } i++; } return num; } void solve() { l=maxx,r=sum; LL mid; while(l<r) { mid=l+(r-l)/2; if(check(mid)<=m) r=mid; else l=mid+1; } } void output() { sum=0; for(int i=n-1;i>=0;i--) { LL tmp=sum+a[i]; if(tmp>l||(i+1<m)) { sum=a[i]; v[i]=1; m--; } else sum+=a[i]; } for(int i=0;i<n-1;i++) { printf("%lld ",a[i]); if(v[i]) printf("/ "); } printf("%lld\n",a[n-1]); } int main() { #ifdef debug freopen("in.in","r",stdin); #endif // debug int t; scanf("%d",&t); while(t--) { sum=0;maxx=0; memset(v,0,sizeof(v)); scanf("%lld%lld",&n,&m); for(int i=0; i<n; i++) { scanf("%lld",&a[i]); sum+=a[i]; maxx=max(a[i],maxx); } solve(); output(); } return 0; }题目地址: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=655
相关文章推荐
- UVA 679(p148)----Dropping Balls
- Linux精讲——mv命令
- [置顶] 那些有趣的编程知识学习网站/课程
- Tomcat的安装与配置
- Exoplayer学习
- 在CentOS上MySQL数据库服务器配置方法
- MVC5 网站开发之五 展示层架构
- Linux添加头文件路径
- linux 后台开发类常见问题及知识点
- Linux 给用户添加sudo权限
- linux(ubuntu) 创建一个名为hadoop的用户和用户组
- linux中fork()函数详解
- linux内核链表
- 在windows 与Linux间实现文件传输(C++&C实现)
- Linux精讲——cp命令
- OPENCV之人眼检测
- 【linux内核学习】怎么找到要修改的驱动源码
- My架构师之路
- Linux input子系统分析
- Linux input子系统分析