I have read this question carefully for a long time .. In fact, the last two sentences are useful. This means that n books are given and then divided into k parts. the maximum number of pages for each book is the minimum. Ask you about the allocation scheme. If the minimum value is the same, there are multiple allocation schemes. If the number of the previous parts is small, it means that the lexicographic output is from small to large.
The greedy method is used here. The condition that f (x) is true is that x is the maximum value to divide n books into k portions. Then, the minimum value of x is obtained. How to determine this x is a binary method. x must be greater than 0 and smaller than the sum of all values. If it is true, the left half side is taken, if it is not true, the right half side is taken as a small value. During the writing, the binary method is not fully understood. I am afraid that the value will be lost and I will save it specially, in fact, x or y (two numbers are equal) after the end of the Bipartite method is the maximum value of each part. And how to determine whether the maximum value is true is to use the greedy method, as far as possible to expand to the right until the first one is greater than the maximum value. If the number of copies is the last one, it must be true. Otherwise, if the number of parts has not reached the last one, it is not true.
When outputting data, you should also note that you have to go from the back to the front. because the number of copies in the front is small, you have to go from the back to the front to greedy. When the remaining parts are the same as the number of copies, you cannot be greedy, every one must be separated. Let's take a look at the simulation data. The plus and minus one have to be related to the previous writing.
Note that the sum will exceed the int range because a maximum of 1x10 ^ 8 and a maximum of 500 are exceeded by 4x10 ^ 9. Therefore, use long. Another point is that at the time of output, the last number cannot be followed by another space. Otherwise, PE is reported.
AC code:
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define NMAX 505#define ll long longint a[NMAX];int ans[NMAX];int solve(ll Max,int n,int k){ int i=0,j=0,nct=0; ll sum=0; while(nct < k) { sum+=a[j]; if(sum > Max && i == j) return 0; if(sum > Max) { nct++; i = j; sum = 0; } else j++; if(j == n) return 1; } return 0;}void path(ll Max,int n,int k){ int nct = 0,i=n-1; ll sum=0; while(i>0) { sum+=a[i]; if(sum > Max) { sum = 0; ans[i] = 1; nct++; } else i--; if(i == k-nct-2) { for(int j = 0; j <= i; j++) ans[j] = 1; break; } } for(int i = 0; i < n; i++) { if(i == n-1) printf("%d",a[i]); else printf("%d ",a[i]); if(ans[i]) printf("/ "); } printf("\n");}int main(){ int i,n,k,m; scanf("%d",&n); while(n--) { memset(ans,0,sizeof(ans)); scanf("%d%d",&m,&k); ll sum = 0; for(i = 0; i < m; i++) { scanf("%d",&a[i]); sum += a[i]; } ll x=0,y=sum,z; while(x