View Code of Problem 613

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;
const int INF = 0x3f3f3f3f   ;
const int offset = 100       ;

int a[110000],tmp[110000];
int n;
ll ans;

void Merge (int l,int m,int r) {
    int i = l;
    int j = m + 1;
    int k = l;
    while (i <= m && j <= r) {
        if (a[i] > a[j]) {
            tmp[k++] = a[j++];
            ans += m - i + 1;
        } else {
            tmp[k++] = a[i++];
        }
    }
    while (i <= m) tmp[k++] = a[i++];
    while (j <= r) tmp[k++] = a[j++];
    for (int i = l; i <= r; ++i)
        a[i] = tmp[i];
}

void Merge_sort (int l,int r) {
    if (l < r) {
        int m = (l + r) >> 1;
        Merge_sort (l,m);
        Merge_sort (m+1,r);
        Merge (l,m,r);
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    int i, j, t, k, u, c, v, p, numCase = 0;

    while (cin >> n >> k) {
        for (i = 0; i < n; ++i) {
            cin >> a[i];
        }
        ans = 0;
        Merge_sort(0, n - 1);
        if (ans - k < 0) {
            cout << 0 << endl;
        } else {
            cout << ans - k << endl;
        }
    }

    return 0;
}










Double click to view unformatted code.


Back to problem 613