题目链接:

点击前往


题目:

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

描述

"K-Dimensional Foil" is a dimensional weapon. Its function is quite easy: It can ascend a region in 3D space to K (K≥3) dimension. One can use it to give the enemy unexpected attack. It was called "The Ultimate Weapon".

--"Remembrance of Mars's Past"

You are the chief technology officer in the space fleet, and your fleet was just suffered from the attack of the K-Dimensional Foil. The good news was that you have found the key parameter K, the dimension of the space. But staying in high dimensional space is very dangerous, you must destroy the K-Dimensional Foil as fast as possible.

You have n spaceships, spaceship i locates at si = (si,1, …, si,K), and the K-Dimensional  Foil is a 1-norm ball with center c = (c1, …, cK) and radius r, a 1-norm ball with center c and radius r is a point set defined as
{x |  d(x, c)  ≤ r}, d(x, c) =∑| xi - ci |

In the formula above, the coordinate of point x is (x1, x2 … xK)

Your spaceships will fire laser cannon to destroy the K-Dimensional Foil. The energy decay is very quick with the increase of the distance in the high dimensional space, so for every spaceship, you want to find the closest point (in Euclidean distance) on the K-Dimensional Foil. It's guaranteed that no spaceship is in the K-Dimensional Foil initially.

输入

The first line of the input is an integer T (T ≤ 100), the number of the test cases.

For each test case, the first line contains two integer n, K (1 ≤ n ≤ 50, 1 ≤ K ≤ 100), the number of spaceship in your fleet and the dimension of the space.

Then one line contains an integer r (1 ≤ r ≤ 104 ), the radius of the K-Dimensional Foil.

Then one line contains K integers c1, … cK, meaning the coordinate of the center of the K-Dimensional Foil.

Then n lines follow. Each line contains K integers si,1, …, si,K, meaning the coordinate of a spaceship.

All the absolute values of the coordinate are smaller than 104.

输出

For each test case, output n lines. The ith line contains K numbers representing the coordinate of the closest point on the K-Dimensional Foil to the ith spaceship. The absolute error between your output and the answer should be less than 10-4

提示

The K-Dimensional Foil in the sample was a square with vertex: (1,0), (0,1), (-1,0), (0,-1)

This problem is special judged.

样例输入

1
2 2
1
0 0
1 1
1 3
样例输出
0.50 0.50
0.00 1.00

题目大意:

题目中在kkk维空间下的“球”,定义如下:
给定球心c(c1,...,ck)c(c_1,...,c_k)c(c1​,...,ck​),和半径rrr。则所有与圆心ccc的曼哈顿距离≤r\leq r≤r的点组成的集合就是kkk维空间下的球。(点xxx和点ccc的曼哈顿距离dis(x,c)=∑i=1k(∣xi−ci∣)dis(x,c) = \sum_{i=1}^{k}{(|x_i-c_i|)}dis(x,c)=∑i=1k​(∣xi​−ci​∣),则”球“包含的点的集合可以描述为{x∣dis(x,c)≤r}\{x|dis(x,c)\leq r\}{x∣dis(x,c)≤r})
然后有nnn个询问,每个询问包含一个kkk维空间下的点,要求输出距离该点曼哈顿距离最小的,且在给定的“球”上的点。


解题思路:

在二维的时候,作图如下:(球心为(0,0)(0,0)(0,0),半径r=1r = 1r=1)

三维的时候作图如下:(球心为(0,0,0)(0,0,0)(0,0,0),半径r=1r = 1r=1)

方法一:(利用点到平面的投影)

首先,我们只考虑第一象限(推广到kkk维就是所有的坐标均≥0\geq 0≥0)(因为其他的点都可以通过对称对称到第一象限)的点,并且假设球心在远点可以平移过来,最后在平移回去)。在第一象限内,“球”只包含唯一的一个平面,那么,如果平面足够大的话,则显然所求答案就是点在平面上的投影。如果点在平面上的投影超过了平面的范围,我们可以先进行投影,然后找到第一个≤0\leq 0≤0的分量(显然,该分量在第一象限的平面外面,因为第一象限的所有分量都是非负数),贪心的让他等于000 (很显然这个分量等于0,最终的距离最小),然后在对剩下的维度重新进行投影(就相当于少了一个维度,进行了降维。)
计算点到平面的投影,可以参照我的这篇博客。下面的内容较为简略,不明白可以查看上述链接。
在本题中,第一象限内的平面方程为:
(1)x1+x2+...+xk−r=0x_1+x_2+...+x_k-r=0\tag{1}x1​+x2​+...+xk​−r=0(1)
设已知点P(p1,p2,...,pk)P(p_1,p_2,...,p_k)P(p1​,p2​,...,pk​),投影点(v1,v2,...,vk)(v_1,v_2,...,v_k)(v1​,v2​,...,vk​),两者连线为l1l_1l1​,则l1l_1l1​的参数方程为:
(2)(x1−p1)=(x2−p2)=...=(xk−pk)=t(x_1-p_1)=(x_2-p_2)=...=(x_k-p_k)=t \tag{2}(x1​−p1​)=(x2​−p2​)=...=(xk​−pk​)=t(2)

