QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 2048 MB Total points: 100

#5083. Linear Regression

统计

Chansu 是 ICPC 大学的一名研究生,正在实验室攻读硕士学位。他的研究课题是揭示特定群体 G 中个人的肥胖程度与年收入之间的关系。

Chansu 从 G 群体中的 $n$ 个人那里收集了形式为 $(x_i, y_i)$ 的数据,其中 $x_i$ 和 $y_i$ 分别表示第 $i$ 个人的肥胖指数和年收入,并提出了一个显而易见的假设:

G 群体中个人的肥胖程度与年收入之间存在线性相关性。

为了证明他的假设,Chansu 试图找到一个具有实系数的最优线性函数 $f^*(x)$,使得相对于所收集数据的误差最小化。更具体地说,函数 $f$ 相对于数据的误差定义为所有 $i = 1, \dots, n$ 的 $|y_i - f(x_i)|$ 的最大值。

然而,结果令人失望,因为最优函数 $f^*(x)$ 的误差大得惊人。这意味着他的假设无法通过这种方式得到证明。

Chansu 试图找出误差巨大的原因。有一天,他将数据 $(x_i, y_i)$ 作为坐标平面上的点绘制出来,发现有少数 $k$ 个点异常远离其他点,因此在移除这些点后,最优函数的误差可以大幅降低。

作为 Chansu 的朋友,你很乐意帮助他。请编写一个程序,在给定 $k$ 的情况下,从给定的数据 $\{(x_1, y_1), \dots, (x_n, y_n)\}$ 中移除 $k$ 个值,找到使误差最小化的最优线性函数,并输出该误差值。

输入格式

程序从标准输入读取数据。输入的第一行包含两个整数 $n$ 和 $k$ ($1 \le n \le 50,000$, $0 \le k \le \min\{\frac{n}{2}, 300\}$),其中 $n$ 是收集的数据值数量。在接下来的 $n$ 行中,每行给出两个整数 $x_i$ 和 $y_i$ ($-10^9 \le x_i, y_i \le 10^9$),表示第 $i$ 个数据值 $(x_i, y_i)$。你可以假设在坐标平面上绘制这些点时,没有三点共线。

输出格式

程序向标准输出写入数据。仅打印一行。该行应包含一个实数 $z$,表示在移除 $k$ 个值后,线性函数相对于剩余数据的最小可能误差。你的输出 $z$ 应包含整数部分、小数点和小数部分,如果满足 $a - 10^{-6} < z < a + 10^{-6}$(其中 $a$ 为精确答案),则判定为“正确”。

样例

输入 1

6 0
0 0
5 -1
9 6
3 0
4 2
3 1

输出 1

2.166667

输入 2

6 1
0 0
5 -1
9 6
3 0
4 2
3 1

输出 2

1.000000

输入 3

6 2
0 0
5 -1
9 6
3 0
4 2
3 1

输出 3

0.500000

输入 4

6 3
0 0
5 -1
9 6
3 0
4 2
3 1

输出 4

0.083333

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.