Test instructions
Give n space, label 1 to N;
give the k files;
Each file gives a few space to take up, and gives the space to occupy;
Now we have to arrange these files in order;
For example, three files, respectively, occupy the number of space 4,3,2;
Then the first file will be put into space 1,2,3,4, the second file put to 5,6,7 third file put to 8, 9;
Ask how to move, require the least movement;
Ideas
Violence
A Move all the parts that can be moved to the correct position (that is, this part is not in the correct position, and the correct one is currently empty)
Two There is no way to move, and then take a position misplaced, put in an empty space (so you can continue to move), and then continue to perform the first step above;
Know right so far;
If it is correct at first; output no optimization needed
#include <cstdio> #include <cstring>const int N = 10005;int Clus[n], CNT, N, K, make;void solve () {int c = 0;whil E (1) {int cc = c;for (int i = 1; I <= n;i++) {if (clus[i]! = i && clus[i]! = 0 && clus[clus[i]] = = 0) {c+ +;p rintf ("%d%d\n", i,clus[i]); Clus[clus[i]] = clus[i];clus[i] = 0;}} if (c = = make) break;if (cc = = c) {bool F = 0;for (int i = 1; I <= n; i++) {if (clus[i]! = i && clus[i]! = 0) {for (i NT J = 1; J <= N; j + +) {if (clus[j] = = 0) {printf ("%d%d\n", i,j); Clus[j] = clus[i];clus[i] = 0;f = 1;break;}}} if (f) break;}}}} int main () {int t;scanf ("%d", &t), while (t--) {memset (clus, 0, sizeof (clus)), make = 0;SCANF ("%d%d", &n,&k); CNT = 1;for (int i = 1; I <= K; i++) {int c;scanf ("%d", &c), int tmp;for (int i = 0; i < c;i++) {scanf ("%d", &tmp ); Clus[tmp] = CNT++;IF (tmp! = cnt-1) make++;} if (make = = 0) printf ("No optimization needed\n"); solve (); if (t) printf ("\ n");}}
uva669 (violence)