[hdu4911] Reverse order to the relevant

Source: Internet
Author: User

Idea: Because only the adjacent number can be exchanged, so a maximum of 1 in reverse order each time (and if there is a reverse pair then certainly can be reduced by 1)! So. Is the statistical inverse of the naked question. Either a tree array or a merge row.

1 #pragmaComment (linker, "/stack:10240000,10240000")2 3#include <iostream>4#include <cstdio>5#include <algorithm>6#include <cstdlib>7#include <cstring>8#include <map>9#include <queue>Ten#include <deque> One#include <cmath> A#include <vector> -#include <ctime> -#include <cctype> the#include <Set> -  - using namespacestd; -  + #defineMem0 (a) memset (a, 0, sizeof (a)) - #defineLson L, M, RT << 1 + #defineRson m + 1, R, RT << 1 | 1 A #definedefine_m int m = (L + r) >> 1 at #defineRep (A, b) for (int a = 0; a < b; a++) - #defineLowbit (x) ((x) & (-(x))) - #defineCONSTRUCTINT4 (name, a, B, C, D) name (int a = 0, int b = 0, int c = 0, int d = 0): A (a), B (b), C (c), D (d) {} - #defineCONSTRUCTINT3 (name, a, B, c) name (int a = 0, int b = 0, int c = 0): A (a), B (b), C (c) {} - #defineConstructInt2 (name, a, b) name (int a = 0, int b = 0): A (a), B (b) {} -  intypedefDoubledb; -typedefLong LongLL; totypedef pair<int,int>PII; +typedef multiset<int>MSI; -typedef multiset<int>:: iterator msii; thetypedefSet<int>si; *typedefSet<int>:: iterator sii; $typedef vector<int>VI;Panax Notoginseng  - Const intdx[8] = {1,0, -1,0,1,1, -1, -1}; the Const intdy[8] = {0, -1,0,1, -1,1,1, -1}; + Const intMAXN = 1e5 +7; A Const intMAXM = 1e5 +7; the Const intMAXV = 1e7 +7; + Const intMD = 1e9 +7; - Const intINF = 1e9 +7; $ Const DoublePI = ACOs (-1.0); $ Const DoubleEPS = 1e-Ten; -  - intTMP[MAXN], A[MAXN]; the  -LL Merge (intLintMintr) {Wuyi     intp = m +1, t =l; theLL res =0; -      for(inti = l; I <= m; i++) { Wu          while(P <= r && a[i] >A[p]) { -tmp[t++] = a[p++]; About         } $Res + = p-m-1; -tmp[t++] =A[i]; -     } -      for(inti = l; I < P; i++) A[i] =Tmp[i]; A     returnRes; + } the  -LL Merge_sort (intLintr) { $     if(L = = r)return 0; the define_m; the     returnMerge_sort (L, m) + merge_sort (M +1, R) +merge (L, M, R); the } the  - intMain () { in     //freopen ("In.txt", "R", stdin); the     intN; the LL K; About      while(Cin >> N >>k) { the          for(inti =0; I < n; i++) { thescanf"%d", A +i); the         } +LL ans = merge_sort (0N1) -K; -Ans =Max (ans, 0LL); thecout << ans <<Endl;Bayi     } the     return 0; the}
View Code

Hint: When you do this, you find a pit that is not easy to spot (not the problem itself), and the addition of multiple functions does not necessarily go from left to right (perhaps to optimize the code in some sense). This code in C + + as if W will be a drop, do not believe that a try ~ (C + + seems to be optimized to a higher degree?) )

[hdu4911] Reverse order to the relevant

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.