UVA 669 (violence)

Source: Internet
Author: User

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", &AMP;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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.