zoukankan      html  css  js  c++  java
  • [hdu4911]逆序对相关

    思路:由于只能交换相邻的数,所以每次最多减小1个逆序对(且如果存在逆序对那么肯定可以减小1个)!于是乎。。就是统计逆序对的裸题了。树状数组或归并都行。

     1 #pragma comment(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>
    10 #include <deque>
    11 #include <cmath>
    12 #include <vector>
    13 #include <ctime>
    14 #include <cctype>
    15 #include <set>
    16 
    17 using namespace std;
    18 
    19 #define mem0(a) memset(a, 0, sizeof(a))
    20 #define lson l, m, rt << 1
    21 #define rson m + 1, r, rt << 1 | 1
    22 #define define_m int m = (l + r) >> 1
    23 #define Rep(a, b) for(int a = 0; a < b; a++)
    24 #define lowbit(x) ((x) & (-(x)))
    25 #define constructInt4(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) {}
    26 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
    27 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
    28 
    29 typedef double db;
    30 typedef long long LL;
    31 typedef pair<int, int> pii;
    32 typedef multiset<int> msi;
    33 typedef multiset<int>::iterator msii;
    34 typedef set<int> si;
    35 typedef set<int>::iterator sii;
    36 typedef vector<int> vi;
    37 
    38 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
    39 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
    40 const int maxn = 1e5 + 7;
    41 const int maxm = 1e5 + 7;
    42 const int maxv = 1e7 + 7;
    43 const int MD = 1e9 +7;
    44 const int INF = 1e9 + 7;
    45 const double PI = acos(-1.0);
    46 const double eps = 1e-10;
    47 
    48 int tmp[maxn], a[maxn];
    49 
    50 LL merge(int l, int m, int r) {
    51     int p = m + 1, t = l;
    52     LL res = 0;
    53     for (int i = l; i <= m; i++) {
    54         while (p <= r && a[i] > a[p]) {
    55             tmp[t++] = a[p++];
    56         }
    57         res += p - m - 1;
    58         tmp[t++] = a[i];
    59     }
    60     for (int i = l; i < p; i++) a[i] = tmp[i];
    61     return res;
    62 }
    63 
    64 LL merge_sort(int l, int r) {
    65     if (l == r) return 0;
    66     define_m;
    67     return merge_sort(l, m) + merge_sort(m + 1, r) + merge(l, m, r);
    68 }
    69 
    70 int main() {
    71     //freopen("in.txt", "r", stdin);
    72     int n;
    73     LL k;
    74     while (cin >> n >> k) {
    75         for (int i = 0; i < n; i++) {
    76             scanf("%d", a + i);
    77         }
    78         LL ans = merge_sort(0, n - 1) - k;
    79         ans = max(ans, 0LL);
    80         cout << ans << endl;
    81     }
    82     return 0;
    83 }
    View Code

    Hint:做这题的时候发现一个不容易发现的坑(与这题本身无关),多个函数相加并不一定按从左至右的顺序执行(可能是为了某种意义上地优化代码)。此代码用c++交好像w会a掉,不信可以一试~(c++貌似优化程度比较高?)

  • 相关阅读:
    解决GOOGLE不能用的办法
    Elmah错误日志工具
    Linq 更改主键值
    qcow2、raw、vmdk等镜像格式
    Ceph相关博客、网站(256篇OpenStack博客)
    Delphi中inherited问题
    Qt qss一些伪装态,以及margin与padding区别
    Qt双缓冲机制:实现一个简单的绘图工具(纯代码实现)
    写出一篇好博文需要用到的工具
    最短路径启蒙题
  • 原文地址:https://www.cnblogs.com/jklongint/p/4418989.html
Copyright © 2011-2022 走看看