zoukankan      html  css  js  c++  java
  • BZOJ3170: [Tjoi2013]松鼠聚会

    【传送门:BZOJ3170


    简要题意:

      给出n个点的坐标,规定两个点的距离=max(|x1-x2|,|y1-y2|)

      要求选出一个点,使得这个点到所有点的距离和最小


    题解:

      切比雪夫转换例题

      将一个点(x,y)的坐标变为(x+y,x−y)后

      原坐标系中的切比雪夫距离=新坐标系中的曼哈顿距离

      求最小曼哈顿距离就行了

      关于切比雪夫与曼哈顿距离转化请左转


    参考代码:

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #define Maxn 110000
    #define mes(x,y) memset(x,y,sizeof(x))
    using namespace std;
    typedef long long LL;
    struct node{int x,id;}X[Maxn],Y[Maxn];
    bool cmp(node n1,node n2){return n1.x<n2.x;}
    LL Lx[Maxn],Ly[Maxn],Rx[Maxn],Ry[Maxn];
    int tx[Maxn],ty[Maxn];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            X[i].x=x+y;Y[i].x=x-y;
            X[i].id=Y[i].id=i;
        }
        sort(X+1,X+n+1,cmp);sort(Y+1,Y+n+1,cmp);
        for(int i=1;i<=n;i++) tx[X[i].id]=i,ty[Y[i].id]=i;
        for(int i=2;i<=n;i++)
        {
            Lx[i]=Lx[i-1]+LL(i-1)*(X[i].x-X[i-1].x);
            Ly[i]=Ly[i-1]+LL(i-1)*(Y[i].x-Y[i-1].x);
        }
        for(int i=n-1;i>=1;i--)
        {
            Rx[i]=Rx[i+1]+LL(n-i)*(X[i+1].x-X[i].x);
            Ry[i]=Ry[i+1]+LL(n-i)*(Y[i+1].x-Y[i].x);
        }
        LL ans=1LL<<62;
        for(int i=1;i<=n;i++) ans=min(ans,Lx[tx[i]]+Ly[ty[i]]+Rx[tx[i]]+Ry[ty[i]]);
        printf("%lld
    ",ans/2);
        return 0;
    }

     

  • 相关阅读:
    android 启动报错
    android 百度地图
    android LayoutInflater使用
    spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'
    sql mysql和sqlserver存在就更新,不存在就插入的写法(转)
    jsp include
    json 解析
    css
    Scrapy组件之item
    Scrapy库安装和项目创建
  • 原文地址:https://www.cnblogs.com/Never-mind/p/10460149.html
Copyright © 2011-2022 走看看