Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7787 | Accepted: 4247 |
Descriptionios
Inputide
Outputthis
Sample Inputspa
4 4 2 8 0 3 10 0 1 2 3 4 5 6 7 8 9 6 -42 23 6 28 -100 65537 5 0 0 0 0 0
Sample Outputcode
Scenario #1: 3 Scenario #2: 0 Scenario #3: 5 Scenario #4: 0
題目大意:歸併排序求逆序對。
#include <stdio.h> int num[1005]; int temp[1005]; int ans = 0; void Merge(int low, int mid, int high) { int i = low, j = mid + 1, k = 0; while(i <= mid && j <= high) { if (num[i] <= num[j]) { temp[k] = num[i]; i++; k++; } else { ans += mid - i + 1; temp[k] = num[j]; j++; k++; } } while(i <= mid) { temp[k] = num[i]; i++; k++; } while(j <= high) { temp[k] = num[j]; j++; k++; } for (k = 0, i = low; i <= high; k++, i++) { num[i] = temp[k]; } } void MergeSort(int lwo, int high) { if (lwo < high) { int mid = (lwo + high) / 2; MergeSort(lwo, mid); MergeSort(mid + 1, high); Merge(lwo, mid, high); } } int main() { int nCase, n, nCount = 0; scanf("%d", &nCase); while(nCase--) { scanf("%d", &n); ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &num[i]); } MergeSort(0, n - 1); printf("Scenario #%d:\n%d\n\n", ++nCount, ans); } return 0; }