您的位置:首页 > 其它

抄书,分任务~~~~~~~

2015-08-07 21:39 337 查看
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 calledscribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.

Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered

) that may have different number of pages (

) and you want to make one copy of each of them. Your task is to divide these books among k scribes,

. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers

such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k,

. At the second line, there are integers

separated by spaces. All these values are positive and less than 10000000.

Output

For each case, print exactly one line. The line must contain the input succession

divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.

If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.

Sample Input

2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100


Sample Output

100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100


思路:

贪心加二分

#include<iostream>
#include<string>
using namespace std;
#define maxn 505
int arr[maxn];
int di[maxn];
int m, k;
long long sum;
int jud(long long M)    //主要作用:找划分的次数能不能满足题目要求
{
long long sum2 = 0;
int p = k;
for (int i = 0; i < m; i++)
{
sum2 += arr[i];
if (sum2 > M)             //找到了划的地方
{
p--;                   //划的次数减减
i--;
sum2 = 0;                //把sun2重新肤质为0,以便下次计算
}
if (!p);                     //已经划完了
{
if (i != m - 1)          //划完了,但是划的次数太少了,所有的书还没分完,所以还是不满足
return 0;
else
return 1;
}
}
return 1;

}
void findnums()
{
memset(di, 0, sizeof(di));
long long left = 0, right = sum, mid;
while (left < right)
{

mid = (left + right) / 2;          //二分法找目标值
if (jud(mid))
right = mid;                    //当前值太大,转到左半边找
else
left = mid + 1;                  //当前值太小,转到右半边找
}

long long  sum0 = 0;
for (int i = m - 1; i >= 0; i--)        //找划的具体地方
{

sum0 += arr[i];
if (sum0 > right)
{
sum0 = 0;
k--;
di[++i] = 1;                  //存起划的地方
}
}

while (k>1)          //没划完,继续划
{

for (int i = 1; i < m; i++)
{
if (!di[i])
{
di[i] = 1;
k--;
break;
}
}
}
cout << arr[0];
for (int j = 1; j < m; j++)
{
if (di[j])
cout << " /";
cout << " " << arr[j];

}
cout << endl;

}
int main()
{
int T;
cin >> T;
while (T--)
{
memset(arr, 0, sizeof(arr));
sum = 0;
cin >> m >> k;
for (int i = 0; i < m; i++)
{
cin >> arr[i];
sum += arr[i];

}
findnums();

}
return 0;

}


心得:

代码看别人的,哎╮(╯▽╰)╭看来以后还是要自己多写写,感觉回来看都有点不懂了,习惯不一样,也是醉醉的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: