切比雪夫距离 入门例题

切比雪夫距离 入门例题切比雪夫距离和曼哈顿距离众所周知两个点 x1 y1 x2 y2 x 1 y 1 x 2 y 2 x1 y1 x2 y2 的曼哈顿距离是 x1 x2 y1 y2 x 1 x 2 y 1 y 2 x1 x2 y1 y2 显然我们可以通过不等式去掉绝对值 max x1 y1 x2 y2 x1 y1 x2 y2 max x 1 y 1 x 2 y 2 x 1 y 1 x 2 y 2


切比雪夫距离和曼哈顿距离

众所周知两个点 ( x 1 , y 1 ) , ( x 2 , y 2 ) (x_1, y_1), (x_2, y_2) (x1,y1),(x2,y2) 的曼哈顿距离是 ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ |x_1 – x_2| + |y_1 – y_2| x1x2+y1y2

显然我们可以通过不等式去掉绝对值 max ⁡ ( ∣ x 1 + y 1 + x 2 + y 2 ∣ , ∣ x 1 + y 1 − ( x 2 + y 2 ) ∣ ) \max(|x_1 + y_1 + x_2 + y_2|, |x_1 + y_1 – (x_2 + y_2)|) max(x1+y1+x2+y2,x1+y1(x2+y2))

切比雪夫距离就是 max ⁡ ( ∣ x 1 − x 2 ∣ , ∣ y 1 − y 2 ∣ ) \max(|x_1 – x_2|, |y_1 – y_2|) max(x1x2,y1y2)

然后我们发现这两个格式很像,事实上确实是可以转换的:

  • 曼哈顿距离 到 切比雪夫距离 : ( x , y ) → ( x + y , x − y ) (x, y) \to (x + y, x -y ) (x,y)(x+y,xy)
  • 切比雪夫距离 到 曼哈顿距离 : ( x , y ) → ( x + y 2 , x − y 2 ) (x, y) \to (\frac{x + y}{2}, \frac{x – y}{2}) (x,y)(2x+y,2xy)

注意: 可能 x + y 2 \frac{x + y}{2} 2x+y 不一定是整数,那么我们不妨考虑在四周枚举一下。


P3964 [TJOI2013]松鼠聚会

P3439 [POI2006]MAG-Warehouse

这题就是经典的不一定是整数,我们要在周围的点分类讨论一下。

