P1708题目链接
这题挺有意思的,看起来是一个复杂的优化问题,其实很水。
主要是题给了很多限定条件,比如中转站和天然气井的个数相等,最后还是一一对应的,求得距离只是走横向或纵向的……
做个比方吧,假设有3个天然气井(a、b、c)和3个中转站(d、e、f),由于一一对应,所以假设随便对应。
d i s t a n c e x = ( x a − x d ) + ( x b − x e ) + ( x c − x f ) d i s t a n c e y = ( y a − y d ) + ( y b − y e ) + ( y c − y f ) d i s t a n c e = ∣ d i s t a n c e x ∣ + ∣ d i s t a n c e y ∣ distance_{x}=(x_{a}-x{d})+(x_{b}-x_{e})+(x_{c}-x_{f})\\distance_{y}=(y_{a}-y{d})+(y_{b}-y_{e})+(y_{c}-y_{f})\\distance=|distance_{x}|+|distance_{y}| distancex=(xa−xd)+(xb−xe)+(xc−xf)distancey=(ya−yd)+(yb−ye)+(yc−yf)distance=∣distancex∣+∣distancey∣
由于加减法具有结合律,所以说随便换,括号也可以拆。
也就是说,只需简单的将天然气井的x求和,再将中转站的x求和,二者作差取绝对值,y同理,绝对值相加就是答案。
有一个小坑是,数据会出现爆int的情况,应该用long long。
AC代码#include
using namespace std;
int main() {
int n;
cin >> n;
long long x = 0, y = 0, temp;
for (int i = 0; i > temp;
x += temp;
cin >> temp;
y += temp;
}
for (int i = 0; i > temp;
x -= temp;
cin >> temp;
y -= temp;
}
cout
关注
打赏
- 【Linux】Ubuntu20.04安装和卸载MySQL8
- 【Linux】Ubuntu 20.04 报错 curl: (23) Failure writing output to destination 的解决方法
- 【Java】JUnit 4.13.2 警告 ‘assertEquals(double, double)‘ is deprecated 的解决方法
- 【JavaScript】处理 @parcel/transformer-js: Browser scripts cannot have imports or exports.
- 【Node.js】Windows环境安装配置NVM和Node.js
- 【Python】处理TypeError: Plain typing.NoReturn is not valid as type argument
- 【Python】Matplotlib可视化50例
- 【C语言】C语言修改MySQL数据库
- 【Java】从默认包导入类和对象报错的解决方法
- 【Java】panel.getGraphics()报错空指针异常的解决方法