当前位置:网站首页>2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
2022-08-05 00:21:00 【Rain Sure】
题目链接
Link with Arithmetic Progression
题目大意
给定一个数组,Let's find a straight line that fits it,Minimum mean squared error is required.I believe that everyone has learned related knowledge in mathematics class or machine learning class.
题解
T a r g e t = m i n ( ∑ i = 1 n [ a 1 + ( i − 1 ) × d − a i ] 2 ) Target = min(\sum^{n}_{i = 1} [a_1 + (i - 1) \times d - a_i]^2) Target=min(i=1∑n[a1+(i−1)×d−ai]2)
This is the value we are asking for,We need to keep it minimal,We can think about it first a 1 a_1 a1求出来,很明显,随着 a 1 a_1 a1from negative infinity to positive infinity,TargetThe value first falls and then rises,也就是说 a 1 a_1 a1是一个凹函数,Therefore, we can consider using three points a 1 a_1 a1,and then considering a 1 a_1 a1确定的情况下,如何确定 d d d.
We deform the formula:
= [ a 1 + ( i − 1 ) × d ] 2 − 2 × [ a 1 + ( i − 1 ) × d ] × a i + a i 2 = [a_1 + (i - 1)\times d]^2 - 2 \times [a_1 + (i - 1) \times d] \times a_i + {a_i}^2 =[a1+(i−1)×d]2−2×[a1+(i−1)×d]×ai+ai2
然后,将带有 d d d的合并同类项.
= ( i − 1 ) 2 × d 2 + 2 × ( i − 1 ) × ( a 1 − a i ) × d + a 1 2 + a i 2 = (i - 1) ^2 \times d^2 + 2 \times (i - 1) \times (a_1 - a_i) \times d + {a_1}^2 + {a_i}^2 =(i−1)2×d2+2×(i−1)×(a1−ai)×d+a12+ai2
According to the knowledge of quadratic functions,我们可以很轻松的确定 d = − b 2 ∗ ( i − 1 ) 2 d = \frac {-b} {2 * (i - 1) ^ 2} d=2∗(i−1)2−b
然后,就可以进行计算了.
话不多说,上代码:
代码
#include<iostream>
#include<cstring>
#include <cstdio>
#include <cctype>
using namespace std;
#define int long long
#define double long double
const int maxn = 100010;
int w[maxn];
int n;
namespace GTI
{
char gc(void)
{
const int S = 1 << 16;
static char buf[S], *s = buf, *t = buf;
if (s == t) t = buf + fread(s = buf, 1, S, stdin);
if (s == t) return EOF;
return *s++;
}
int gti(void)
{
int a = 0, b = 1, c = gc();
for (; !isdigit(c); c = gc()) b ^= (c == '-');
for (; isdigit(c); c = gc()) a = a * 10 + c - '0';
return b ? a : -a;
}
}
using GTI::gti;
double check(double a1)
{
double a = 0, b = 0;
for(int i = 1; i <= n; i ++){
a += (i - 1) * (i - 1);
b += 2 * (i - 1) * (a1 - w[i]);
}
double d = - b / (2 * a);
double res = 0;
for(int i = 1; i <= n; i ++){
res += (a1 + (i - 1) * d - w[i]) * (a1 + (i - 1) * d - w[i]);
}
return res;
}
signed main()
{
int t; t = gti();
while(t --)
{
n = gti();
for(int i = 1; i <= n; i ++) w[i] = gti();
double l = -1e10, r = 1e10;
while(r - l > 1e-5) {
double len = r - l;
double mid_l = l + len / 3, mid_r = r - len / 3;
if(check(mid_l) >= check(mid_r)) l = mid_l;
else r = mid_r;
}
printf("%.10Lf\n", check(r));
}
return 0;
}
边栏推荐
- 2022杭电多校第三场 K题 Taxi
- QSunSync 七牛云文件同步工具,批量上传
- 【云原生--Kubernetes】调度约束
- oracle创建表空间
- Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
- leetcode:267. 回文排列 II
- leetcode: 266. All Palindromic Permutations
- 【idea】idea配置sql格式化
- 10 种常见的BUG分类
- 怎样进行在不改变主线程执行的时候,进行日志的记录
猜你喜欢
数据类型-整型(C语言)
进程间通信和线程间通信
英特尔WiFi 7产品将于2024年亮相 最高速度可达5.8Gbps
【LeetCode】矩阵模拟相关题目汇总
[LeetCode] Summary of Matrix Simulation Related Topics
Three tips for you to successfully get started with 3D modeling
【LeetCode】Summary of Two Pointer Problems
【LeetCode】滑动窗口题解汇总
找不到DiscoveryClient类型的Bean
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
随机推荐
Essential knowledge for entry-level 3D game modelers
克服项目管理中恐惧心理
Cython
机器学习(公式推导与代码实现)--sklearn机器学习库
How to automatically push my new articles to my fans (very simple, can't learn to hit me)
Chinese and Japanese color style
Redis visual management software Redis Desktop Manager2022
电子行业MES管理系统的主要功能与用途
2022多校第二场 K题 Link with Bracket Sequence I
More than 2022 cattle school training topic Link with the second L Level Editor I
【LeetCode】Summary of Two Pointer Problems
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
在线中文姓名生成工具推荐
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
typeScript - Partially apply a function
Couple Holding Hands [Greedy & Abstract]
tiup uninstall
Three tips for you to successfully get started with 3D modeling
软件测试面试题:什么是软件测试?软件测试的目的与原则?
Mysql_14 存储引擎