One day, CYLL found an interesting piece of commercial from newspaper: the Cyber-restaurant was offering a kind of “Lunch Special” which was said that one could “buy one get two for free”. That is, if you buy one of the dishes on their menu, denoted by d​i
​​ with price pi, you may get the two neighboring dishes di−1 and di+1 for free! If you pick up d1, then you may get d2and the last one d​n for free, and if you choose the last one d​n​​ , you may get d​n−1​​ and d​1​​ for free.

However, after investigation CYLL realized that there was no free lunch at all. The price p​i
​​ of the i-th dish was actually calculated by adding up twice the cost c​i
​​ of the dish and half of the costs of the two “free” dishes. Now given all the prices on the menu, you are asked to help CYLL find the cost of each of the dishes.

Format of function:
void Price( int n, double p[] );
where int n satisfies that 2< n ≤10000; double p[] is passed into the function as an array of prices p​,i and then will be returned storing the costs of the dishes.

Sample program of judge:

#include <stdio.h>#define Max_size 10000 /* max number of dishes */void Price( int n, double p[] );int main()
{int n, i;double p[Max_size];scanf("%d", &n);for (i=0; i<n; i++) scanf("%lf", &p[i]);Price(n, p);for (i=0; i<n; i++)printf("%.2f ", p[i]);printf("\n");return 0;
}/* Your function will be put here */

Sample Input:
12 23.64 17.39 12.77 16.62 10.67 14.85 12.68 26.90 28.30 15.59 37.99 23.18
Sample Output:
9.20 5.58 3.24 7.00 1.99 6.36 2.25 10.01 11.52 0.50 17.65 4.88

尝试的方法:

高斯法:只能过前两个点,原因是内存不允许定义double * 10001 * 10001的数组

追赶法(?):查了才知道有这个怪异的名字。主要想法就是x2x_2x2​,…xn−1x_{n-1}xn−1​都用x1x_1x1​和xnx_nxn​表示,然后带到最后一个方程里解。
只能过前两个点,原因是随着迭代次数增加,x1x_1x1​,xnx_nxn​的系数和常数项都会变的很大,超出double范围。(主要是因为这个矩阵的元素都是正的)

