Merge sort to find the number of reverse order

Source: Internet
Author: User

A-frosh WeekTime limit:8000MS Memory Limit:0KB 64bit IO Format:%lld &%llu SubmitStatusPracticeUVA 11858Appoint Description:Acmparand (2013-07-07)System Crawler (2014-11-19)

Description

Problem E:frosh weekduring Frosh Week, students play various fun games to get to know all other and compete against othe R teams. In one such-game, all the Frosh-a team stand in a line, and is then asked to arrange themselves according to some crit Erion, such as their height, their birth date, or their student number. This rearrangement of the line must is accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required. Input Specificationinput contains several test cases. For each test case, the first line of input contains one positive an integer n, the number of students on the team, which wil l am no more than one million. The following n lines each contain one integer and the student number of each student on the team. No student number would appear more than once. Sample Input
3312
Output specificationfor Each test case, output a line containing the minimum number of swaps required to arrange the Stude NTS in increasing order by student number. Output for Sample Input
2
Ond?ej Lhoták
#include <stdio.h> #define N 1000001 typedef long Long LL;  int n;    int num[n], tmp[n];            int input () {if (scanf ("%d", &n)! = 1) return 0;      int i;      for (i = 0; i < n; i++) {scanf ("%d", &num[i]);  } return 1;            } LL mergesort (int l, int r, int a[]) {if (L >= R) return 0;      int m = (L + r) >> 1;      LL S1 = mergesort (L, M, a);      LL s2 = mergesort (M + 1, R, a);            LL sum = s1 + s2;      int p = l, q = m + 1;            int k = l; while (P <= m | | q <= r) {if (P > m | | | (q <= r && a[p] > A[q]))              {tmp[k++] = a[q++];          Sum + = m + 1-p;          } else {tmp[k++] = a[p++];      }} int i;      for (i = l; I <= R; i++) {a[i] = Tmp[i];  } return sum;      } void Solve () {LL ans = mergesort (0, n-1, num);  printf ("%lld\n", ans); } int main () {#ifndeF Online_judge freopen ("D:\\oj\\uva_in.txt", "R", stdin);      #endif while (input ()) {solve ();  } return 0;   }

  

Merge sort to find the number of reverse order

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.