(3){v1=t+p1v2=t+p2...vk=t+pk\begin{cases} v_1 &=& t + p_1 \tag{3} \\ v_2 &=& t + p_2 \\ &...&\\ v_k &=& t + p_k \\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧​v1​v2​vk​​==...=​t+p1​t+p2​t+pk​​(3)
将(3)(3)(3)代入原方程(1)(1)(1)解得
(4)t=−(p1+p2+...+pk−r)kt = \cfrac{-(p_1+p_2+...+p_k-r)}{k}\tag{4} t=k−(p1​+p2​+...+pk​−r)​(4)
将(4)(4)(4)代入(3)(3)(3)中得:
(2.5)(v1,v2,...,vk)={v1=t+p1v2=t+p2...vk=t+pk={v1=−(p1+p2+...+pk−r)k+p1v2=−(p1+p2+...+pk−r)k+p2...vk=−(p1+p2+...+pk−r)k+pk\begin{aligned} (v_1,v_2,...,v_k)&= \tag{2.5} \begin{cases} v_1 &=& t + p_1 \\ v_2 &=& t + p_2 \\ &...&\\ v_k &=& t + p_k \\ \end{cases}\\ &= \begin{cases} v_1 &=& \cfrac{-(p_1+p_2+...+p_k-r)}{k} + p_1 \\ v_2 &=& \cfrac{-(p_1+p_2+...+p_k-r)}{k} + p_2 \\ &...&\\ v_k &=& \cfrac{-(p_1+p_2+...+p_k-r)}{k} + p_k \\ \end{cases}\\ \end{aligned} (v1​,v2​,...,vk​)​=⎩⎪⎪⎪⎨⎪⎪⎪⎧​v1​v2​vk​​==...=​t+p1​t+p2​t+pk​​=⎩⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎧​v1​v2​vk​​==...=​k−(p1​+p2​+...+pk​−r)​+p1​k−(p1​+p2​+...+pk​−r)​+p2​k−(p1​+p2​+...+pk​−r)​+pk​​​(2.5)
按照公式计算即可。具体见代码


方法二:(贪心)

根据题意,给定已知点P(p1,p2,...,pk)P(p_1,p_2,...,p_k)P(p1​,p2​,...,pk​),需要求得kkk维球面上的点V(v1,v2,...,vk)(v_1,v_2,...,v_k)(v1​,v2​,...,vk​)(球心为(c1,c2,...,ck)(c_1,c_2,...,c_k)(c1​,c2​,...,ck​),半径为rrr),使得dis(P,V)dis(P,V)dis(P,V)最小,其中:
{dis(P,V)=∑i=1k(pi−vi)2∑i=1k(∣vi−ci∣)=r\begin{cases} dis(P,V)=\sum_{i=1}^{k}(p_i-v_i)^2 \\ \sum_{i=1}^{k}{(|v_i-c_i|)}=r\\ \end{cases} {dis(P,V)=∑i=1k​(pi​−vi​)2∑i=1k​(∣vi​−ci​∣)=r​
根据题意,我们可以得到下式
∑i=1k(∣pi−ci∣)=T(T≥r)\sum_{i=1}^{k}{(|p_i-c_i|)} = T (T\geq r) i=1∑k​(∣pi​−ci​∣)=T(T≥r)
设:
d=T−rd = T - r d=T−r
那么,原题目可以等价成:
我们有一个点P′(p1′,p2′,...,pk′)P'(p'_1,p'_2,...,p'_k)P′(p1′​,p2′​,...,pk′​),初始P′=PP'=PP′=P,然后之后的每一次操作都可以选择某一个维度的分量pi′p'_ipi′​,使得pi′=pi′±1p'_i = p'_i\pm1pi′​=pi′​±1,要求在ddd次操作之后,∑i=1k(∣pi′−ci∣)=r\sum_{i=1}^{k}{(|p'_i-c_i|)}=r∑i=1k​(∣pi′​−ci​∣)=r,同时∑i=1k(pi′−pi)2\sum_{i=1}^{k}(p'_i-p_i)^2∑i=1k​(pi′​−pi​)2最小。
每进行一次操作都可以让∑i=1k(∣pi′−ci∣)\sum_{i=1}^{k}{(|p'_i-c_i|)}∑i=1k​(∣pi′​−ci​∣)减小111(即使得某一∣pi′−ci∣|p'_i-c_i|∣pi′​−ci​∣变小111),


代码:

方法一:

#include <cstdio>
#include <cmath>using namespace std;
const int MAXN = 200;
double c[MAXN], temp[MAXN], ans[MAXN];
int flag[MAXN];
bool low[MAXN];int main() {int T;scanf("%d", &T);while (T--) {int n, k, r;scanf("%d %d %d", &n, &k, &r);for (int i = 0; i < k; i++) scanf("%lf", &c[i]);for (int i = 0; i < n; i++) {double sum = 0, tk = k;for (int j = 0; j < k; j++) {scanf("%lf", &temp[j]);temp[j] -= c[j];flag[j] = (temp[j] >= 0 ? 1 : -1);temp[j] = fabs(temp[j]);sum += temp[j];low[j] = false;}while (true) {bool tFlag = true;for (int j = 0; j < k; j++) {if (low[j]) continue;ans[j] = (r - sum) / tk + temp[j];if (ans[j] < 0) {ans[j] = 0, low[j] = true, tk--, tFlag = false, sum -= temp[j];break;}}if (tFlag) break;}for (int j = 0; j < k; j++)printf("%.5f ", flag[j] * ans[j] + c[j]);putchar('\n');}}return 0;
}

方法二:(贪心)

先挖坑,后填。

[2018北京ICPC网络赛H] HihoCoder - 1835 K-Dimensional Foil II(点到平面的距离)相关推荐

  1. (每日一题)2016 北京ICPC网络赛G hihocoder 1388 (中国剩余定理 + NTT)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Weblink https://vjudge.net/problem/HihoCoder-1388 P ...

  2. 2014 ACM/ICPC 北京赛区网络赛解题报告汇总

    首页 算法竞赛» 信息聚合 ONLINE JUDGE 书刊杂志 BLOG» 新闻故事» 招聘信息» 投稿须知 2014 ACM/ICPC 北京赛区网络赛解题报告汇总 九月 21st, 2014 | P ...

  3. 2019 ICPC 南昌网络赛 H. The Nth Item

    2019 ICPC 南昌网络赛 H. The Nth Item 题目大意:已知一个数列F(n): F(0)=0,F(1)=1 F(n)=3∗F(n−1)+2∗F(n−2),(n≥2) ​ 给你一个操作 ...

  4. ACM-ICPC 2018 北京赛区网络预赛(A B C D H)

    ACM-ICPC 2018 北京赛区网络预赛(A B C D H) 自闭症患者 A. Saving Tang Monk II 题目链接 题面: 划掉 题意: 大搜索,队友写的 思路: 大搜索,队友写的 ...

  5. ICPC网络赛第二场G Limit

    ICPC网络赛第二场G Limit 题意: 给你数组a和b,求解: 1≤n≤100000,−100≤ai,bi≤100,0≤t≤5.1≤n≤100000,−100≤a_{i},b_{i}≤100,0≤ ...

  6. icpc网络赛第二场K Meal

    icpc网络赛第二场K Meal 题意: 有n个人,n个菜, 现在n个人轮流吃菜,起初S中有n个菜,第i个人会在还没拿走的菜中随机选一个,拿走第j个菜的概率为ai,j∑k∈Sai,k\frac{a_{ ...

  7. J Red-Black Paths(ICPC网络赛第一场)

    J Red-Black Paths(ICPC网络赛第一场) 题意: 有n个点,m次操作,有三种操作: 1 u v:从u向v建一个有向边 2 u:将点u染成红色 3 u: 将点u染成黑色 4 查询最新生 ...

  8. 2018 ACM-ICPC南京网络赛 Magical Girl Haze(分层最短路)

    2018 ACM-ICPC南京网络赛 Magical Girl Haze There are NN cities in the country, and MM directional roads fr ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

    Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...

最新文章

  1. 一口一个,超灵活的Python迷你项目
  2. 域名买了,DNS解析怎么选?一键按需定制解析服务不是梦!
  3. PyCharm修改镜像源无用?
  4. java 消息机制 ActiveMQ入门实例
  5. WebDev.WebServer 学习
  6. Java并发编程实战_真香!阿里P8耗时半年著作660页Java高并发与网络编程实战总结...
  7. 西门子rwd68温控器说明书_西门子RWD68说明书
  8. Android学习之碎片与活动之间的通信
  9. phabricator mysql_Phabricator服务的搭建
  10. 华为Android10版怎么截屏,安卓手机截图方法 华为手机如何截图 - 云骑士一键重装系统...
  11. 【note】Java程序设计基础第五版(上)
  12. IntelliJ IDEA :解决idea导入项目爆红
  13. iframe 父页面 子页面互相取值调用
  14. PTES执行内容思维导图
  15. Git在windows和linux安装教程
  16. 在 VSLAM 的后端优化中的重投影误差的雅可比计算详细推导
  17. Java11实现X509TrustManager报错SSLHandshakeException分析及解决办法
  18. 保姆级教学——Python+Pygame怎么实现吃豆豆游戏
  19. 降压(Buck)变换电路设计原理、参数取值及MATLAB仿真实验
  20. 计算机主机外设接口,计算机机系统中,所有外设都必须通过接口连接到主机上。...

热门文章

  1. 使用python将PDF转word
  2. 外卖中常见的饮料名称大全
  3. windows基于face_recognition实现人脸识别
  4. js函数式编程最佳实践 - 持续更新
  5. 阿里妈妈2018的DIN DIEN
  6. 将字符串中的前导*号全部删除,中间和尾部的*号不删除
  7. 这 27 位,为什么能成为马斯克的“左膀”与“右臂”?
  8. gateway网关默认访问规则
  9. 开源BI报表及OLAP多维分析平台OPENI(二)—搭建Eclipse下的Openi开发环境
  10. GSEA富集分析:从概念理解到界面实操