当前位置:网站首页>[2022 Niuke Game 2 J question link with arithmetic progress] three part set three part / three part extreme value / linear equation fitting least square method
[2022 Niuke Game 2 J question link with arithmetic progress] three part set three part / three part extreme value / linear equation fitting least square method
2022-07-28 03:12:00 【Yuzhibo one dozen seven~】
Topic link
Topic



The question :
Give you a length of n Sequence a, For each element , You can change it to any value , But the cost is the square of their difference , What is the minimum cost of changing this sequence into an arithmetic sequence
analysis :
In fact, if you look at this minimum value problem, it is easy to think of three points , Because the curve of his function is quadratic , There is an extreme value , So we can use quadratic function to do , It's equal difference of three points d, Then look at the left third d Value represents the minimum cost and the right third of the minimum cost to compare , This is a common idea of three points , Then in the judgment function of three points , Is the curve of a quadratic function , Just find the best value , But this precision card is particularly dead , You can't multiply it directly , Add it up slowly , This is also a skill , Then take a look at the code of the extreme value of three points :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
#define double long double
const int N=1e6+10;
int n;
double a[N],eps=1e-10;
double c[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 read(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::read;
double cal(double d)
{
double ans=0,b=0;
for(int i=1;i<=n;i++)
{
b += a[i]-(i-1)*d;
}
b /= n;
for(int i=1;i<=n;i++){
double t = a[i]-(i-1)*d;
ans += (t-b)*(t-b);
}
return ans;
}
int main()
{
int T;
cin>>T;
while(T--)
{
n=read();
for(int i=1;i<=n;i++) a[i]=read();
double l=-1e9,r=1e9;
while(fabs(r-l)>eps)
{
double lmid=l+(r-l)/3,rmid=r-(r-l)/3;
if(cal(lmid)>cal(rmid)) l=lmid;
else r=rmid;
}
printf("%.10Lf\n",cal(l));
}
return 0;
}
Least square method
The arithmetic sequence can be regarded as a straight line , Then it is equivalent to linear fitting , Just move the least square method you learned in high school , But pay attention to the formula with less multiplication , Then the loss of accuracy will be small , Now look at the code :
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
#define double long double
const int N=1e6+10;
int n;
double a[N];
int read() {
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*f;
}
int main()
{
int T;
cin>>T;
while(T--)
{
n=read();
double x=(n+1)/2.0;
double y=0,t=0,tx=0;
for(int i=1;i<=n;i++)
{
a[i]=read();
y+=a[i];
t+=i*a[i];
tx+=i*i;
}
y/=n;
double k=0;
for(int i=1;i<=n;i++) k+=(i-x)*(a[i]-y);
double ttt=0;
for(int i=1;i<=n;i++) ttt+=(x-i)*(x-i);
k/=ttt;
double b=y-k*x;
double ans=0;
double tt=k+b;
for(int i=1;i<=n;i++)
{
ans+=(a[i]-tt)*(a[i]-tt);
tt+=k;
}
printf("%.10Lf\n",ans);
}
return 0;
}
边栏推荐
- WEB安全基础 - - -命令执行漏洞
- 决策树与随机森林学习笔记(1)
- @The function of valid (cascade verification) and the explanation of common constraint annotations
- Development and design logic of rtsp/onvif protocol easynvr video platform one click upgrade scheme
- C#实现弹出一个对话框的同时,后面的form不可用
- 分布式 session 的4个解决方案,你觉得哪个最好?
- 每日刷题巩固知识
- Niuke-top101-bm340
- Trivy [1] tool scanning application
- 方案分享 | 高手云集 共同探索重口音AI语音识别
猜你喜欢
随机推荐
QT专题1:实现一个简易计算器
[stream] basic knowledge of stream
决策树与随机森林学习笔记(1)
Note that these regions cannot take the NPDP exam in July
openGauss源代码,用什么IDE工具管理、编辑、调试?
Opengauss source code, what ide tools are used to manage, edit and debug?
My approval of OA project (meeting inquiry & meeting signature)
微服务架构统一安全认证设计与实践
会议OA项目之我的审批&&签字功能
嵌入式分享合集22
行业洞察 | 语音识别真的超过人耳朵了吗?
嵌入式开发:提示和技巧——用C进行防御性编程的最佳实践
Design of the multi live architecture in different places of the king glory mall
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
社恐适合什么工作?能做自媒体吗?
Embedded sharing collection 22
注意,这些地区不能参加7月NPDP考试
OA项目之我的审批(会议查询&会议签字)
JS 事件对象2 e.charcode字符码 e.keyCode键码 盒子上下左右移动
Day 8 of DL







[email protected]注解使用"/>
