标签:style blog color os io for 2014 html
为什么线段树可以求逆序数?
在你插a[i]时,前面已经有多少个比a[i]大的数
POJ 2299 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define MAX INT_MAX #define MIN INT_MIN #define LL __int64 #define init(a) memset(a,0,sizeof(a)) #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const int maxn = 500010; using namespace std; int T[maxn<<2], index[maxn], num[maxn]; int n; int cmp(const int i, const int j) { return num[i] < num[j]; } void update( int l, int r,int rt, int num, int add) { T[rt] += add; if(r==l) return ; int m = (l+r) >> 1; if(num <= m) update(lson, num, add); else update(rson, num, add); } int query( int l, int r, int rt,int L, int R) { if(L<=l && r<=R) return T[rt]; int m = (l+r) >> 1; int ans = 0; if(L <= m) ans += query(lson, L, R); if(R > m) ans += query(rson, L, R); return ans; } int main() { while(~scanf("%d", &n)) { if(n==0) break; init(index); for(int i=1;i<=n;i++) { scanf("%d", &num[i]); update(1, n,1, i, 1);//离散化 index[i] = i; } sort(index+1, index+n+1,cmp); //排序num,更新索引 LL sum = 0; for(int i=1;i<=n;i++) { sum += (query( 1, n,1, 1, index[i])-1); update(1, n,1, index[i], -1); } printf("%I64d\n", sum); } return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define MAX INT_MAX #define MIN INT_MIN #define LL __int64 #define init(a) memset(a,0,sizeof(a)) #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const int maxn = 500010; using namespace std; int T[maxn<<2], index[maxn], num[maxn]; int n; int cmp(const int i, const int j) { return num[i] < num[j]; } void update( int l, int r,int rt, int num, int add) { T[rt] += add; if(r==l) return ; int m = (l+r) >> 1; if(num <= m) update(lson, num, add); else update(rson, num, add); } int query( int l, int r, int rt,int L, int R) { if(L<=l && r<=R) return T[rt]; int m = (l+r) >> 1; int ans = 0; if(L <= m) ans += query(lson, L, R); if(R > m) ans += query(rson, L, R); return ans; } int main() { while(~scanf("%d", &n)) { if(n==0) break; init(index); for(int i=1;i<=n;i++) { scanf("%d", &num[i]); update(1, n,1, i, 1); index[i] = i; } sort(index+1, index+n+1,cmp); LL sum = 0; for(int i=1;i<=n;i++) { sum += (query( 1, n,1, 1, index[i])-1); update(1, n,1, index[i], -1); } // printf("%I64d\n", sum); LL ans = sum; for(int i = 1;i<=n;i++) { sum = sum - num[i] + (n-num[i]- 1); ans = min(sum,ans); } cout<<ans<<endl; } return 0; }
线段树求逆序数方法 HDU1394&&POJ2299,布布扣,bubuko.com
标签:style blog color os io for 2014 html
原文地址:http://blog.csdn.net/wjw0130/article/details/38541039