HDU 4911 inversion: Reverse Order Number pair

Source: Internet
Author: User
Click Open link Inversion Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 1106 accepted submission (s): 474


Problem descriptionbobo has a sequence A1, A2 ,..., An. He is allowed to swap two AdjacentNumbers for no more than K times.

Find the minimum number of inversions after his swaps.

Note: The number of inversions is the number of pair (I, j) where 1 ≤ I <j ≤ n and AI> AJ.
Inputthe input consists of several tests. For each tests:

The first line contains 2 integers n, k (1 ≤ n ≤ 105,0 ≤ k ≤ 109). The second line contains N integers A1, A2 ,..., An (0 ≤ AI ≤ 109 ).
Outputfor each tests:

A single integer denotes the minimum number of inversions.
Sample Input
3 12 2 13 02 2 1
 
Sample output
12
 
Authorxiaoxu Guo (ftiasch)
Source2014 multi-university training contest 5
Here is a series that allows you to calculate the number pairs in reverse order. One of the purposes of Merge Sorting is to find the inverse number pair, which is efficient with less time. You can use the line segment tree to discretization the array. This is exactly the same as poj2299.
// Merge and sort ms1820k # include <stdio. h> # include <string. h> # define M 100007 # define ll _ int64ll a [m], C [m], ans; void Merge (ll a [], int first, int mid, int last, ll C []) {int I = first, j = Mid + 1; int M = mid, n = last, K = 0; while (I <= M | j <= N) {If (j> N | (I <= M & A [I] <= A [J]) c [k ++] = A [I ++]; else {C [k ++] = A [J ++]; ans + = (M-I + 1) ;}} for (I = 0; I <K; I ++) A [first + I] = C [I];} void merge_sort (ll a [], int first, int last, ll C []) {If (first <last ){ Int mid = (first + last)> 1; merge_sort (A, first, mid, c); merge_sort (A, Mid + 1, last, c); merge (, first, mid, last, c) ;}} int main () {int N; ll K; while (scanf ("% d % i64d", & N, & K )! = EOF) {memset (A, 0, sizeof (a); memset (C, 0, sizeof (c); ans = 0; For (INT I = 0; I <n; I ++) scanf ("% i64d", & A [I]); merge_sort (A, 0, n-1, C ); if (ANS-k <0) printf ("0 \ n"); else printf ("% i64d \ n", ANS-k);} return 0 ;}

// Line segment tree 546ms27296k # include <stdio. h> # include <string. h >#include <algorithm> # define M 500007 # define ll _ int64using namespace STD; int s [m], n; struct tree {int L, R, mid; ll val;} tree [m <1]; struct SA {int ID; ll val;} p [M * 2]; int CMP (sa a, sa B) {return. val <B. val;} void build (INT left, int right, int I) {tree [I]. L = left; tree [I]. R = right; tree [I]. mid = (left + right)> 1; tree [I]. val = 0; If (Left = right) {return;} build (LEF T, tree [I]. mid, I * 2); Build (tree [I]. mid + 1, right, I * 2 + 1);} int query (INT left, int right, int I) {If (left> right) return 0; if (tree [I]. L = left & tree [I]. R = right) return tree [I]. val; If (right <= tree [I]. mid) query (left, right, I * 2); else if (left> tree [I]. mid) query (left, right, I * 2 + 1); else return query (left, tree [I]. mid, 2 * I) + query (tree [I]. mid + 1, right, I * 2 + 1);} void insert (INT left, int I) {tree [I]. val ++; If (tree [I]. L = T REE [I]. r) return; If (left <= tree [I]. mid) insert (left, 2 * I); else insert (left, 2 * I + 1);} void discretization () {int TMP = P [1]. val, Pos = 1; for (INT I = 1; I <= N; I ++) if (P [I]. val! = TMP) TMP = P [I]. val, P [I]. val = ++ Pos; else P [I]. val = Pos; For (INT I = 1; I <= N; I ++) s [p [I]. id] = P [I]. val;} int main () {_ int64 K; while (scanf ("% d % i64d", & N, & K )! = EOF) {ll ans = 0; build (0, M, 1); memset (S, 0, sizeof (s); For (INT I = 1; I <= N; I ++) {scanf ("% i64d", & P [I]. val); P [I]. id = I;} Sort (p + 1, P + n + 1, CMP); discretization (); For (INT I = 1; I <= N; I ++) {ans + = query (s [I] + 1, n, 1); insert (s [I], 1);} If (ANS-k <0) printf ("0 \ n"); else printf ("% i64d \ n", ANS-k);} return 0 ;}


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.