您的位置:首页 > 运维架构

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: