当前位置:网站首页>【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法
2022-07-28 02:19:00 【宇智波一打七~】
题目链接
题面



题意:
给你一个长度为n的序列a,对于每一个元素,你可以更改其为任何值,但是代价为他们差值的平方,问你把这个序列改成等差数列的最小花费是多少
分析:
其实如果看这种最小值的题的话很容易想到的就是三分,因为三分他的函数曲线是呈二次函数型的,有一个极值,所以说可以用二次函数做,就是三分等差d,然后看左边三分之一处的d值代表的最小花费和右边三分之一处的最小花费来作比较,这个是三分的常用思路,然后在三分的判断函数里,是一个二次函数的曲线,只需要找到最值就行,但是这个精度卡的特别死,不能直接乘起来,要慢慢加起来,这也算是一种技巧把,那么看一下三分极值的代码:
#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;
}
最小二乘法
等差数列可以看作是一条直线,然后相当于线性拟合,就把高中学过的最小二乘法给搬过来就行,但是注意要用那个乘法少的公式,那样损失的精度会小,下面请看代码:
#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;
}
边栏推荐
- Oracle basicfile lob field space recycling shrink space doubts
- CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
- Games101 review: ray tracing
- Qt官方示例:Fridge Magnets Example(冰箱贴)
- OA项目之我的审批(会议查询&会议签字)
- RTSP/Onvif协议EasyNVR视频平台一键升级方案的开发设计逻辑
- Note that these regions cannot take the NPDP exam in July
- 从硬件编程到软件平台的ci/cd
- 别再用 offset 和 limit 分页了,性能太差!
- style=“width: ___“ VS width=“___“
猜你喜欢

app 自动化 环境搭建(一)

Record of a cross domain problem

行业洞察 | 语音识别真的超过人耳朵了吗?

Data center construction (III): introduction to data center architecture
[email protected] Annotation usage"/>[email protected] Annotation usage

Interview experience: first tier cities move bricks and face software testing posts. 5000 is enough

分布式事务——Senta(一)

“29岁,普通功能测试,我是如何在一周内拿到5份Offer的?”

Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!

Explanation of CNN circular training | pytorch series (XXII)
随机推荐
超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)
Data Lake: flume, a massive log collection engine
[ACNOI2022]总差一步
蓝桥杯原题
数据湖:数据库数据迁移工具Sqoop
JS event object offsetx/y clientx y pagex y
Eigenvalues and eigenvectors
数据中台夯实数据基础
Es6.--promise, task queue and event cycle
CNN循环训练的解释 | PyTorch系列(二十二)
NPDP考生!7月31号考试要求在这里看!
Pytest the best testing framework
别再用 offset 和 limit 分页了,性能太差!
Pytorch 相关-梯度回传
注意,这些地区不能参加7月NPDP考试
Yiwen teaches you to distinguish between continuous integration, continuous delivery and continuous deployment
数据湖:海量日志采集引擎Flume
Trivy [1] tool scanning application
QT专题1:实现一个简易计算器
JS event object 2 e.charcode character code e.keycode key code box moves up, down, left and right