参数法+LU分解:
解法:
求解:[20.50...000.50.520.5...000...........................0.520.5...........................000...0.520.50.500...00.52][x1x2x3.........xn]=[p1p2p3.........pn]\begin{bmatrix}2&0.5&0&...&0&0&0.5\\\\0.5&2&0.5&...&0&0&0\\\\...&...&...&...&...&...&...\\\\...&...&0.5&2&0.5&...&...\\\\...&...&...&...&...&...&...\\\\0&0&0&...&0.5&2&0.5\\\\0.5&0&0&...&0&0.5&2\end{bmatrix}\begin{bmatrix}x_1\\\\x_2\\\\x_3\\\\...\\\\...\\\\...\\\\x_n\end{bmatrix}=\begin{bmatrix}p_1\\\\p_2\\\\p_3\\\\...\\\\...\\\\...\\\\p_{n}\end{bmatrix}⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​20.5.........00.5​0.52.........00​00.5...0.5...00​.........2.........​00...0.5...0.50​00.........20.5​0.50.........0.52​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​x1​x2​x3​.........xn​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​p1​p2​p3​.........pn​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​
不妨设原矩阵为A,设左上方n-1阶矩阵为B,设c=[0.5,0,0,0...,0.5]Tc=[0.5,0,0,0...,0.5]^Tc=[0.5,0,0,0...,0.5]T
则有如下关系:
A=[BccT2]A=\begin{bmatrix}B&c\\c^T&2\end{bmatrix}A=[BcT​c2​]
代入方程可得
[B[x1x2...xn−1]+cxncT[x1x2...xn−1]+2xn]=[[p1p2...pn−1]pn]\begin{bmatrix}B\begin{bmatrix}x_1\\x_2\\...\\x_{n-1}\end{bmatrix}+cx_{n}\\\\c^T\begin{bmatrix}x_1\\x_2\\...\\x_{n-1}\end{bmatrix}+2x_n\end{bmatrix}=\begin{bmatrix}\begin{bmatrix}p_1\\\\p_2\\\\...\\\\p_{n-1}\end{bmatrix}\\\\p_n\end{bmatrix}⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​B⎣⎢⎢⎡​x1​x2​...xn−1​​⎦⎥⎥⎤​+cxn​cT⎣⎢⎢⎡​x1​x2​...xn−1​​⎦⎥⎥⎤​+2xn​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡​p1​p2​...pn−1​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤​pn​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​
取上方方块阵建立等式
不妨设xi=uixn+vi,i=1,2,...,nx_i=u_ix_n+v_i,i=1,2,...,nxi​=ui​xn​+vi​,i=1,2,...,n代入等式
B(uxn+v)+cxn=PB(ux_n+v)+cx_n=PB(uxn​+v)+cxn​=P
Bv=PBv=PBv=P
Bu+c=0Bu+c=0Bu+c=0
就把原方程求解转化为解两个n-1阶三对角矩阵
[20.50...0000.520.5...000...........................0.520.5...........................000...0.520.5000...00.52][u1u2u3.........un−1]=[−0.50...0...0−0.5]\begin{bmatrix}2&0.5&0&...&0&0&0\\\\0.5&2&0.5&...&0&0&0\\\\...&...&...&...&...&...&...\\\\...&...&0.5&2&0.5&...&...\\\\...&...&...&...&...&...&...\\\\0&0&0&...&0.5&2&0.5\\\\0&0&0&...&0&0.5&2\end{bmatrix}\begin{bmatrix}u_1\\\\u_2\\\\u_3\\\\...\\\\...\\\\...\\\\u_{n-1}\end{bmatrix}=\begin{bmatrix}-0.5\\\\0\\\\...\\\\0\\\\...\\\\0\\\\-0.5\end{bmatrix}⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​20.5.........00​0.52.........00​00.5...0.5...00​.........2.........​00...0.5...0.50​00.........20.5​00.........0.52​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​u1​u2​u3​.........un−1​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​−0.50...0...0−0.5​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​
[20.50...0000.520.5...000...........................0.520.5...........................000...0.520.5000...00.52][v1v2v3.........vn−1]=[p1p2p3.........pn−1]\begin{bmatrix}2&0.5&0&...&0&0&0\\\\0.5&2&0.5&...&0&0&0\\\\...&...&...&...&...&...&...\\\\...&...&0.5&2&0.5&...&...\\\\...&...&...&...&...&...&...\\\\0&0&0&...&0.5&2&0.5\\\\0&0&0&...&0&0.5&2\end{bmatrix}\begin{bmatrix}v_1\\\\v_2\\\\v_3\\\\...\\\\...\\\\...\\\\v_{n-1}\end{bmatrix}=\begin{bmatrix}p_1\\\\p_2\\\\p_3\\\\...\\\\...\\\\...\\\\p_{n-1}\end{bmatrix}⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​20.5.........00​0.52.........00​00.5...0.5...00​.........2.........​00...0.5...0.50​00.........20.5​00.........0.52​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​v1​v2​v3​.........vn−1​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡​p1​p2​p3​.........pn−1​​⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤​
最后利用
xn=pn−0.5v1−0.5vn−10.5u1+0.5un−1x_n=\frac{p_n-0.5v_1-0.5v_{n-1}}{0.5u_1+0.5u_{n-1}}xn​=0.5u1​+0.5un−1​pn​−0.5v1​−0.5vn−1​​解出xnx_{n}xn​然后迭代求解。
解三对角矩阵:
用Court分解法

上代码

void CF(double* p, int n,double* x){double z[10001];double u[10001]; z[0]=p[0]/2;u[0]=0.25;double l;for(int i=1;i<n-1;i++){l=2-0.5*u[i-1];u[i]=0.5/l;z[i]=(p[i]-0.5*z[i-1])/l;}l=2-0.5*u[n-2];z[n-1]=(p[n-1]-0.5*z[n-2])/l;x[n-1]=z[n-1];for(int i=n-2;i>=0;i--){x[i]=z[i]-u[i]*x[i+1];}
}
void Price( int n, double p[] ){double p1[10001];p1[0]=p1[n-2]=-0.5;for(int i=1;i<n-2;i++) p1[i]=0;double u[10001],v[10001];CF(p1,n-1,u);CF(p,n-1,v);double x[10001];x[n-1]=(p[n-1]-0.5*v[0]-0.5*v[n-2])/(2+0.5*u[0]+0.5*u[n-2]);for(int i=0;i<n-1;i++){x[i]=u[i]*x[n-1]+v[i];}for(int i=0;i<n;i++){p[i]=x[i];}
}

参考:

https://wenku.baidu.com/view/262c84ac941ea76e59fa042c.html#
https://blog.csdn.net/qq_32096491/article/details/78213914


祝大家解题愉快!

