首页 > 学院 > 开发设计 > 正文

hdu1394 分治 or 线段树

2019-11-14 09:01:47
字体:
来源:转载
供稿:网友

     利用分治求一次逆序数,然后每次把第一个元素放到末尾,设该交换元素的值为x,设上一次求得的逆序数为y,那么此时的逆序数等于y - x + (n - x - 1),减去x是因为x作为第一个元素,其后共有x个元素小于x,移动x会导致逆序数减少x个,而加上 (n - x - 1) 是因为将x移动到末尾,其前面(n - 1)个元素中会有(n - x - 1)个元素大于x。

    此题的复杂度在于求第一次逆序数O(nlgn),后面每次移动元素求更新后的逆序数时间是O(1),因此总的复杂度为(nlgn)。

AC代码:

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 5000 +5;int a[maxn], c[maxn];int solve(int l, int r){    int mid = (l + r) / 2;    if( l == r) return 0;    int ans = 0;    ans += solve(l, mid) + solve(mid + 1, r);    // Merge    int b[maxn];    int x = l, y= mid + 1;    int  k = 0;    while(x <= mid && y <= r){        if(a[x] <= a[y]){            b[k++] = a[x++];        }        else {            ans += mid + 1 - x;            b[k++] = a[y++];        }    }    while(x <= mid) b[k++] = a[x++];    while(y <= r) b[k++] = a[y++];    k = 0;    for(int i = l; i <= r; ++i) a[i] = b[k++];    return ans;}int main(){    int n;    while(scanf("%d", &n) == 1){        for(int i = 0; i < n; ++i){            scanf("%d", &a[i]);        }        memcpy(c, a, sizeof(a));        int ans = solve(0, n-1);        int x = ans;        for(int i = 0; i < n; ++i){            ans = min(ans, x + n - 1 - 2 * c[i]);            x = x + n - 1 - 2 * c[i];        }        PRintf("%d/n",ans);    }    return 0;}

线段树也能做这个题,每个区间保存的值代表[l, r]中总出现多少元素,每次加入x元素时,查找区间[x + 2, n]即可得到该元素的加入会增加多少逆序数。

贴上线段树代码:

#include<cstdio>#define min(x,y) (x) < (y) ? x : yconst int maxn = 4 * 5000 + 5;int a[5000 + 5];struct node{    int L, R;    int cnt;}t[maxn];void Build(int l, int r, int cur){    t[cur].L = l, t[cur].R = r;    t[cur].cnt = 0;    if(l == r) return;    int mid = (l + r) / 2;    Build(l, mid, cur << 1);    Build(mid + 1, r, (cur << 1) + 1);}void add(int c, int cur){    int l = t[cur].L, r = t[cur].R;    t[cur].cnt++;    if(l == r) return;    int mid = (l + r) / 2;    if(c <= mid) add(c, cur << 1);    else add(c, (cur << 1) + 1);}int find1(int l, int r, int cur){ //search the Inversion (logn)    int l1 = t[cur].L, r1 = t[cur].R;    if(l == l1 && r == r1) return t[cur].cnt;    int mid = (l1 + r1) / 2;    if(r <= mid) return find1(l, r, cur << 1);    else if(l >= mid + 1) return find1(l, r, (cur << 1) + 1);    else return find1(l, mid, cur << 1) + find1(mid + 1, r, (cur << 1) + 1);}int main(){    int n;    while(scanf("%d", &n) == 1){        Build(1, n, 1);        int ans = 0;        for(int i = 0; i < n; ++i){            scanf("%d", &a[i]);            a[i]++;            add(a[i], 1);            if(a[i] + 1 <= n) ans += find1(a[i] + 1, n, 1);        }        int x = ans;        for(int i = 0; i < n; ++i){            ans = min(ans, x + n + 1 - 2 * a[i]);            x = x + n + 1 - 2 * a[i];        }        printf("%d/n",ans);    }    return 0;}如有不当之处欢迎指出!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表