zoukankan      html  css  js  c++  java
  • codevs3163 抄书问题2

    题目描述 Description

    现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比 如不能把第一、第三、第四本数给同一个人抄写。现在请你设计一种方案,使得复制时间最短。复制时间为抄写页数最多的人用去的时间。

    (本题数据范围扩大,本题支持 O(nk) 算法)

    输入描述 Input Description

    第一行两个整数M、K;(K<=1000 M<=10000  满足 k<=m)

    第二行M个整数,第i个整数表示第i本书的页数。

    输出描述 Output Description

    K行,每行两个正整数,第i行表示第i个人抄写的书的起始编号和终止编号。K行的起始编号应该从小到大排列,如果有多解,则尽可能让前面的人少抄写。

    样例输入 Sample Input

    9 3

    1 2 3 4 5 6 7 8 9

    样例输出 Sample Output

    1 5

    6 7

    8 9

    数据范围及提示 Data Size & Hint

    详见试题 

    本题支持 O(nk) 算法

    //注意最后的处理
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn = 10005;
    int m,k,a[maxn],rec[maxn],tmp[maxn],s[maxn];
    bool check(int t){
        int p = k,w = 0;
        for(int i = m;i >= 1;i--){
        if(a[i] > t) return false;
    if(w + a[i] > t){ w = 0; p--; } if(!p) return false; w += a[i]; tmp[p] = i; } for(int i = 1;i <= k;i++) rec[i] = tmp[i]; return true; } int main(){ cin>>m>>k; for(int i = 1;i <= m;i++){ scanf("%d",&a[i]); s[i] = s[i-1] + a[i]; } int l = 1,r = s[m],mid; while(l <= r){ mid = (l + r) >> 1; if(check(mid)){ r = mid - 1; }else{ l = mid + 1; } } rec[k+1] = m + 1; if(rec[1] != 1) rec[1] = 1; for(int i = 2;i <= k;i++){ if(rec[i] <= rec[i-1]){ rec[i] = rec[i-1] + 1; continue; } break; } for(int i = 1;i <= k;i++){ cout<<rec[i]<<" "<<rec[i+1]-1<<endl; } return 0; }
  • 相关阅读:
    C语言实验报告
    C语言实验报告
    第四次作业4-树和二叉树
    第03次作业-栈和队列
    第02次作业-线性表
    Data_Structure01-绪论作业
    C语言第二次实验报告
    C语言实验报告
    第04次作业-树
    第03次作业-栈和队列
  • 原文地址:https://www.cnblogs.com/hyfer/p/5791446.html
Copyright © 2011-2022 走看看