codevs 4310 复制书稿

4310 复制书稿

 

 时间限制: 1 s
 空间限制: 4000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

现在要把m本有顺序的书分给k个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一、第三和第四本书给同一个人抄写。

现在请你设计一种方案,使得复制时间最短。复制时间为抄写页数最多的人用去的时间。

输入描述 Input Description

第一行两个整数m,k;(k≤m≤500),     

第二行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

k≤m≤500

 
#include<cstdio>
#include<iostream>
using namespace std;
int m,k;
int a[600],b[600],f[600][600];
void xx(int x,int y)//输出,贪心 
{
	if (y==0) return;
	if (y==1) //只有一个人抄书 
	  {  
          cout<<1<<' '<<x<<endl;
          return;
	  }
	int t=x,l=a[x]; 
	while (l+a[t-1]<=f[k][m]) //只要第j个人能抄,就让他抄,使前j个人抄书负担最小 
	  {
	  	l+=a[t-1];
	  	t--;
	  }
	xx(t-1,y-1);//此时只有t-1本书可以抄,还有y-1个人 
	cout<<t<<' '<<x<<endl;//递归输出 
}
int main()
{
	scanf("%d%d",&m,&k);
	for (int i=1;i<=k;i++)
	  for (int j=1;j<=m;j++)
	    f[i][j]=1000000;
	for (int i=1;i<=m;i++)
	  {
	  	 scanf("%d",&a[i]);
	  	 b[i]=b[i-1]+a[i];
	  	 f[1][i]=b[i];
	  }
	
	for (int i=2;i<=k;i++)//f[i][j]为j本书交给i个人抄所需的最短时间
	  for (int j=i;j<=m;j++)
	    for (int p=1;p<=j-1;p++)
	      f[i][j]=min(f[i][j],max(f[i-1][p],b[j]-b[p])); 
	xx(m,k);//从最后一个人开始分配 
	return 0;
} 

  

I'm so lost but not afraid ,I've been broken and raise again
原文地址:https://www.cnblogs.com/sjymj/p/5299119.html