HUST 1343 Reverse Number (pre-qualifying practice in Harbin Tech Asia)

Source: Internet
Author: User

G-Reverse numberTime Limit:MS Memory Limit:131072KB 64bit IO Format:%lld &A mp %llu SubmitStatusPracticeHUST 1347

Description

Given a non-negative integer sequence A with the length N, you can exchange the adjacent numbers each time. After K exchanging operations, what ' s the minimum reverse number the sequence can achieve? The reverse number of a sequence is the number of pairs (I, j) such that I < J and Ai > Aj

Input

There is multiple cases. For each case, first line contains-numbers:n, K 2<=n<=100000, 0 <= K < 2^60 Second line contains N Non-ne Gative numbers, each of the which not greater than 2^30

Output

Minimum reverse number can get after K exchanging operations.

Sample Input

3 13 2 15 25 1 4 3 2

Sample Output

Case #1:2Case #2:5 first uses a tree-like array to find the number of reverse order. Because each exchange can increase or decrease a pair of reverse order number, false with m pair of reverse order number, we exchange n pair, then this n to we let him each time to reduce a pair of reverse order number, Exchange n times and then there are m-n pairs reverse. Pay attention to the value of K in the title, if the number of reverse order to find more than K, then you can directly obtain the result res-k, if less than K, at this time pay attention to whether there is a repetition in the number string, if not then the reverse number is exactly 0 after the Exchange res-k, the remaining number of exchanges is k-res, if K-res is even  , then we can repeat the same pair at this time the reverse number is still 0, if it is odd, this time only the result is 1. If there are duplicates in the string, then the two repetitions can be exchanged, regardless of whether it is an odd or even number, the result does not affect the minimum number of reverse pairs. 
/*=============================================================================## Author:liangshu-cbam # # QQ        : 756029571 # # School: Harbin Polytechnic University # # last modified:2015-08-30 22:32## filename:a.cpp## Description: # The people who is crazy enough to think they can change the world, is the ones who does! =============================================================================*/# #include <iostream># include<sstream> #include <algorithm> #include <cstdio> #include <string.h> #include < cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include < queue> #include <map> #include <set>using namespace std; #define MAXN 100010struct node{int v,id;} S[MAXN]; int c[maxn],n;typedef Long Long ll;ll res;bool cmp (node X,node y) {return ((X.V&GT;Y.V) | | ((X.V==Y.V) && (x.id>y.id));} int lowbit (int x) {return x& (x^ (x-1));}    ll getsum (int pos) {ll ret = 0LL; WhiLe (pos>0) {Ret+=c[pos];    pos-= Lowbit (POS); } return ret;}        ll update (int pos) {while (pos<=n) {c[pos]++;    Pos+=lowbit (POS);    }}int Main () {int k,x;    int cs = 1;        while (scanf ("%d%d", &n,&k)!=eof) {set<int>cnt;        Memset (C,0,sizeof (c));        res = 0;            for (int i=1; i<=n; i++) {scanf ("%d", &AMP;S[I].V);            Cnt.insert (S[I].V);        S[i].id = i;        } sort (s+1,s+n+1,cmp);            for (int i=1; i<=n; i++) {res + = Getsum (s[i].id);        Update (s[i].id);        } if ((res-k) >=0) printf ("Case #%d:%lld\n", CS ++,res-k);            else {if (cnt.size () < n) {printf ("Case #%d:0\n", CS + +);                } else {if (ABS (res-k)%2) printf ("Case #%d:1\n", CS + +);            else printf ("Case #%d:0\n", CS + +);  }      }} return 0;} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HUST 1343 Reverse Number (pre-qualifying practice in Harbin Tech Asia)

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.