跳到正文

题解

题解:P18145 [NWRRC 2026] Nuts and Bolts

limaotong约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年11月30日
收录
2026年11月30日
更新
2026年11月30日
篇幅
约 2 分钟
题目
P18145

版本

  1. v1当前

想象木板在数轴上,单位长度为 11 厘米,孔在 00 的位置,那么显然第一块木板的最左边在 0−x=−x0-x=-x 的位置,第二块木板的最左边在 0−y=−y0-y=-y 的位置,第一块木板的最右边在 a−xa-x 的位置,第二块木板的最右边在 b−yb-y 的位置。整体的最左边就是两块木板的左端点更靠左的那个,也就是位置更小的。整体的最右边就是两块木板的右端点更靠右的那个,也就是位置更大的。

所以最终答案为 max⁡(a−x,b−y)−min⁡(−x,−y)\max(a-x,b-y)-\min(-x,-y)。

代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ft first
#define sd second
#define fs(i,x,y) for(int i=(x);i<=(y);i++)
#define fj(i,x,y) for(int i=(x);i>=(y);i--)
#define len(s) s.length()
#define mht(a,b,x,y) (fabs(x-a)+fabs(y-b))
#define juli(a,b,x,y) (sqrt((x-a)*(x-a)+(y-b)*(y-b)))
#define numb(c) (c-'0')
#define pii pair<int,int>
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0); 
	cout.tie(0);
    int a,b,x,y;
    cin>>a>>b>>x>>y;
    int l=min(-x,-y),r=max(a-x,b-y);
    cout<<r-l;
	return 0;
}
原文