URAL - 1828 Approximation by a Progression(最小二乘法)
2015-04-21 01:28
423 查看
Approximation by a Progression
Submit Status
Description
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 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
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平。按公式求就好了。
Time Limit: 500MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
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 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 |
#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; } }
相关文章推荐
- URAL 1828. Approximation by a Progression 数学
- URAL 1915 Titan Ruins: Reconstruction of Bygones(思路)
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra迪杰斯特拉算法)
- URAL 1205 By the Underground or by Foot?
- Ural 1915 - Reconstruction of Bygones 模拟
- The issus in Age Progression/Regression by Conditional Adversarial Autoencoder (CAAE)
- Reinforcement Learning_By David Silver笔记六: Value Function Approximation
- The Approximation of One Matrix by Another of Lower Rank
- URAL 1205 By the Underground or by Foot?(SPFA)
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
- ural 1915. Titan Ruins: Reconstruction of Bygones
- URAL 1915 Titan Ruins: Reconstruction of Bygones (栈)
- URAL 1658 Sum of Digits (DP) #by Plato
- Enterprise Library Step By Step系列(十二):异常处理应用程序块——进阶篇
- poj1828
- uva 550 Multiplying by Rotation(模拟)
- Opencv step by step - ROI
- 机器学习 - Clustering by fast search and find of density peaks
- Connection reset by peer: socket write error
- 使用CruiseControl创建你的Build Grid - CruiseControl Step by Step (5)