URAL - 1828 Approximation by a Progression(最小二乘法)

Approximation by a Progression

Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Your are given a sequence of integers a1, …, an. Find an arithmetic progression b1, …, bn for
which the value ∑( ai ? bi2 is minimal. The elements of the progression can be non-integral.

Input

The first line contains the number n of elements in the sequence (2 ≤ n ≤ 10 4). In the second line you are given the integers a1,
…, an; their absolute values do not exceed 10 4.

Output

Output two numbers separated with a space: the first term of the required arithmetic progression and its difference, with an absolute or relative error of at most 10 ?6. It is guaranteed that the answer
is unique for all input data.

Sample Input

input output
4
0 6 10 15
0.400 4.900
4
-2 -2 -2 -2
-2 0

arithmetic progression 等差数列的通式an=a1+(n-1)*d,即an=(a1-d)+n*d 是直线方程的形式,而(i,mi)是分布在直线两边的点,要求直线的 k=d,b=a1-d,于是想到最小二乘法,对Y=kX+b , 有 k=((XY)平--X平*Y平)/((X^2)平--(X平)^2),  b=Y平--kX平。按公式求就好了。

#include<iostream>
#include<iomanip>
using namespace std;
const int MAXN = 10005;
double a[MAXN];
int main()
{
	int n;
	while (cin >> n)
	{
		for (int i = 1; i <= n; i++)
			cin >> a[i];
		double xy=0, x=0, y=0, x2=0;
		for (int i = 1; i <= n; i++)
		{
			xy = xy + a[i] * i;
			x = x + i;
			y = y + a[i];
			x2 = x2 + i*i;
		}
		double k = (xy/n -(x/n)*(y/n)) / (x2/n  - (x/n)*(x/n));
		double b = y / n - k*(x / n);
		double a1 = b + k;
		double d = k;
		cout <<fixed<<setprecision(6)<< a1 << " " <<fixed<<setprecision(6)<<k << endl;
	}
}
时间: 2024-08-02 17:00:33

URAL - 1828 Approximation by a Progression(最小二乘法)的相关文章

URAL 1828. Approximation by a Progression 数学

1828. Approximation by a Progression Time limit: 0.5 second Memory limit: 64 MB Your are given a sequence of integers a1, -, an. Find an arithmetic progression b1, -, bn for which the value ∑(ai ? bi)2 is minimal. The elements of the progression can

最小二乘法的推导证明

1.附加题:推导线性最小二乘法过程 上述式子求解b 时最后一步用到求和性质,事实上 同理可证分子部分,具体请参考<计量经济学导论(第四版)Introductory Econometrics A Modern Approach Fourth Edition · 杰弗里·M·伍德里奇(Jeffrey M. Wooldridge)著>中的附录A 基本数学工具 . 2.赛马问题 问:36匹马,6条跑道,无计时器,最少几次比赛可以选出前3名? 答案是8次,思路如下: (1)把36匹马分成6组,分别进行6

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

HDU 1828 Picture(矩形周长并)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

Partial least squares regression(偏最小二乘法回归)

偏最小二乘法(PLS)是近年来发展起来的一种新的多元统计分析 http://en.wikipedia.org/wiki/Partial_least_squares_regression Partial least squares regression(偏最小二乘法回归),布布扣,bubuko.com

基于 移动最小二乘法(MLS) 的三维数据拟合

项目介绍: 1. 需要预测的数据: 2. 采用的权函数以及形函数: 3. 求解的形函数曲线结果: 4. 算法流程图: 5. 预测结果: x=[234 255 255 76 12];y=[162 242 176 54 55];z=[199 200 57 50 73]; 对应的预测结果为: >> MLS_Output Esti_ux = 53.3651 73.8599 54.2216 5.9668 9.0063 Esti_uy = 43.9818 77.5332 48.3499 5.2517 11

最小二乘法

            1.前言:                    a.本文中主要讲解线性最小二乘的标准形式及求解方法.                    b.在同类问题的求解方法中,RANSAC算法是另一种求解思路,它们各有优点与缺点.                     --当测量数据 x 属于高斯分布(期望为x的真值,或者说期望为0误差符合高斯分布)时,此时应选择最小二乘法求解.                        因为误差服从高斯分布的情况下, 最小二乘法等价于极

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is