UVa Problem Solution: 10041 - Vito's Family

本文介绍了一个简单的算法,用于解决Vito寻找居住街道的问题,该街道位于其亲戚住址的中位数上,以最小化总的移动距离。通过计算中位数并求得所有地址到该中位数地址的距离之和,可以有效地找到最佳位置。

Vito should live on the street which is the median of his relatives.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10041 C++ "Vito's Family" */
  6. #include <algorithm>
  7. #include <deque>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <list>
  11. #include <map>
  12. #include <queue>
  13. #include <set>
  14. #include <stack>
  15. #include <string>
  16. #include <vector>
  17. using namespace std;
  18. int min_dist_sum(int *first, int *last)
  19. {
  20.   int *median = first + (last - first) / 2;
  21.   nth_element(first, median, last);
  22.   int sum = 0;
  23.   for (; first < median; ++first)
  24.     sum += *median - *first;
  25.   for (; first < last; ++first)
  26.     sum += *first - *median;
  27.   return sum;
  28. }
  29.           
  30. int main(int argc, char *argv[])
  31. {
  32. #ifndef ONLINE_JUDGE
  33.   filebuf in, out;
  34.   cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
  35.   cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
  36. #endif
  37.   int ncases;
  38.   cin >> ncases;
  39.   while (ncases-- > 0) {
  40.     int r;
  41.     cin >> r;
  42.     int s[500];
  43.     for (int i = 0; i < r; ++i)
  44.       cin >> s[i];
  45.     cout << min_dist_sum(s, s + r) << '/n';
  46.   }
  47.   return 0;
  48. }

pre.nc的ncl_filedump输出是这样的: Variable: f Type: file filename: pre path: /mnt/d/vs/1111/biyelunwen/precipitation_monthly-mean_era5_1979-2018_v1.0.nc Number of Attributes:9 history : Wed Dec 23 11:29:49 2020: ncpdq -a time,latitude,longitude /tmp/tmp2rggwnzz.nc_temp /tmp/tmp2rggwnzz.nc Conventions : CF-1.7 Institution : (C) VITO, Belgium References : See documentation for full references at https://climate.copernicus.eu/ Contact : Please contact Copernicus User Support on the Copernicus Climate Change Service website (https://climate.copernicus.eu/) Version : v1 Title : C3S D427 SIS Biodiversity / Bio-climate indicators / precipitation_monthly-mean / ERA5 / 40-year month_00 / 0.5degree_global Source : ERA5 dataset Licence : ECMWF Copernicus licence 1.0 dimensions: latitude = 348 longitude = 720 time = 480 variables: Variable: latitude Type: double Total Size: 348 values 2784 bytes Number of Dimensions: 1 Dimensions and sizes: [ 348 <latitude> ] Coordinates: latitude: [-89.75..83.75] Number of Attributes: 9 _FillValue : nan axis : Y long_name : latitude standard_name : latitude units : degrees_north stored_direction : increasing type : double valid_max : 90 valid_min : -90 Variable: longitude Type: double Total Size: 720 values 5760 bytes Number of Dimensions: 1 Dimensions and sizes: [ 720 <longitude> ] Coordinates: longitude: [-179.75..179.75] Number of Attributes: 9 _FillValue : nan axis : X long_name : longitude standard_name : longitude units : degrees_east stored_direction : increasing type : double valid_max : 180 valid_min : -180 Variable: time Type: int64 Total Size: 480 values 3840 bytes Number of Dimensions: 1 Dimensions and sizes: [ 480 <time> ] Coordinates: time: [ 0.. 0] Number of Attributes: 4 units : days since 1979-01-01 00:00:00 calendar : proleptic_gregorian standard_name : time long_name : time Variable: precipitation_monthly-mean Type: float Total Size: 120268800 values 481075200 bytes Number of Dimensions: 3 Dimensions and sizes: [ 480 <time> x 348 <
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值