抄书,分任务~~~~~~~
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
思路:
贪心加二分
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 #define maxn 505 5 int arr[maxn]; 6 int di[maxn]; 7 int m, k; 8 long long sum; 9 int jud(long long M) //主要作用:找划分的次数能不能满足题目要求 10 { 11 long long sum2 = 0; 12 int p = k; 13 for (int i = 0; i < m; i++) 14 { 15 sum2 += arr[i]; 16 if (sum2 > M) //找到了划的地方 17 { 18 p--; //划的次数减减 19 i--; 20 sum2 = 0; //把sun2重新肤质为0,以便下次计算 21 } 22 if (!p); //已经划完了 23 { 24 if (i != m - 1) //划完了,但是划的次数太少了,所有的书还没分完,所以还是不满足 25 return 0; 26 else 27 return 1; 28 } 29 } 30 return 1; 31 32 } 33 void findnums() 34 { 35 memset(di, 0, sizeof(di)); 36 long long left = 0, right = sum, mid; 37 while (left < right) 38 { 39 40 mid = (left + right) / 2; //二分法找目标值 41 if (jud(mid)) 42 right = mid; //当前值太大,转到左半边找 43 else 44 left = mid + 1; //当前值太小,转到右半边找 45 } 46 47 long long sum0 = 0; 48 for (int i = m - 1; i >= 0; i--) //找划的具体地方 49 { 50 51 sum0 += arr[i]; 52 if (sum0 > right) 53 { 54 sum0 = 0; 55 k--; 56 di[++i] = 1; //存起划的地方 57 } 58 } 59 60 while (k>1) //没划完,继续划 61 { 62 63 for (int i = 1; i < m; i++) 64 { 65 if (!di[i]) 66 { 67 di[i] = 1; 68 k--; 69 break; 70 } 71 } 72 } 73 cout << arr[0]; 74 for (int j = 1; j < m; j++) 75 { 76 if (di[j]) 77 cout << " /"; 78 cout << " " << arr[j]; 79 80 } 81 cout << endl; 82 83 } 84 int main() 85 { 86 int T; 87 cin >> T; 88 while (T--) 89 { 90 memset(arr, 0, sizeof(arr)); 91 sum = 0; 92 cin >> m >> k; 93 for (int i = 0; i < m; i++) 94 { 95 cin >> arr[i]; 96 sum += arr[i]; 97 98 } 99 findnums(); 100 101 } 102 return 0; 103 104 }
心得:
代码看别人的,哎╮(╯▽╰)╭看来以后还是要自己多写写,感觉回来看都有点不懂了,习惯不一样,也是醉醉的