首页 > 其他分享 >HDU 1394 Minimum Inversion Number

HDU 1394 Minimum Inversion Number

时间:2022-10-25 14:04:53浏览次数:68  
标签:HDU 1394 int Number tot long ans ask include


题目链接:​​传送门​

求出原数组的逆序对
算把一个数从对头拿到队尾的过程中
产生的贡献
诶我好像昨天做过这个题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
int n, m, t[A], a[A];
int lowbit(int x) {return x & -x;}
void change(int x, int val) {
for (int i = x; i <= n; i += lowbit(i)) t[i] += val;
}
int ask(int x, int ans = 0) {
for (int i = x; i > 0; i -= lowbit(i)) ans += t[i];
return ans;
}

int main(int argc, char const *argv[]) {
while (~scanf("%d", &n)) {
memset(t, 0, sizeof t);
int tot = 0, ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i]; a[i]++;
tot += ask(n) - ask(a[i]);
change(a[i], 1);
}
ans = tot;
for (int i = 1; i <= n; i++) ans = min(ans, tot = tot - a[i] + n - a[i] + 1);
cout << ans << endl;
}
}


标签:HDU,1394,int,Number,tot,long,ans,ask,include
From: https://blog.51cto.com/lyle/5794662

相关文章