HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱
思路:最近在补之前还没过的题,发现了这道多校的题。显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯定是-1操作。那么显然问题就变成了求逆序对数*min(x,y)。树状数组求逆序对数。
代码:
#include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> typedef long long ll; using namespace std; const int maxn = 100000 + 10; const int seed = 131; const ll MOD = 100000007; const int INF = 0x3f3f3f3f; int a[maxn], b[maxn], node[maxn]; int lowbit(int x){ return x&(-x); } void update(int x){ for(int i = x; i < maxn; i += lowbit(i)) node[i]++; } ll query(int x){ ll ret = 0; for(int i = x; i > 0; i -= lowbit(i)) ret += node[i]; return ret; } int main(){ int n, x, y; while(~scanf("%d%d%d", &n, &x, &y)){ ll ans = 0; memset(node, 0, sizeof(node)); for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); for(int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; for(int i = n; i >= 1; i--){ ans += query(a[i] - 1); update(a[i]); } printf("%lld\n", min(x, y) * ans); } return 0; }