2014年7月7日

UVa 12488 - Start Grid

#include <stdio.h>

int main() {
  int N;
  while (scanf("%d", &N) == 1) {
    int grid[2][99];
    int i, j, k;
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[0][i]);
    }
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[1][i]);
    }
    int count = 0;
    for (i = N - 1; i >= 0; i--) {
      for (j = 0; j < N; j++) {
        if (grid[0][j] == grid[1][i]) {
          for (k = j; k < i; k++) {
            int temp = grid[0][k + 1];
            grid[0][k + 1] = grid[0][k];
            grid[0][k] = temp;
            count++;
          }
          break;
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

沒有留言:

張貼留言