TOJ-2242(归并求逆序)
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 1482 Accepted Runs: 391
There are three members: Nuanran, Chhot, Kandy in Tju_Rocket, one ACM-ICPC team of TJU, they always play some strange games. One day, they play a game named Rocket's Game. In this game, Nuanran is the judge, and Chhot and Kandy are the players.
There is a N * N maze with the numbers from 0 to N*N-1 in it. One player chooses the numbers row by row to get a number sequence, and the other one chooses the numbers column by column to get another number sequence. They have to calculate the numbers of inversions of the sequences which they choose. The one who has the smaller numbers of inversions of the sequence will win the game.
We define the number of inversions of a sequence is the number of pairs of numbers which have the reverse order. That means, given any sequence A[], if A[i]>A[j] and i<j, A[i] and A[j] are called a reverse order pair.
For example, if we have a sequence 3, 0, 2, 4, 5, 1, the pairs of (3,0), (3,2), (3,1), (2,1), (4,1),(5,1) have the reverse order, so the number of inversions of this sequence is 6.
Each time Nuanran casts the dice (a die has six squares of the same size; each face of a die has a number in [1..6]), if the dice number M is odd, then Chhot chooses the numbers row by row, and Kandy chooses the numbers column by column, vice versa.
For example, if the maze is:
8 0 7
2 5 6
1 3 4
and Nuanran casts a number 5, then Chhot chooses the numbers sequence 8, 0, 7, 2, 5, 6, 1, 3, 4 and Kandy chooses the numbers sequence 8, 2, 1, 0, 5, 3, 7, 6, 4.
Your task is here: given a N * N maze and the number M casted by Nuanran, try to find who will win the game.
Input
The input consists of multiple test cases. Each test case starts with a line containing N and M (1 ≤ N ≤ 200, 1 ≤ M ≤ 6). This line is followed by N lines containing N numbers, each indicating a N * N maze. The input ends with EOF (the end of file).
Output
Output one line for each test case containing a word "Chhot wins the Rocket's Game" if Chhot wins, or "Kandy wins the Rocket's Game" if Kandy wins, or "Chhot and Kandy will try again" if it is a draw game. You must print a blank line between the cases except the last one.
Sample Input
1 3 0 3 3 1 0 3 4 2 5 7 8 6 4 3 1 2 5 12 4 6 9 15 11 8 10 14 3 7 0 13
Sample Output
Chhot and Kandy will try again Chhot wins the Rocket's Game Kandy wins the Rocket's Game
题意:有两个人玩游戏,一个n*n的数组,有一个人一行一行取得到一个序列,另一个人一列一列取得到一个序列。两个序列中逆序数小的人赢。如果裁判掷色子是奇数,则Chhot一行一行取,Kandy一列一列取,如果偶数,就相反。
思路:分别求两个序列的逆序。注意输出。每组case之间需要打空行,最后一个case后没有空行了。
1 #include<iostream> 2 #include <cstdio> 3 using namespace std; 4 #define maxn 210 5 int n, m; 6 int mat[maxn][maxn]; 7 8 int mat1[maxn*maxn]; 9 int mat2[maxn*maxn]; 10 //暂存序列 11 int str[maxn*maxn]; 12 13 //归并排序的子排序,将2个穿归并成一个,求逆序数 14 template<typename T> 15 long long mergeCountNum(T str1, int size1, T str2, int size2) 16 { 17 int i,j,k; 18 long long num; 19 i = j = k = num = 0; 20 21 //取2个串中较小元,组成总串 22 while(i < size1 && j < size2){ 23 if(str1[i] <= str2[j]){ 24 str[k++] = str1[i++]; 25 //加上逆序数 26 num += j; 27 } 28 else str[k++] = str2[j++]; 29 } 30 31 //将剩余的元素合并到总串后 32 while (i < size1){ 33 str[k++] = str1[i++]; 34 //加上逆序数 35 num += j; 36 } 37 while (j < size2) 38 str[k++] = str2[j++]; 39 40 //拷贝总串 41 for (int x = 0; x < (size1 + size2); x++) 42 str1[x] = str[x]; 43 44 return num; 45 } 46 47 //分治归并排序并返回逆序数 48 template<typename T> 49 long long mergesort(T str, int size) 50 { 51 if(size <= 1) return 0; 52 T str1 = str; 53 int size1 = size / 2; 54 T str2 = str + size1; 55 int size2 = size - size1; 56 return mergesort(str1,size1) + mergesort(str2,size2) 57 + mergeCountNum(str1,size1,str2,size2); 58 } 59 60 int main() 61 { 62 int t = 0; 63 while(~scanf("%d%d", &n, &m)){ 64 if(t != 0){ 65 printf("\n"); 66 } 67 int k = 0; 68 for (int i = 0; i < n; i++){ 69 for (int j = 0; j < n; j++){ 70 scanf("%d", &mat[i][j]); 71 mat1[k++] = mat[i][j]; 72 } 73 } 74 k = 0; 75 for (int i = 0; i < n; i++){ 76 for (int j = 0; j < n; j++){ 77 mat2[k++] = mat[j][i]; 78 } 79 } 80 81 long long count1 = mergesort(mat1, n*n); 82 long long count2 = mergesort(mat2, n*n); 83 84 if(m % 2 == 1){ 85 if(count1 < count2) 86 printf("Chhot wins the Rocket's Game\n"); 87 else if(count1 > count2) 88 printf("Kandy wins the Rocket's Game\n"); 89 } 90 else { 91 if(count1 < count2) 92 printf("Kandy wins the Rocket's Game\n"); 93 else if(count1 > count2) 94 printf("Chhot wins the Rocket's Game\n"); 95 } 96 if(count1 == count2) 97 printf("Chhot and Kandy will try again\n"); 98 t++; 99 } 100 return 0; 101 }

浙公网安备 33010602011771号