当前位置:网站首页>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;
}
边栏推荐
- EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决
- kotlin:Nothing
- SwiftUI Swift 之正向地理编码与反向地理编码(教程含源码)
- Zero knowledge proof: zkp with DDH assumption
- Ue5 gas learning notes 1.6 skills gameplay ability
- 广告推荐CTR点击率预测实践项目!
- Four years later, Debian finally recaptured the "debian.community" domain name!
- redis优势以及数据结构相关知识
- 1.2、队列
- JVM tuning
猜你喜欢

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

湖上建仓全解析:如何打造湖仓一体数据平台 | DEEPNOVA技术荟系列公开课第四期

Decimal to binary advanced version (can convert negative numbers and boundary values)

MYSQL入门与进阶(六)

Zero knowledge proof: zkp with DDH assumption

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

Special Lecture 6 tree DP learning experience (long-term update)

EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决

AI has changed thousands of industries. How can developers devote themselves to the new "sound" state of AI voice

EasyCVR新版本级联时,下级平台向上传递层级目录显示不全的原因分析
随机推荐
Ue5 gas learning notes 1.2 game Tags
MYSQL入门与进阶(七)
redis持久化之RDB和AOF的区别
Introduction and advanced level of MySQL (10)
2022年中国企业服务产业市场行情
One Hot编码是什么?为什么要用它,什么时候用它?
JVM tuning
When the new version of easycvr is linked at the same level, the subordinate platform passes up the cause analysis of the incomplete display of the hierarchical directory
Open source database innovation in the era of digital economy | the 2022 open atom global open source summit database sub forum was successfully held
Golang concurrent lock
408复习策略(强化阶段)
Introduction and advanced MySQL (III)
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
Ue5 gas learning notes 1.4 attribute set
Ue5 gas learning notes 1.7 task ability tasks
AI has changed thousands of industries. How can developers devote themselves to the new "sound" state of AI voice
1.1、稀疏数组
Why app uses JSON protocol to interact with server: serialization related knowledge
Docker builds MySQL master-slave replication
1.2 queue