uva714 Copying Books

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=1<<29;

int n,k;
ll a[maxn];
int ans[maxn],tmp[maxn];
ll Max,sum;

bool check(ll m)
{
    ll s=0;
    MS0(tmp);
    int cnt=1;
    for(int i=n;i>=1;i--){
        s+=a[i];
        if(s>m){
            s=a[i];
            tmp[i]=1;
            cnt++;
        }
    }
    if(cnt<=k){
        int cur=1;
        while(cnt<k){
            if(!tmp[cur]) tmp[cur]=1,cnt++;
            cur++;
        }
        return 1;
    }
    return 0;
}

void bin(ll l,ll r)
{
    ll res=r;
    MS0(tmp);
    while(l<r){
        ll m=(l+r)>>1;
        if(check(m)) memcpy(ans,tmp,sizeof(tmp)),r=m;
        else l=m+1;
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    int T;cin>>T;
    while(T--){
        scanf("%d%d",&n,&k);
        sum=0;Max=-INF;
        REP(i,1,n) scanf("%d",&a[i]),Max=max(a[i],Max),sum+=a[i];
        MS0(ans);
        bin(Max,sum);
        REP(i,1,n){
            printf("%d",a[i]);
            if(ans[i]) printf(" / ");
            else printf("%s",i==n?"\n":" ");
        }
    }
    return 0;
}
/**
直接二分检查就可以了,注意答案的优先顺序。
*/

时间: 2024-08-25 13:38:12

uva714 Copying Books的相关文章

POJ1505&amp;&amp;UVa714 Copying Books(DP)

Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scri

uva714 - Copying Books(最大值最小化)

题目:uva714 - Copying Books(最大值最小化) 题目大意:给出n本书,每本书的值代表这本书的页数.然后给定m个scribers,每个scriber至少要抄一本书,或者连续的几本书.每个scriber的工作量就等于他要抄的书的页数之和.问怎样划分能使的scribers中工作量的最大值最小.这里要求答案如果有多种的话就输出前面的和比较小的那个划分. 解题思路:最大值最小化问题. 二分尝试可能的最大值,然后如果在这个最大值的情况下可以划分的话,说明最大值可能是这个值,也可能更小.如

UVa714 Copying Books (二分法,贪心)

链接:http://vjudge.net/problem/UVA-714 分析:二分枚举最小值,用贪心的思想每段序列尽量往右划分,注意:因为要求字典序最小解,输出时还有一个贪心过程,左起S[i]尽量小,相当于右起S[j]尽量大,还有一种情况是剩下的数之和已经小于等于ans,但是此时剩余要分配的组数还有多(remain>1). 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 usin

uva 714 Copying Books (二分)

uva 714 Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its co

uva 714 - Copying Books(贪心 最大值最小化 二分)

题目描述开头一大堆屁话,我还仔细看了半天..其实就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,如果最小值相同情况下有多种分配方案,输出前面份数小的,就像字典序输出从小到大一样的意思. 这里用到贪心的方法,定义f(x)为真的条件是满足x为最大值使n本书分成k份,那么就是求x的最小值.如何确定这个x就是用的二分法,x一定大于0小于所有值的合,不断的二分再判断是否成立,成立就取左半边,不成立说明太小了就取右半边,写的时候还是没有把二分法理解透彻,我还怕会丢失

poj 1505 Copying Books

http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS   Memory Limit: 10000K Total Submissions: 7053   Accepted: 2200 Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be r

UVa 714 Copying Books(贪心 二分)

题意  把m数分成k组  使每组数的和的最大值最小  如果有多种分法 靠前的组的和尽量小 关键是找出那个最小的最大值   可以通过二分来找出  开始左端点为m个数中最大的数  右端点为m个数的和  若中点能将m个数分为小于等于k组  比它大的肯定都是可以的  中点变为右端点   否则中点变成左端点 然后就可以贪心逆向模拟了  从后往前每组选择尽量多的数直到剩下的数等于组数 #include <bits/stdc++.h> using namespace std; typedef long lo

POJ 1505 (Copying Books)(经典二分)

Description Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of

UVa 714 - Copying Books 二分答案

题目链接:714 - Copying Books 解题思路 具体处理方法见代码 /************************************************************** Problem: User: youmi Language: C++ Result: Accepted Time: Memory: ****************************************************************/ //#pragma comm