Hdu 5535 Cake construction + Memory search, hdu5535
Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5355
Question: Given n and m, where 1 <= n <= 1e5, 2 <= m <= 10; ask if there is ~ N numbers are divided into m groups, so that the sums in each group are equal. If m rows are output, each row indicates a group of values; otherwise, NO is output;
Ps: Total T <= 1000 groups of data, which is still quite large;
Train of Thought: After pre-prediction, if possible, the m group will be directly constructed from the largest to the smallest and equal m groups, so that the value of n can be reduced to 2 m ~ 4m-2; because when n = 4s-1, subtract a cycle again, and the right boundary of the next discussion is 2s-1, yi zhi (2s-1) * 2 m/2/m can be used to construct a suitable m-type sub-statement. In addition, the author's constant coefficient cannot be too large, and the AC code runs for 514 ms, when we change the n-cycle to n> 4 * s-1, we use the TLE command. In the search, the coefficient is more than doubled;
Dfs is also clever. You need to add a start to monotonically search for the data in each group. In the end, all m groups are obtained and then return true, instead of directly returning each group, in this way, you can also modify the directly selected errors;
After determining whether a group is complete, it only restores the parameter value to the original value. You also need to use dp optimization and set define for short;
1 # include <bits/stdc ++. h> 2 using namespace std; 3 # define rep0 (I, l, r) for (int I = (l); I <(r); I ++) 4 # define rep1 (I, l, r) for (int I = (l); I <= (r); I ++) 5 # define rep_0 (I, r, l) for (int I = (r); I> (l); I --) 6 # define rep_1 (I, r, l) for (int I = (r ); i> = (l); I --) 7 # define MS0 (a) memset (a, 0, sizeof (a) 8 # define MS1 (a) memset (, -1, sizeof (a) 9 # define MSi (a) memset (a, 0x3f, sizeof (a) 10 # define inf 0x3f3f3f 3f 11 # define lson l, m, rt <1 12 # define rson m + 1, r, rt <1 | 1 13 # define lowbit (x) (x & (-x) 14 typedef pair <int, int> PII; 15 # define A first 16 # define B second 17 # define MK make_pair 18 typedef long ll; 19 typedef unsigned int uint; 20 template <typename T> 21 void read1 (T & m) 22 {23 T x = 0, f = 1; char ch = getchar (); 24 while (ch <'0' | ch> '9') {if (ch = '-') f =-1; ch = getchar ();} 25 while (ch> = '0 '&& Ch <= '9') {x = x * 10 + ch-'0'; ch = getchar ();} 26 m = x * f; 27} 28 template <typename T> 29 void read2 (T & a, T & B) {read1 (a); read1 (B );} 30 template <typename T> 31 void read3 (T & a, T & B, T & c) {read1 (a); read1 (B); read1 (c );} 32 template <typename T> 33 void out (T a) 34 {35 if (a> 9) out (a/10 ); 36 putchar (a % 10 + '0'); 37} 38 int I, j, k, n, m, l, r; 39 const int N = 1e5 + 7; 40 int ans [11] [N], aux [11] [N], vs [44], ave, board; 41 in T dp [50] [11], rec [50] [11] [11] [50]; 42 # define def rec [board] [m] 43 bool dfs (int tot, int id, int start) 44 {45 if (tot = ave) {id ++; tot = 0; start = 1;} // This is the end of one item and the start of another item; of course, true is returned only when all ends; 46 if (id = m) {47 rep1 (I, 1, board) if (! Vs [I]) {48 aux [id] [++ aux [id] [0] = I; 49} 50 return true; 51} 52 rep1 (I, start, board) {53 if (tot + I> ave) return false; 54 if (! Vs [I]) {55 vs [I] = 1; 56 aux [id] [++ aux [id] [0] = I; 57 if (dfs (tot + I, id, I + 1) return true; 58 vs [I] = 0, aux [id] [0] --; 59} 60} 61 return false; 62} 63 bool solve (int top) 64 {65 if (top> = 4 m-1) {66 for (int I = 1, j = 0; I <= m; I ++, j ++) {67 ans [I] [++ ans [I] [0] = top-2 * m + 1 + j; 68 ans [I] [++ ans [I] [0] = top-j; 69} 70 return solve (top-2 * m ); 71} 72 else {73 ave = top * (top + 1)/m/2; 74 // printf ("% d", ave); 75 board = top; 76 if (dp [board] [m] = 0) {77 if (dfs (0, 1 )) {78 dp [board] [m] = 1; 79 rep1 (I, 1, m) 80 rep1 (j, 0, aux [I] [0]) 81 def [I] [j] = aux [I] [j]; // define 82} 83 else dp [board] [m] =-1; 84} 85 if (dp [board] [m] = 1) return true; 86 return false; 87} 88} 89 int main () 90 {91 // freopen ("data.txt", "r", stdin); 92 // freopen ("out.txt", "w", stdout); 93 int T; read1 (T); 94 for (int kase = 1; kase <= T; kase ++) {95 MS0 (vs); 96 rep1 (I, 0, 10) ans [I] [0] = aux [I] [0] = 0; 97 read2 (n, m); 98 ll sum = 1LL * n * (n + 1)/2; 99 if (sum % m | sum/m <n |! Solve (n) {100 puts ("NO"); 101 continue; 102} 103 puts ("YES"); 104 rep1 (I, 1, m) {105 printf ("% d", ans [I] [0] + def [I] [0]); 106 rep1 (j, 1, ans [I] [0].) 107 printf ("% d", ans [I] [j]); 108 rep1 (j, 1, def [I] [0]) 109 printf ("% d", def [I] [j]); 110 puts (""); 111} 112} 113 return 0; 114}