AT185 2点間距離の最大と最小 题解

Doveqise

2019-06-30 08:42:26

Solution

这道题我看题解区讲的都很谜 所以我来发图解释一下 如图(SketchPad画的有点丑) ![AT185题解图](https://cdn.luogu.com.cn/upload/pic/61695.png) 现在有$5$个点 ①②③④⑤,相邻两点距离分别为$1,2,3,4,5$, 我们要求①~⑤的最大距离,就是把他们的距离加起来 那么最小距离呢? 如果最长的边长度$≤$其余边长度之和, 我们看一下图二,就是这样,所以最小距离为$0$; 如果最长的边的长度$>$其余边长度之和, 我们看一下图三,这里4和5的距离改成了$8$, 所以最小距离就是最长的边的长度$-$其余边长度之和, 即$8-1-2-3=2$; 接下来贴代码↓ ```cpp #include<bits/stdc++.h> using namespace std; signed main() { int n, tot=0, maxx=0; scanf("%d",&n); for (int i=1,x; i<=n; i++) { scanf("%d",&x); tot+=x; maxx=max(maxx, x); } printf("%d\n",tot); tot-=maxx; if (tot>=maxx) puts("0"); else printf("%d\n",maxx-tot); return 0; } ```