Question link: https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4615
T represents the T group sample, N people and K groups are a region, and the next n numbers represent the positions of N people, followed by two replacements:
- Only two adjacent people can be exchanged at a time, and each exchange takes one second. Find a time t1 after the switch to the required location.
- You can directly exchange the positions of two people. The time spent is the difference between the two people's current positions. You can exchange them at the same time to find the time t2.
Obtain the time consumed by T1 over T2. Example: 10 3
3 7 1 2 4 6 5 8 10 9
Example of this group: First replacement: I. Swap 7 with 1? 3 1 7 2 4 6 5 8 10 9
Ii. Swap 7 with 2? 3 1 2 7 4 6 5 8 10 9
Iii. Swap 7 with 4? 3 1 2 4 7 6 5 8 10 9
Iv. Swap 7 with 6? 3 1 2 4 6 7 5 8 10 9
V. Swap 7 with 5? 3 1 2 4 6 5 7 8 10 9
Vi. Swap 10 with 9? 3 1 2 4 6 5 7 8 9 10 (now everyone is where they are supposed to be)
A total of 6 seconds, the second replacement: Direct Switch location, so the most time spent is: 3 7 1 2 4 6 5 8 10 9
5 seconds. Therefore, the second replacement is 1 second less than the first replacement.
If you encounter this problem during the competition, the second replacement can be done directly with greed, but the first one won't. Next, let's look at the question solution: Merge Sorting can be implemented. I found that the original number of reverse orders for Merge Sorting can be used in this way .......... A [I] = (a [I]-1)/k + 1 transforms a [I] And then returns the number of reverse orders.
// Sort the data from the merge statement to the reverse sequence # include <stdio. h> # include <iostream> using namespace STD; int g_ncount = 0; void mergearray (int A [], int first, int mid, int last, int temp []) {int I = first, j = Mid + 1; int M = mid, n = last; int K = 0; while (I <= M & J <= N) // number after a [I] Number A [J] {if (a [I] <= A [J]) temp [k ++] = A [I ++]; else {temp [k ++] = A [J ++]; // A [J] and each of the preceding numbers can form a reverse order number pair g_ncount + = m-I + 1 ;}} while (I <= m) temp [k ++] = A [I ++]; while (j <= N) temp [k ++] = A [J ++]; for (I = 0; I <K; I ++) A [first + I] = temp [I];} void mergesort (int A [], int first, int last, int temp []) {If (first <last) {int mid = (first + last)/2; mergesort (A, first, mid, temp ); // ordered mergesort (A, Mid + 1, last, temp) on the left; // ordered mergearray (A, first, mid, last, temp) On the right ); // merge two more ordered series} bool mergesort (int A [], int N) {int * P = new int [N]; If (P = NULL) return false; mergesort (A, 0, n-1, P); Delete [] P; return true;} int main () {int case, T, K, A [1005]; scanf ("% d", & case); For (int cas = 1; CAS <= case; CAS ++) {scanf ("% d", & T, & K); g_ncount = 0; int count2 = 0; For (INT I = 0; I <t; I ++) {scanf ("% d", & A [I]), a [I] = (a [I]-1)/k + 1; int M = I/k + 1; if (M> A [I]) count2 = max (count2, I + 1-(K * A [I] + 1 )); else if (M <A [I]) count2 = max (count2, K * (a [I]-1) + 1-(I + 1 ));} mergesort (A, T); printf ("case % d: % d \ n", Cas, g_nCount-count2 );}}
Uvalive 6604 airport sort [merge sort] [number of reverse orders]