HDU 4911 Inversion 树状数组求逆序数对

显然每次交换都能降低1

所以求出逆序数对数,然后-=k就好了。。

_(:зゝ∠)_

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100005
#define ll long long
ll c[N+100000], maxn;
inline ll Lowbit(ll x){return x&(-x);}
void change(ll i, ll x)//i点增量为x
{
    while(i <= maxn)
    {
        c[i] += x;
        i += Lowbit(i);
    }
}
ll sum(ll x){//区间求和 [1,x]
    ll ans = 0;
    for(ll i = x; i >= 1; i -= Lowbit(i))
        ans += c[i];
    return ans;
}
ll a[N], n, k;
set<ll>s;
set<ll>::iterator p;
map<ll,ll>mp;
int main(){
    ll i;
    while(cin>>n>>k){
        s.clear(); mp.clear();
        for(i = 1; i <= n; i++)scanf("%I64d",&a[i]), s.insert(a[i]);
        maxn = n+100;
        for(p = s.begin(), i = 2; p!=s.end(); p++, i++)
        {
            mp[*p] = i;
        }
        for(i = 1; i <= n; i++)a[i] = mp[a[i]];
        memset(c, 0, sizeof c);
        ll ans = 0;
        for(i = n; i >= 1; i--)
        {
            ans += sum(a[i]-1);
            change(a[i], 1);
        }
        ans -= k;
        cout<< max(0ll, ans) <<endl;
    }
    return 0;
}
时间: 2024-10-06 01:20:08

HDU 4911 Inversion 树状数组求逆序数对的相关文章

HDU 2838 (树状数组求逆序数)

题意: 给你N个排列不规则的数(1~N),任务是把它从小到大排好,每次仅仅能交换相邻两个数,交换一次的代价为两数之和.求最小代价 思路:对于当前数X.我们如果知道前面比它大的数有多少,如果为K,那么有部分代价是确定的,那就是K*X.然后还得加上比它大的那些数之和,这就是当数列到X为止,排好所须要的最小代价. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h&

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

线段树或树状数组求逆序数(附例题)

学习了博主:MyZee   , shengweison 的文章 线段树或树状数组求逆序数 假设给你一个序列 6 1 2 7 3 4 8 5,  首先我们先手算逆序数, 设逆序数为 N; 6的前面没有比他大的数 N +=0 1的前面有一个比他大的数 N+=1 2的前面有一个比他大的数 N+=1 7的前面没有比他大的数 N+=0 ... 最后得到 N = 0 + 1 + 1 + 0 + 2 + 2 + 0 + 3 = 9 其实我们可用用线段树,或者树状数组模拟这个过程. 又因为线段树和树状数组的效率

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input seque

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

poj2299 Ultra-QuickSort 树状数组求逆序数

poj2299 Ultra-QuickSort   树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 49587   Accepted: 18153 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequenc

Codeforces Round #261 (Div. 2) D. Pashmak and Parmida&#39;s problem (树状数组求逆序数 变形)

题目链接 题意: 给出一些数a[n],求(i, j), i<j 的数量,使得:f(1, i, a[i]) > f(j, n, a[j]) . f(lhs, rhs, x) 指在 { [lhs, rhs]范围中,a[k]的值=x } 的数量. 1.  f(1, i, a[i]) 就是指a[i]前面包括a[i]的数中,有几个值=a[i]. 2.  f(j, n, a[j]) 就是指a[j]后面包括a[j]的数中有几个值=a[j]. 虽然a[x]范围不小,但是n的范围是1000,不是很大,所以我们可