HDU 1394 Minimum Inversion Number

来源:互联网 发布:大数据云计算人工智能 编辑:程序博客网 时间:2024/05/22 05:29

找以前的水题切,但是忘记以前怎么想的呢,TAT...抓狂

于是乎重新推了一次,找到了新的规律,时间复杂度大概是O(nlogn)+O(n^2)+O(n);如果数据规模还大一点就TLE了。。。

首先求出逆序对数,对于初始队列维护四个值:

1、第一个值为左边比A[i]大的数Left[i];

2、左边比A[i]小的数Left2[i];

3、右边比A[i]大的数Right[i];

4、i个数A[i]的逆序对数d[A[i]];

然后循环移位时可以发现一个规律,即新的数列逆序对数为:

当前逆序对数(Si)-(d[A[i]])-Left2[i]+Left[i]+Right[i];

#include <iostream>#include <cstdlib>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <map>#include <stack>using namespace std;const int maxn = 5000 + 100;const int INF = 0x3f3f3f3f;void readint(int &x){char c = getchar();while(!isdigit(c)) c = getchar();x = 0;while(isdigit(c)) { x = x*10+c-'0'; c = getchar(); }}void writeint(int x){if(x > 9) writeint(x/10);putchar(x%10+'0');}int sumv[maxn<<2];int A[maxn];int Left[maxn], Right[maxn];int Left2[maxn];int d[maxn];void pushup(int o){sumv[o] = sumv[o*2]+sumv[o*2+1];}void build(int o, int L, int R){if(L == R) { sumv[o] = 0; return ; }int M = L+(R-L)/2;build(o*2, L, M);build(o*2+1, M+1, R);pushup(o);}void update(int o, int L, int R, int p, int v){if(L == R) { sumv[o] = v; return ; }int M = L+(R-L)/2;if(p <= M) update(o*2, L, M, p, v);else update(o*2+1, M+1, R, p, v);pushup(o);}int query(int o, int L, int R, int ql, int qr){if(ql <= L && qr >= R) return sumv[o];int M = L+(R-L)/2, ans = 0;if(ql <= M) ans += query(o*2, L, M, ql, qr);if(qr > M) ans += query(o*2+1, M+1, R, ql, qr);return ans;}int n;int main(){while(~scanf("%d", &n)){memset(Left, 0, sizeof(Left));memset(Right, 0, sizeof(Right));memset(Left2, 0, sizeof(Left2));for(int i = 1; i <= n; i++) { readint(A[i]); A[i]++; }for(int i = 1; i <= n; i++){for(int j = 1; j <= i; j++){if(A[j] > A[i]) Left[i]++;if(A[j] < A[i]) Left2[i]++;} for(int j = i+1; j <= n; j++) if(A[j] > A[i]) Right[i]++;}build(1, 1, n);int ans = 0;for(int i = n; i >= 1; i--){int t = query(1, 1, n, 1, A[i]);update(1, 1, n, A[i], 1);d[A[i]] = t;ans += t;}int pre = 0, res = ans;for(int i = 1; i <= n-1; i++){pre = res;res = pre-d[A[i]]-Left2[i]+Left[i]+Right[i];ans = min(ans, res);}writeint(ans), puts("");}return 0;}


 

原创粉丝点击