当前位置:网站首页>2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
2022 Niuke multi School Game 2 J. link with arithmetic progress (three points + enumeration)
2022-07-28 18:55:00 【Cutele_】
The question
Give a sequence , The element can be changed to any value , The cost is the square of the difference , Ask the minimum cost of adapting it into an isochronous sequence
Ideas
Suppose the previous sequence is a1,a2,a3……
The modified sequence is b1,b2,b3……
The first item is b1, The tolerance is d
Difference value ci=ai-bi
bi=b1+(i-1)d
The answer is c1c1+c2c2+……=
(a1-(b1+(i-1)d))^2+ (a2-(b1+(i-1)d))^2+……
Then simplify it and find out the tolerance d Take it as an unknown number to get a bivariate primary function
Three points d To narrow it down check When calculating the cost
know d in the future
ci=ai-bi=ai-(b1+(i-1)d)
ci+b1 It's all unknown , Put to one side
ci+b1=ai-(i-1)d=di
c1c1+c2c2+c3c3+……
=(d1-b1)^2+ (d2-b1)^ 2+(d3-b1)^2+……
=nb1b1-2(d1+d2+d3+……)b1+(d1 * d1+d2 * d2+d3 * d3+……)
b1 It's an unknown number
amount to ax^2+by+c Find the extreme value , When x=-b/2a Get extreme value at
b1=(-2(d1+d2+d3+……))/(-2n)=(d1+d2+d3+……)/n
And then calculate ci That's it
ci=di-b1
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
long double a[maxn];
long double b[maxn];
long double eps=1e-10;
int n;
long double check(long double d){
long double res=0,sum=0;
for(int i=1;i<=n;i++){
b[i]=(a[i]-(i-1)*d);
sum=sum+b[i];
}
sum/=n;
for(int i=1;i<=n;i++){
res=res+(b[i]-sum)*(b[i]-sum);
}
return res;
}
int main() {
int _;scanf("%d",&_);
while(_--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%Lf",&a[i]);
long double l=-1e10,r=1e10;
while(r-l>eps){
long double mid1=l+(r-l)/3.0,mid2=r-(r-l)/3.0;
if(check(mid1)>check(mid2)) l=mid1;
else r=mid2;
//cout<<_<<" "<<l<<" "<<r<<endl;
}
printf("%.10Lf\n",check(l));
}
return 0;
}
边栏推荐
- Introduction and advanced level of MySQL (9)
- New upgrade! The 2022 white paper on cloud native architecture was released
- EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析
- Ue5 gas learning notes 1.10 prediction
- Zero knowledge proof: zkp with DDH assumption
- N32替换STM32,这些细节别忽略!
- UE4.25 Slate源码解读
- [GXYCTF2019]StrongestMind
- Win11系统svchost.exe一直在下载怎么办?
- 三分钟了解快来新媒体
猜你喜欢

11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?

使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版

Meta Q2财报:营收首次下滑,Metaverse将与苹果竞争

Introduction and advanced MySQL (4)

1.3、链表

Docker builds MySQL master-slave replication

Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version

Easynlp Chinese text and image generation model takes you to become an artist in seconds

行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开

How to break through the bottleneck of professional development for software testing engineers
随机推荐
零知识证明:具有DDH假设的 ZKP
面试官:ThreadLocal使用场景有哪些?内存泄露问题如何避免?
When golang encounters high concurrency seckill
One Hot编码是什么?为什么要用它,什么时候用它?
GC垃圾回收器详解
LVS manual
2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
Gaode map realizes customized small blue dots, customized point markers, drawing polygon / circular areas, and displaying or hiding customized point markers according to the movement of the map
EasyCVR设备离线后无法再次上线该如何解决?
Introduction and advanced level of MySQL (5)
MYSQL入门与进阶(七)
Full analysis of warehouse building on the lake: how to build a lake warehouse integrated data platform | deepnova technology collection series open class phase IV
1.3、链表
配置教程:新版本EasyCVR(v2.5.0)组织结构如何级联到上级平台?
十进制转二进制进阶版(可转化负数以及边界值)
[GXYCTF2019]StrongestMind
历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
不理解模块化、组件化、插件化的区别怎么行?
Log base zap of go language series
三分钟了解快来新媒体