zoukankan      html  css  js  c++  java
  • bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)

    题目链接:

    3170: [Tjoi 2013]松鼠聚会

    Time Limit: 10 Sec  Memory Limit: 128 MB

    Description

    有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1。现在N个松鼠要走到一个松鼠家去,求走过的最短距离。

    Input

    第一行给出数字N,表示有多少只小松鼠。0<=N<=10^5
    下面N行,每行给出x,y表示其家的坐标。
    -10^9<=x,y<=10^9

    Output

    表示为了聚会走的路程和最小为多少。

    Sample Input

    6
    -4 -1
    -1 -2
    2 -4
    0 2
    0 3
    5 -2

    Sample Output

    20
     
    题意:
     
    给出n个点,选出一个点,求其他点到这一点的切比雪夫距离和的最小值;
     
    思路:
     
    切比雪夫距离可以转化成曼哈顿距离,把原来的坐标系旋转45度,
    原来的距离为max{|x1-x2|,|y1-y2|};
    旋转后的坐标可以表示为(x1-y1,x1+y1)
    2*max{|x1-x2|,|y1-y2|}=|x1-y1-(x2-y2)|+|x1+y1-(x2+y2)|;
    曼哈顿距离和的最小值可以x,y排序前缀和;这样就解决了;
     
    AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=1e5+10;
    const int maxn=1e3+20;
    const double eps=1e-12;
    
    struct node
    {
        int id;
        LL x,y;
    }po[N];
    int cmp(node a,node b)
    {
        return a.x<b.x;
    }
    int cmp1(node a,node b)
    {
        return a.y<b.y;
    }
    LL pre[N],nex[N],sumx[N],sumy[N];
    int main()
    {
        int n;
        LL x,y;
        read(n);
        For(i,1,n)
        {
            read(x);read(y);
            po[i].x=x-y;
            po[i].y=x+y;
            po[i].id=i;
        }
        sort(po+1,po+n+1,cmp);
        for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].x-po[i-1].x);
        for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].x-po[i].x);
        for(int i=1;i<=n;i++)sumx[po[i].id]=pre[i]+nex[i];
        sort(po+1,po+n+1,cmp1);
        for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].y-po[i-1].y);
        for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].y-po[i].y);
        for(int i=1;i<=n;i++)sumy[po[i].id]=pre[i]+nex[i];
        LL ans=inf;
        for(int i=1;i<=n;i++)ans=min(ans,sumx[i]+sumy[i]);
        cout<<ans/2<<endl;
        return 0;
    }
    

      

  • 相关阅读:
    HTTP Continuation or nonHTTP traffic 数据包
    linuxTcp IP协议栈源码阅读笔记(转)
    使用Windows命令行启动服务
    数据库集群
    ShellExecute
    oracle 中数据库完全导入导出:cmd命令行模式
    理解ORACLE数据库字符集
    asp.net 编码设置
    ShellExecute与ShellExecuteEx的用法
    C++用位运算实现循环移位
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5804190.html
Copyright © 2011-2022 走看看