6-3 There is No Free Lunch (40分)相关推荐

  1. Java黑皮书课后题第1章:1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里。编写一个程序显示以每小时为多少千米为单位的平均速度值(1英里等于1.6千米)

    Java黑皮书课后题第1章:1.12(以千米计的平均速度) 题目 题目描述 破题 代码块 修改日志 题目 题目描述 1.12(以千米计的平均速度)假设一个跑步者1小时40分35秒跑了24英里.编写一个 ...

  2. 计算机专业比重点线高40多分,这3所211大学,超过一本线40分就可报考,性价比高,值得报考...

    985,211大学是我国的重点大学,与普通大学相比,师资力量和学科力量都很强.所以很多考生之所以在高中期间努力学习,就是为了能考入名校,毕业之后能找一份薪资待遇不错的工作.一般来说,综合实力强的学校录 ...

  3. 高中数学40分怎么办_2019年第35届全国高中数学联赛试题及参考答案

    2019年第35届全国高中数学联赛考试已结束,本文收集整理本次数学联赛的试题和参考答案,以供大家了解参考. 本次数学联赛由全国高中数学联赛组委会统一命题,共分为一试和二试. 一试时间为80分钟,包括8 ...

  4. 高中数学40分怎么办_高二数学不会,准高三该怎么办?40分到高考140如何逆袭?...

    原标题:高二数学不会,准高三该怎么办?40分到高考140如何逆袭? 高二,这个年级是有点尴尬的,适应了高一的学习,感觉高二学习没有了动力,离高考还远,于是有些孩子就开始了放任自己,开始了放弃,殊不知高 ...

  5. 7-3 素数对猜想 (40 分)

    ** 7-3 素数对猜想 (40 分) ** 让我们定义d n 为:d n=p n+1 −p n ,其中p i​ 是第i个素数.显然有d 1 =1,且对于n>1有d n 是偶数."素数 ...

  6. 7-2 数组元素循环右移问题 (40 分)

    ** 7-2 数组元素循环右移问题 (40 分) ** 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A 0 A 1 ⋯ ...

  7. 7-1 图形卡片排序游戏 (40 分)

    ** 7-1 图形卡片排序游戏 (40 分) ** 掌握类的继承.多态性使用方法以及接口的应用.详见[作业指导书2020-OO第07次作业-1指导书V1.0.pdf 输入格式: 首先,在一行上输入一串 ...

  8. 7-5 日期问题面向对象设计(聚合二) (40 分)

    ** 7-5 日期问题面向对象设计(聚合二) (40 分) ** 参考题目7-3的要求,设计如下几个类:DateUtil.Year.Month.Day,其中年.月.日的取值范围依然为:year∈[18 ...

  9. 哈登独得40分保罗复出 火箭主场103:98复仇魔术

    保罗在本场比赛迎来复出. 中新网北京1月28日电 北京时间28日,NBA常规赛继续展开争夺,火箭队在主场迎来魔术队的挑战,本场比赛火箭后卫保罗迎来复出.最终凭借哈登独得40分的表现,火箭在主场103: ...

  10. 高中数学40分怎么办_高一数学考40分还能拯救吗

    高一数学只考40分怎么办,高考是所有的家长及学生都面临的一道严峻的问题,小编整理了相关信息,来看一下! 高一数学考40分还能拯救吗 没有关系,只是你的学习方法有问题.坚持努力纠错,有高三一轮复习帮你补 ...

最新文章

  1. 2019秋第三周学习总结
  2. [导入]php 安全基础 第八章 共享主机 文件系统浏览
  3. 任务调度及远端管理(基于Quartz.net)
  4. 重学java基础第一课:解决大家的疑问
  5. python excel 添加数据_使用pyexcel python在电子表格中添加行数据
  6. Java 算法数字黑洞
  7. 收文处理和发文处理的环节_公文处理,如何提速增效
  8. 常看网页表单数据_数据收集、整理低效繁琐?WPS表单帮你轻松解决
  9. linux重启mysql一直_linux正确重启MySQL的方法
  10. .NET Framework 1.1 中 SmartNavigation 的实现
  11. 综述 | 知识图谱技术综述(下)
  12. 假疫苗事件,错在企业,责任在管理部门
  13. String对象的match方法
  14. 服务器ping不通,但是远程桌面可以连接上
  15. iPhone的备忘录如何进行撤销?
  16. JAVA实现 剑指offer第二版 2
  17. 第一次发,可能不太好,别喷我
  18. 欲罢不能 推荐,刷屏时代如何摆脱行为上瘾
  19. 移动客户端与服务器通信方式一
  20. 使用tesseract识别图片中的文字

热门文章

  1. C# DevExpress组件 - ChartControl图表控件
  2. Python 绘图字体控制 + 文字在图片中的位置调整
  3. 游戏编程之二 windows编程基础
  4. linux行命令测网速,Linux命令行测试网速的方法
  5. 极品抓鸡教程36课笔记
  6. 【操作系统】Unix文件类型有哪些
  7. Linux/windows下java调用lingo
  8. linux卸载wine qq,ubuntu安装wineQQ
  9. 【VS】错误1error LNK1168: 无法打开 F:\C++6\prob\ConsoleApplication1\Debug\ConsoleApplication1.exe 进行写入
  10. 故障:在 Application Log 中出现 ID57860 的 Backup Exec 错误日志