当前位置:网站首页>[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;
}
边栏推荐
- Pytest the best testing framework
- Why is it that when logging in, you clearly use the account information already in the database, but still display "user does not exist"?
- Qt官方示例:Fridge Magnets Example(冰箱贴)
- 行业洞察 | 语音识别真的超过人耳朵了吗?
- Data Lake: flume, a massive log collection engine
- QT topic 1: implementing a simple calculator
- [ACNOI2022]总差一步
- Day 19 of leetcode
- How to authenticate Youxuan database client
- 【stream】stream流基础知识
猜你喜欢

Why is it that when logging in, you clearly use the account information already in the database, but still display "user does not exist"?

What "posture" does JD cloud have to promote industrial digitalization to climb to a "new level"?

基于JSP&Servlet实现的众筹平台系统

汇总了50多场面试,4-6月面经笔记和详解(含核心考点及6家大厂)

智能工业设计软件公司天洑C轮数亿元融资

MySQL index learning

Kubernetes -- Introduction

Is it you who are not suitable for learning programming?

会议OA项目之我的审批&&签字功能

TFX airflow experience
随机推荐
Ah Han's story
Is it you who are not suitable for learning programming?
JS 事件对象 offsetX/Y clientX Y PageX Y
JS event object 2 e.charcode character code e.keycode key code box moves up, down, left and right
What kind of job is social phobia suitable for? Can you do we media?
Full of dry goods, hurry in!!! Easy to master functions in C language
stm32F407-------DSP学习
Ci/cd from hardware programming to software platform
Day 8 of DL
ELS keyboard information
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
分布式 session 的4个解决方案,你觉得哪个最好?
[stream] parallel stream and sequential stream
满满干货赶紧进来!!!轻松掌握C语言中的函数
Trivy [1] tool scanning application
Confusion matrix in CNN | pytorch series (XXIII)
树莓派开发继电器控制灯
牛客-TOP101-BM340
exness:日本物价上涨收入下降,英镑/日元突破 165
P6118 [joi 2019 final] solution to the problem of Zhenzhou City