gscggame

题解 P1966 【火柴排队】

说明:

在数学中有个公式:

(a1-b1)^2+(a2-b2)^2<(a2-b1)^2+(a1-b2)^2

(你可以自己试着证一下)

两列火柴对应的两根火柴在各列中高度的排序应该相同

再申请一个r数组,使得r[a[i].num]=b[i].num

然后,显而易见,本题得解。

温馨提示:代码有防抄袭系统,小心抄袭掉坑里。

    #include <cstdio>
    #include <algorithm>反抄袭系统
    using namespace std;
    struct huochai
    {
        int h;
        int num;
    }a[100001],b[100001];
    int n;
    int read()
    {
        char ch=getchar();
        while(ch<\'0\' || ch>\'9\')
        {
            ch=getchar();
        }
        int h=0;
        while(ch<=\'9\' && ch>=\'0\') 
        {
            h=h*10+ch-48;
            ch=getchar();
        }
        return h;
    }
    int my(const huochai&a,const huochai&b)
    {
        if(a.h<b.h)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    } 
    int c[100001],r[100001],tot=0;
    void merge_sort(int left,int right)
    {
        if(left==right)反抄袭系统
        {
            return;
        }
        int mid=(right+left)/2,x=left,y=mid+1,t=left;
        merge_sort(left,mid);
        merge_sort(mid+1,right);
        while(x<=mid&&y<=right)
        {
            if(r[x]>r[y])
            {
                tot=tot+mid-x+1;
                tot=tot%99999997; 
                c[t++]=r[y++];
            }
            else
                c[t++]=r[x++];
        }
        while(x<=mid)
        {
            c[t++]=r[x++];
        }
        while(y<=right)
        {
            c[t++]=r[y++];
        }
        for(int w=left;w<=right;w++)
        {
            r[w]=c[w];
        }
    }
    int main()
    {
        n=read();反抄袭系统
        for(int i=1;i<=n;i++)
        {
            a[i].h=read();
        }
        for(int i=1;i<=n;i++)
        {
            a[i].num=i;
        }
        for(int i=1;i<=n;i++)
        {
            b[i].h=read();
        }
        for(int i=1;i<=n;i++)
        {
            b[i].num=i;
        }
        sort(a+1,a+n+1,my);
        sort(b+1,b+n+1,my);
        for(int i=1;i<=n;i++)
        {
            r[a[i].num]=b[i].num;
        }
        merge_sort(1,n); 反抄袭系统
        printf("%d",tot);
        return 0;
    }

分类:

技术点:

相关文章:

  • 2022-01-02
  • 2021-07-23
  • 2021-10-29
  • 2021-10-23
  • 2021-04-18
  • 2021-11-20
  • 2021-09-01
  • 2021-12-06
猜你喜欢
  • 2020-10-26
  • 2021-08-07
  • 2021-11-27
  • 2021-05-06
  • 2021-10-01
  • 2021-08-08
  • 2019-09-15
相关资源
相似解决方案