zoukankan      html  css  js  c++  java
  • hdu 6318 Swaps and Inversions (线段树求逆序对数)

    Swaps and Inversions

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2787    Accepted Submission(s): 1071


    Problem Description
    Long long ago, there was an integer sequence a.
    Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
    You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
    What is the minimum amount of money you need to spend?
    The definition of inversion in this problem is pair (i,j) which 1i<jn and ai>aj.
     
    Input
    There are multiple test cases, please read till the end of input file.
    For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
    In the second line, n integers separated by spaces, representing the orginal sequence a.
    1n,x,y100000, numbers in the sequence are in [109,109]. There're 10 test cases.
     
    Output
    For every test case, a single integer representing minimum money to pay.
     
    Sample Input
    3 233 666 1 2 3 3 1 666 3 2 1
     
    Sample Output
    0 3
     
    题目大意:
    求给定序列的逆序数。
     
    维护一个和给定序列一样大的初始全为0的数组。从大到小依次取数列中的数,将其对应的位置置为1,并查询前面有多少个1,则逆序数加上这个数。
    用线段树可以方便地查询区间1的个数。
    注意从大到小取数的时候,如果两个数一样大,则先取位置靠后的。这样逆序数就不会多算了。在排序中实现这个构思。
     
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    
    using namespace std;
    
    const int maxn=100000;
    
    struct tnode
    {
        int num;//
        int seq;//序号
        bool operator<(const tnode& y) const
        {
            if(num==y.num)
                return seq<y.seq;
            return num<y.num;
        }
    };
    tnode node[maxn+10];
    
    struct ttree
    {
        int l,r;
        int sum;
    };
    ttree tree[maxn*4+10];
    
    void pushup(int x)
    {
        if(tree[x].l==tree[x].r)
            return;
        tree[x].sum=tree[x*2].sum+tree[x*2+1].sum;
    }
    
    void build(int x,int l,int r)
    {
        tree[x].l=l;
        tree[x].r=r;
        tree[x].sum=0;
        if(l<r)
        {
            int mid=(l+r)/2;
            build(x*2,l,mid);
            build(x*2+1,mid+1,r);
        }
    }
    
    void modify(int x,int pos)
    {
        if(tree[x].l==tree[x].r)
            tree[x].sum=1;
        else
        {
            int mid=(tree[x].l+tree[x].r)/2;
            if(pos<=mid)
                modify(x*2,pos);
            else
                modify(x*2+1,pos);
            pushup(x);
        }
    }
    
    int query(int x,int l,int r)
    {
        if(l<=tree[x].l&&tree[x].r<=r)
            return tree[x].sum;
        int ret=0;
        int mid=(tree[x].l+tree[x].r)/2;
        if(l<=mid)
            ret+=query(x*2,l,r);
        if(r>mid)
            ret+=query(x*2+1,l,r);
        return ret;
    }
    
    int main()
    {
        int n,x,y;
        while(scanf("%d%d%d",&n,&x,&y)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&node[i].num);
                node[i].seq=i;
            }
            sort(node+1,node+n+1);
    
            long long ans=0;
            build(1,1,n);
            for(int i=n;i>=1;i--)
            {
                ans+=1LL*query(1,1,node[i].seq);
                modify(1,node[i].seq);
            }
    
            printf("%lld
    ",ans*min(x,y));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Lua的各种资源2
    Lua的各种资源1
    游戏AI:行为树
    关于资源包存储资源路径名的方案
    scrapy爬虫笔记(创建一个新的项目并运行)
    scrapy爬虫笔记(安装)
    运行scrapy报错:You do not have a working installation of the service_identity module
    运行scrapy demo时报错:[twisted] CRITICAL: Unhandled error in Deferred
    python3下使用有道翻译网页版实现翻译功能~~~附源码
    python3+openCV实现图片的人脸人眼检测,原理+参数+源代码
  • 原文地址:https://www.cnblogs.com/acboyty/p/9715652.html
Copyright © 2011-2022 走看看