#include  
      using namespace std; //#define Fread //#define Getmod #ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif // Fread template <typename T> void r1(T &x) { 
     x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T,typename... Args> inline void r1(T& t, Args&... args) { 
     r1(t); r1(args...); } #ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { 
     int z; typemod(int a = 0) : z(a) { 
    } inline int inc(int a,int b) const { 
    return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const { 
    return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const { 
    return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const { 
    return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const { 
    return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const { 
    return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) { 
    *this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) { 
    *this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) { 
    *this = *this * x; return *this;} int operator == (const typemod<mod> &x) const { 
    return x.z == z;} int operator != (const typemod<mod> &x) const { 
    return x.z != z;} }; typedef typemod<mod> Tm; #endif //#define int long long const int maxn = 1e5 + 5; const int maxm = maxn << 1; typedef long long int64; int64 sumx, sumy, sum; struct Node { 
     int64 x, y; int64 val; Node(void) : x(0), y(0), val(0) { 
    } void init() { 
     int nx, ny; r1(nx), r1(ny); x = (nx + ny), y = (nx - ny); sumx += x, sumy += y; } }a[maxn]; int n; signed main() { 
     // freopen("S.in", "r", stdin); // freopen("S.out", "w", stdout); int i, j; r1(n); for(i = 1; i <= n; ++ i) a[i].init(); sort(a + 1, a + n + 1, [&] (const Node &a, const Node &b) { 
     return a.x < b.x; }); // printf("%lld\n", sumx); for(sum = 0, i = 1; i <= n; ++ i) { 
     a[i].val += (i - 1) * a[i].x - sum; a[i].val += (sumx - sum - a[i].x) - (n - i) * a[i].x; sum += a[i].x; } sort(a + 1, a + n + 1, [&] (const Node &a, const Node &b) { 
     return a.y < b.y; }); for(sum = 0, i = 1; i <= n; ++ i) { 
     a[i].val += (i - 1) * a[i].y - sum; a[i].val += (sumy - sum - a[i].y) - (n - i) * a[i].y; sum += a[i].y; } // for(i = 1; i <= n; ++ i) printf("%d : %lld, %lld\n", i, a[i].val, a[i].y); int64 ans(1e18); for(i = 1; i <= n; ++ i) if(a[i].val < ans) ans = a[i].val; printf("%lld\n", ans >> 1); return 0; } 
#include  
      using namespace std; //#define Fread //#define Getmod #ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif // Fread template <typename T> void r1(T &x) { 
     x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T,typename... Args> inline void r1(T& t, Args&... args) { 
     r1(t); r1(args...); } #ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { 
     int z; typemod(int a = 0) : z(a) { 
    } inline int inc(int a,int b) const { 
    return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const { 
    return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const { 
    return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const { 
    return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const { 
    return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const { 
    return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) { 
    *this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) { 
    *this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) { 
    *this = *this * x; return *this;} int operator == (const typemod<mod> &x) const { 
    return x.z == z;} int operator != (const typemod<mod> &x) const { 
    return x.z != z;} }; typedef typemod<mod> Tm; #endif //#define int long long const int maxn = 1e5 + 5; const int maxm = maxn << 1; int n; __int128 min(__int128 x, __int128 y) { 
     return x < y ? x : y; } __int128 abs(__int128 x) { 
     return x < 0 ? -x : x; } __int128 sx, sy, st; struct Node { 
     __int128 x, y, t; Node(void) : x(0), y(0), t(0) { 
    } void init() { 
     __int128 nx, ny; r1(nx, ny, t); x = (nx + ny), y = (nx - ny); sx += x * t, sy += y * t, st += t; } }a[maxn]; void write(__int128 x) { 
     vector<char> s; s.clear(); while(x) s.push_back((char)('0' + x % 10)), x /= 10; reverse(s.begin(), s.end()); for(auto v : s) putchar(v); } void Out(__int128 x, __int128 y) { 
     write((x + y) >> 1), putchar(' '), write((x - y) >> 1), puts(""), void(); } __int128 calc(__int128 x, __int128 y) { 
     __int128 res(0); for(int i = 1; i <= n; ++ i) { 
     res += ( abs(x - a[i].x) + abs(y - a[i].y) ) * a[i].t; } return res; } void Work(__int128 x,__int128 y) { 
     int i, j; if((x & 1) == (y & 1)) return Out(x, y); __int128 s1 = calc(x, y + 1); __int128 s2 = calc(x - 1, y); __int128 s3 = calc(x, y - 1); __int128 s4 = calc(x + 1, y); __int128 ans = min(min(s1, s2), min(s3, s4)); if(ans == s1) return Out(x, y + 1); if(ans == s2) return Out(x - 1, y); if(ans == s3) return Out(x, y - 1); if(ans == s4) return Out(x + 1, y); } signed main() { 
     // freopen("S.in", "r", stdin); // freopen("S.out", "w", stdout); int i, j; r1(n); for(i = 1; i <= n; ++ i) a[i].init(); sort(a + 1, a + n + 1, [&] (const Node &a, const Node &b) { 
     return a.x < b.x; }); __int128 sum, ct, mnx, mny, x, y; mnx = 0x3f3f3f3f3f3f3f3f; mny = mnx = (mnx * mnx); for(sum = ct = 0, i = 1; i <= n; ++ i) { 
     __int128 tmp(0); tmp += a[i].x * ct - sum; tmp += - (st - ct - a[i].t) * a[i].x + (sx - a[i].t * a[i].x - sum); if(tmp < mnx) mnx = tmp, x = a[i].x; sum += a[i].x * a[i].t; ct += a[i].t; } sort(a + 1, a + n + 1, [&] (const Node &a, const Node &b) { 
     return a.y < b.y; }); for(sum = ct = 0, i = 1; i <= n; ++ i) { 
     __int128 tmp(0); tmp += a[i].y * ct - sum; tmp += - (st - ct - a[i].t) * a[i].y + (sy - a[i].t * a[i].y - sum); if(tmp < mny) mny = tmp, y = a[i].y; sum += a[i].y * a[i].t; ct += a[i].t; } Work(x, y); return 0; } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213704.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午5:50
下一篇 2026年3月18日 下午5:50


相关推荐

  • patch文件的执行和制作

    patch文件的执行和制作执行 patch 文件 br patch p NUM0 1 4 patchbr nbsp br NUM setting p0givestheen p1givesbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp u howard src blurfl blurfl cbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp withoutthele p4givesbr nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp blurfl blurfl c 制作 p

    2025年11月15日
    6
  • python微信推送{u‘errcode‘: 40008, u‘errmsg‘: u‘invalid message type rid: 6111061f-19703d5b[通俗易懂]

    python微信推送{u‘errcode‘: 40008, u‘errmsg‘: u‘invalid message type rid: 6111061f-19703d5b[通俗易懂]记录一下前两天自己搞的一个蠢事,当时是要做一个微信信息推送,我先是按照微信的接口文档和网上的一些例子把代码写好了,测试的时候一直报这个40008,看微信接口文档又是说消息类型不对,大概就是说你给的data跟你定义的模板格式不对但是我都对了好几次,发现没问题,后面检查了一下接口的链接,发现跟接口文档里的不一样,应该是在复制别人的时候复制错了,换成文档里的链接后就正常了。所以,以后遇到这种{u’errcode’:40008,u’errmsg’:u’invalidmessagetyperid:

    2022年6月10日
    69
  • 阿里笔试题整理1

    阿里笔试题整理11.下面哪一个不是动态链接库的优点?(B)A.共享B.装载速度快C.开发模式好D.减少页面交换知识补充1)动态链接库:a.动态链接提供了一种方法,使进程可以调用不属于其可执行代码的函数。b.使用动态链接库可以更为容易地将更新应用于各个模块,而不会影响该程序的其他部分。c.动态链接库文件,是一种不可执行的二进制程序文件,它允许程序共享执行特殊任务所必需的代码和其他资源。https…

    2022年5月16日
    66
  • java套打快递单

    java套打快递单packageorg sq common utils importorg apache commons codec binary Base64 importorg apache http entity StringEntity importorg dom4j Document importorg dom4j DocumentExce importorg dom4j

    2025年7月4日
    4
  • 使用教育邮箱免费激活JetBrains IDEA/Pycharm/Phpstorm/webstorm等

    使用教育邮箱免费激活JetBrains IDEA/Pycharm/Phpstorm/webstorm等教育邮箱格式 1 在网址 https www jetbrains com store fromMenu edition discounts 中说明如下 可见 JetBrains 对于师生是免费的 2 在网址 https www jetbrains com zh student 即可申请 3 申请完成后 在自己的教育邮箱中点击 Confirm

    2026年3月27日
    2
  • 【C#】 Convert.ToInt16 、Convert.ToInt32、Convert.ToInt64 区别[通俗易懂]

    【C#】 Convert.ToInt16 、Convert.ToInt32、Convert.ToInt64 区别[通俗易懂]   一般写程序是用的都是Convert.ToInt32,为什么呢?1.Convert.ToInt是数据类型转换成int类型2.   有三种方法toint16,toint32,toint64   int16-数值范围:-32768到32767   int32-数值范围:-2,147,483,648到2,147,483,647   int64…

    2026年1月31日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号