Test instructions: There is a continuous n block of memory, and then there are k files, each file can be divided into Ki block into memory, the other memory block is empty, now gives each file of Ki fragments in memory placed in the first few blocks (fragment order), and then began to move the contents of the memory block, action a B means to put memory a in memory B , as long as B is empty and requires minimal action to enable the file to be stored sequentially from the smallest block of memory, outputting the sequential operation process.
A flag[i] = J means that the position I put on the debris should be placed on the position J, you can go through the first place to put the debris, and then start the loop, pick out an incorrect position, put the debris in the free position, and then loop through the pieces can be placed in the correct position in the correct position, Until all is right.
#include <stdio.h> #include <string.h>const int n = 10005;int N, k, Flag[n], Num;bool judge () {for (int i = 1; I <= num; i++) if (flag[i]! = i) return False;return true;} void Solve () {int c = 0;while (1) {int cc = c;for (int i = 1; I <= num; i++) {if (!flag[i]) {for (int j = 1; j <= N; J + +) if (flag[j] = i) {c + +;p rintf ("%d%d\n", J, I); flag[i] = i;flag[j] = 0;break;}}} if (c = = cc) break;}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &k), memset (flag, 0, sizeof (flag)); Inttemp, a num = 0;for (int i = 0; i < K; i++) {scanf ("%d", &temp), for (int j = 0; J < temp; J + +) {scanf ("%d", &A); FLA G[a] = ++num;}} if (judge ()) printf ("No optimization needed\n"), else {solve (); while (1) {for (int i = 1; I <= num; i++) {if (Flag[i] &am p;& Flag[i]! = i) {for (int j = num + 1, j <= N; j + +) if (!flag[j]) {printf ("%d%d\n", I, j); Flag[j] = flag[i];flag[ I] = 0;break;} Break;}} Solve (); if (judge ()) break;}} if (t) printf ("\ n");} return 0;}
UVA 669 (violence)