Thinking about it:
After reading this question, there is really no idea, read the solution to understand. The minimum value is determined by the dichotomy before grouping. This way of thinking is beyond my expectation, and I have been moving towards DP. It seems that I need more discipline.
Reference:
"Algorithmic Competition Primer (2nd edition)"
Code:
/** * AC @ Sep 5th * Run time:0.000s */#include <bits/stdc++.h>using namespace Std;typedef long Long ll;cons t int maxn = + 50;int value[maxn];int M, K; LL sum, maxc;void read () {cin >> M >> K; sum = MaxC = 0; for (int i = 0; i < M; ++i) {cin >> value[i]; Sum + = Value[i]; MaxC = MaxC > Value[i]? Maxc:value[i]; }}bool is_possile (int p) {int tmpvalue = 0, group = 1; for (int i = M-1; I >= 0; i.) {if (Tmpvalue + value[i] > P | | i+1 <= k-group) {tmpvalue = Value[i]; Group + +; } else {tmpvalue + = Value[i]; }} return group = = K; void print (int pos, int group, int p) {std::vector<int> V; int i; LL sum = 0; for (i = pos; sum + value[i] <= p && i+1>k-group;-I.) {sum + = Value[i]; V.push_back (Value[i]); if (group = = K) {//the head for (int l = v.size ()-1; l > 0;--l) { cout << V[l] << ""; } cout << v[0]; } else {print (I, group+1, p); cout << "/"; for (int l = v.size ()-1; l >= 0;--l) {cout << "" << v[l]; }}}void output (int p) {print (M-1, 1, p); cout << Endl;} void work () {//If left are initialized with 0, some problems'll occur LL = MaxC, right = SUM, mid; while (right > left) {mid = (right + left) >> 1; if (Is_possile (mid)) {right = Mid; } else {left = mid + 1; }} output (right);} int main (int argc, char const *argv[]) {Ios::sync_with_stdio (false); Cin.tie (0); int T; Cin >> T; while (T--) {read (); Work (); } return 0;}
UVA 714 Copying Books