码迷,mamicode.com
首页 > 其他好文 > 详细

【例题 8-10 UVA - 714】 Copying Books

时间:2018-01-03 21:10:17      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:eve   ons   als   cout   put   code   gpo   最小值   source   

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


二分最后的最大值的最小值。
得到ans
然后从后往前尽量划分。
如果发现不够分成k个。
那么就从第一个开始接着分restk个(每隔1个分1块
中间遇到之前分了的就直接跳过

【代码】

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
    8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
    二分最后的最大值的最小值。
    得到ans
    然后从后往前尽量划分。
    如果发现不够分成k个。
    那么就从第一个开始接着分restk个(每隔1个分1块
    中间遇到之前分了的就直接跳过
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 500;

int n,k,a[N+10];
bool tag[N+10];

bool ok(ll up){
    ll now = 0;
    int times = 1;
    for (int i = 1;i <= n;i++)
        if (a[i]>up) return false;
            else
                {
                    now+=a[i];
                    if (now > up){
                        now = a[i];
                        times++;
                    }
                }
    return times<=k;
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        memset(tag,0,sizeof tag);
        cin >> n >> k;
        for (int i = 1;i <= n;i++) cin >> a[i];
        ll l = 0,r = 50e8,temp = -1;
        while (l <= r){
            ll mid = (l+r)>>1;
            if (ok(mid)){
                temp = mid;
                r = mid-1;
            }else l = mid + 1;
        }
        k--;
        ll now = a[n];
        for (int i = n-1;k>0 && i >= 1;i--){
            now += a[i];
            if (now > temp){
                k--;
                now = a[i];
                tag[i] = 1;
            }
        }
        if (k > 0){
            for (int i = 1;k>0 && i <= n;i++)
                if (!tag[i]){
                    tag[i] = 1;
                    k--;
                }
        }
        for (int i = 1;i <= n;i++)
        {
            cout << a[i];
            if (tag[i]) cout <<" /";
            if (i!=n) cout << ' ';else cout << endl;
        }
    }
    return 0;
}

【例题 8-10 UVA - 714】 Copying Books

标签:eve   ons   als   cout   put   code   gpo   最小值   source   

原文地址:https://www.cnblogs.com/AWCXV/p/8185301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!