zoukankan      html  css  js  c++  java
  • BZOJ3170 [Tjoi2013]松鼠聚会 切比雪夫距离

    BZOJ3170

    题意:


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

    n <= 1e5;

    思路: 

      题意描述的是切比雪夫距离,就是两点之间的距离为max(dx,dy)。要求所有点的话,用曼哈顿距离配上前缀和能比较快得求出来。

      所以要把切比雪夫距离转化为曼哈顿距离。

      曼哈顿距离通过对x,y坐标分别排序求前缀和,可以O(n)得出所有点的曼哈顿距离前缀和。

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    typedef pair<ll,int>pli;
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    //#define R register
    #define OKC ios::sync_with_stdio(false);cin.tie(0)
    #define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
    #define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    //priority_queue<int ,vector<int>, greater<int> >que;
    
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    
    /*-----------------------showtime----------------------*/
    
                const int maxn = 1e5+9;
                struct node
                {
                    ll x,y;
                    int id;
                }p[maxn];
    
                bool cmpx(node a, node b){
                    return a.x < b.x;
                }
    
                bool cmpy(node a,node b){
                    return a.y < b.y;
                }
                ll sum[maxn],sx[maxn],sy[maxn];
    int main(){
                int n;  
                scanf("%d", &n);
                for(int i=1; i<=n; i++){
                    ll x,y;
                    scanf("%lld%lld", &x, &y);
                    p[i].x = x+y;
                    p[i].y = x-y;
                    p[i].id = i;
                }
    
                sort(p+1,p+1+n,cmpx);
    
                for(int i=1; i<=n; i++) sx[i] = sx[i-1] + p[i].x;
                for(int i=1 ;i<=n; i++){
    
                    sum[p[i].id] = p[i].x*(i-1) - sx[i-1] + sx[n] - sx[i] - p[i].x*(n-i);
                }
    
                sort(p+1,p+1+n,cmpy);
    
                for(int i=1; i<=n; i++) sy[i] = sy[i-1] + p[i].y;
    
                ll ans = inff;
    
                for(int i=1 ;i<=n; i++){
    
                    ll tmp = p[i].y*(i-1) - sy[i-1] + sy[n] - sy[i] - p[i].y*(n-i);
                    ans = min(ans, sum[p[i].id] + tmp);
                }
                printf("%lld
    ", ans/2);
                return 0;   
    }
    View Code
  • 相关阅读:
    HDU3625(SummerTrainingDay05-N 第一类斯特林数)
    HDU3359(SummerTrainingDay05-I 高斯消元)
    HDU2157(SummerTrainingDay05-F dp)
    HDU4565(SummerTrainingDay05-C 矩阵快速幂)
    LOJ1070(SummerTrainingDay05-B 矩阵快速幂)
    SPOJ7001(SummerTrainingDay04-N 莫比乌斯反演)
    POJ3090(SummerTrainingDay04-M 欧拉函数)
    POJ1284(SummerTrainingDay04-K 原根)
    POJ2478(SummerTrainingDay04-E 欧拉函数)
    BZOJ3884(SummerTrainingDay04-C 欧拉定理)
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/10317560.html
Copyright © 2011-2022 走看看