其实只需要开三重循环

根据OI中的一个重要的原理

给定一个序列a,求一个数x使得\(\sum |a_i-x|\)最小,那么这个数是序列a的中位数

证明略

然后既然是中位数,一定是数列中的数,类比到这题,聚集到的点的横纵坐标一定都存在于某些点上,所以O(n^2)枚举聚集到哪个点。

然后求出所有点到这个聚集点的距离,sort一下,求一个前缀和,更新ans数组即可,预计复杂度\(O(n^3\log n)\)

#include <bits/stdc++.h>
using namespace std; struct coord
{
int x, y;
} a[55]; int n, ans[55], dis[55]; int main()
{
scanf("%d", &n);
memset(ans, 0x3f, sizeof(ans));
for (int i = 1; i <= n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
for (int x = 1; x <= n; x++)
for (int y = 1; y <= n; y++)
{
for (int p = 1; p <= n; p++)
dis[p] = abs(a[p].x - a[x].x) + abs(a[p].y - a[y].y);
sort(dis + 1, dis + 1 + n, less<int>());
for (int p = 1; p <= n; p++)
{
dis[p] += dis[p - 1];
ans[p] = min(ans[p], dis[p]);
}
}
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}

luogu1632 点的移动的更多相关文章

随机推荐

  1. git学习4 常用命令

    1:更新: 更新后,更新只在Workspace中,没有到暂存区.git status可以查看当前状态. git add <file>  可以放到待提交区.      git  checko ...

  2. 为什么每个浏览器都有Mozilla字样

    你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样?Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 ( ...

  3. web基础 (三) CSS

    css 层叠样式表 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector { property: value; property: value; ... property: ...

  4. [转]Missing MSS Settings in Security Options of Group Policy (GPO)

    I'm currently working on a new Windows Server 2012 and Windows 8 project. As part of that project is ...

  5. 2015.4.25利用UIAutomation 替代API函数,解决了ListView无法读数据的难题,顺便实现了鼠标模拟滚轮

    UIAutomation比API的优点是类似于消息处理机制,而不是主要靠模拟鼠标键盘发送消息 首先添加引用UIAutomationClient和UIAutomationTypes,在安装.net3.5 ...

  6. 如何Catalog磁带库中的备份集

    在NBU备份的环境中,可以使用以下步骤来Catalog磁带库中的备份集. 1. 查找需要Catalog的备份集名称 可以使用两种方法查找Oracle备份集. 方法一是使用RMAN的list命令查找,例 ...

  7. 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...

  8. orancle数据库 插入数量 值大于 1000 解决方案

    存储过程:当基站ID大于1000的时候,把ID通过存储过程插入表,然后处理 不推荐这么弄,没办法,项目逼到这了,以后尽量避免这样的需求发生! CREATE OR REPLACE PROCEDURE i ...

  9. 如何边遍历集合边删除元素--使用Iterator中的remove()方法

    在遍历集合时,想将符合条件的某些元素删除,开始是用了下面的方法 public static void main(String[] args) throws UnsupportedEncodingExc ...

  10. spring中的class配置不能使用properties中的字符串

    1.比如在a.properties中我们声明了一个变量: classRoom=com.wc82.ClassRoom 2.然后在spring的配置文件中:applicationContext.xml,有 ...