2593 今天我D了个P

第一次AC了一道有点难度的题

 

好好写一回解题报告

 

Max Sequence
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 8026 Accepted: 3230

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N).

You should output S.

Input

The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

5
-5 9 -5 11 20
0

Sample Output

40

Source

POJ Monthly--2005.08.28,Li Haoyuan
此题要求一个已知整数序列中两个子序列和的最大值
和2479如出一辙 数据量有所变化 因此要在堆上用new来分配数组了
研究了两天的DP,翻来覆去终于有了点头绪。
所谓DP,只是一种思想,不要去找所谓的万能公式。
一个大问题的最优解,如果可以用小问题的最优解通过一个条件式表达出来(DP公式),就可以用DP的思想进行解决。
具体怎么说呢,还是多做些题体会吧。
代码:
关于这道题中的求最大和子序列的问题
今天又看了另一位的代码 觉得他的代码能非常好地体现解题思想,遂转来
就和我以前说的一样(在哪说的忘记了)。。
用b来记录一定要将子序列延续到当前位置(也就是序列头浮动,序列尾固定),也就是一定要将序列继续下去的话,当前位置和其之前的序列一共可以给最大和贡献多少。
而我认为 dp也是体现在这里的。这才是dp函数。
拉格朗日插值: x P error ________________________ -4.9000 1.8118 0.1772E+01 -4.8000 2.1406 0.2099E+01 -4.7000 1.7951 0.1752E+01 -4.6000 1.2266 0.1181E+01 -4.5000 0.6758 0.6288E+00 -4.4000 0.2497 0.2006E+00 -4.3000 -0.0242 0.7547E-01 -4.2000 -0.1606 0.2143E+00 -4.1000 -0.1931 0.2492E+00 -4.0000 -0.1589 0.2178E+00 -3.9000 -0.0915 0.1531E+00 -3.8000 -0.0160 0.8078E-01 -3.7000 0.0507 0.1741E-01 -3.6000 0.0998 0.2817E-01 -3.5000 0.1289 0.5342E-01 -3.4000 0.1398 0.6021E-01 -3.3000 0.1371 0.5296E-01 -3.2000 0.1261 0.3713E-01 -3.1000 0.1123 0.1802E-01 -3.0000 0.1000 0.0000E+00 -2.9000 0.0924 0.1388E-01 -2.8000 0.0911 0.2202E-01 -2.7000 0.0965 0.2417E-01 -2.6000 0.1077 0.2112E-01 -2.5000 0.1236 0.1438E-01 -2.4000 0.1421 0.5809E-02 -2.3000 0.1617 0.2743E-02 -2.2000 0.1809 0.9701E-02 -2.1000 0.1988 0.1397E-01 -2.0000 0.2150 0.1502E-01 -1.9000 0.2299 0.1294E-01 -1.8000 0.2442 0.8330E-02 -1.7000 0.2593 0.2182E-02 -1.6000 0.2766 0.4279E-02 -1.5000 0.2979 0.9806E-02 -1.4000 0.3245 0.1332E-01 -1.3000 0.3577 0.1409E-01 -1.2000 0.3980 0.1184E-01 -1.1000 0.4456 0.6871E-02 -1.0000 0.5000 0.0000E+00 -0.9000 0.5600 0.7488E-02 -0.8000 0.6238 0.1403E-01 -0.7000 0.6892 0.1808E-01 -0.6000 0.7537 0.1838E-01 -0.5000 0.8144 0.1442E-01 -0.4000 0.8687 0.6619E-02 -0.3000 0.9139 0.3496E-02 -0.2000 0.9480 0.1358E-01 -0.1000 0.9691 0.2101E-01 0.0000 0.9762 0.2375E-01 0.1000 0.9691 0.2101E-01 0.2000 0.9480 0.1358E-01 0.3000 0.9139 0.3496E-02 0.4000 0.8687 0.6619E-02 0.5000 0.8144 0.1442E-01 0.6000 0.7537 0.1838E-01 0.7000 0.6892 0.1808E-01 0.8000 0.6238 0.1403E-01 0.9000 0.5600 0.7488E-02 1.0000 0.5000 0.0000E+00 1.1000 0.4456 0.6871E-02 1.2000 0.3980 0.1184E-01 1.3000 0.3577 0.1409E-01 1.4000 0.3245 0.1332E-01 1.5000 0.2979 0.9806E-02 1.6000 0.2766 0.4279E-02 1.7000 0.2593 0.2182E-02 1.8000 0.2442 0.8330E-02 1.9000 0.2299 0.1294E-01 2.0000 0.2150 0.1502E-01 2.1000 0.1988 0.1397E-01 2.2000 0.1809 0.9701E-02 2.3000 0.1617 0.2743E-02 2.4000 0.1421 0.5809E-02 2.5000 0.1236 0.1438E-01 2.6000 0.1077 0.2112E-01 2.7000 0.0965 0.2417E-01 2.8000 0.0911 0.2202E-01 2.9000 0.0924 0.1388E-01 3.0000 0.1000 0.0000E+00 3.1000 0.1123 0.1802E-01 3.2000 0.1261 0.3713E-01 3.3000 0.1371 0.5296E-01 3.4000 0.1398 0.6021E-01 3.5000 0.1289 0.5342E-01 3.6000 0.0998 0.2817E-01 3.7000 0.0507 0.1741E-01 3.8000 -0.0160 0.8078E-01 3.9000 -0.0915 0.1531E+00 4.0000 -0.1589 0.2178E+00 4.1000 -0.1931 0.2492E+00 4.2000 -0.1606 0.2143E+00 4.3000 -0.0242 0.7547E-01 4.4000 0.2497 0.2006E+00 4.5000 0.6758 0.6288E+00 4.6000 1.2266 0.1181E+01 4.7000 1.7951 0.1752E+01 4.8000 2.1406 0.2099E+01 4.9000 1.8118 0.1772E+01 5.0000 0.0385 0.0000E+00 module La_mod implicit none contains subroutine La(n,x,xknow,fx,P) real*8,intent(in)::x integer::n real*8::xknow(0:n),fx(0:n) real*8,intent(out)::P integer::i,j real*8::t1,t2 P=0.0d0 do i=0,n t1=1.0d0 do j=0,n if(i/=j) then t1=t1*(x-xknow(j))/(xknow(i)-xknow(j)) end if end do P=P+fx(i)*t1 end do end subroutine La end module La_mod program main use La_mod implicit none integer,parameter::n=15 real*8::xknow(0:n),fx(0:n) real*8::x,P,Pr real*8::a,b,h integer::i,j,k,m a=-5.0d0 b=5.0d0 h=(b-a)/n print*,"拉格朗日插值:" print*,"x P error" print*,"________________________" do i=0,n xknow(i)=a+i*h fx(i)=1/(1+xknow(i)**2) end do m=101 h=(b-a)/(m-1) do i=1,m-1 x=a+i*h call La(n,x,xknow,fx,P) pr=1.0d0/(1.0d0+x**2) write(*,"(2F10.4,E12.4)")x,P,abs(P-pr) end do read(*,*) end program main
10-23
代码module New_mod implicit none contains subroutine New(n,x0,y0,x,p) integer::n real*8::x0(0:n),y0(0:n),x,p integer::i,j,k real*8::a(0:n,0:n),t1 do i=0,n a(i,0)=y0(i) end do do j=1,n do i=j,n a(i,j)=(a(i,j-1)-a(i-1,j-1))/(x0(i)-x0(i-j)) end do end do p=a(0,0) t1=1.0d0 do i=1,n t1=t1*(x-x0(i-1)) p=p+a(i,i)*t1 end do end subroutine New end module New_mod program main use New_mod implicit none integer,parameter::n=15 real*8::x0(0:n),y0(0:n),x,y,p integer::i,j,k,num real*8::a,b,h a=-5.0d0 b=5.0d0 h=(b-a)/n do i=0,n x0(i)=a+i*h y0(i)=1.0d0/(1.0d0+x0(i)**2) end do num=101 h=(b-a)/(num-1) print*,"x f(x) p error" do i=0,num-1 x=a+i*h y=1.0d0/(1.0d0+x**2) call New(n,x0,y0,x,p) write(*,"(f12.4,3e12.4)") x,y,p,abs(y-p) end do read(*,*) end program main结果: x f(x) p error -5.0000 0.3846E-01 0.3846E-01 0.0000E+00 -4.9000 0.3998E-01 0.1812E+01 0.1772E+01 -4.8000 0.4160E-01 0.2141E+01 0.2099E+01 -4.7000 0.4331E-01 0.1795E+01 0.1752E+01 -4.6000 0.4513E-01 0.1227E+01 0.1181E+01 -4.5000 0.4706E-01 0.6758E+00 0.6288E+00 -4.4000 0.4912E-01 0.2497E+00 0.2006E+00 -4.3000 0.5131E-01 -0.2416E-01 0.7547E-01 -4.2000 0.5365E-01 -0.1606E+00 0.2143E+00 -4.1000 0.5615E-01 -0.1931E+00 0.2492E+00 -4.0000 0.5882E-01 -0.1589E+00 0.2178E+00 -3.9000 0.6169E-01 -0.9145E-01 0.1531E+00 -3.8000 0.6477E-01 -0.1601E-01 0.8078E-01 -3.7000 0.6807E-01 0.5067E-01 0.1741E-01 -3.6000 0.7163E-01 0.9980E-01 0.2817E-01 -3.5000 0.7547E-01 0.1289E+00 0.5342E-01 -3.4000 0.7962E-01 0.1398E+00 0.6021E-01 -3.3000 0.8410E-01 0.1371E+00 0.5296E-01 -3.2000 0.8897E-01 0.1261E+00 0.3713E-01 -3.1000 0.9425E-01 0.1123E+00 0.1802E-01 -3.0000 0.1000E+00 0.1000E+00 0.0000E+00 -2.9000 0.1063E+00 0.9239E-01 0.1388E-01 -2.8000 0.1131E+00 0.9110E-01 0.2202E-01 -2.7000 0.1206E+00 0.9646E-01 0.2417E-01 -2.6000 0.1289E+00 0.1077E+00 0.2112E-01 -2.5000 0.1379E+00 0.1236E+00 0.1438E-01 -2.4000 0.1479E+00 0.1421E+00 0.5809E-02 -2.3000 0.1590E+00 0.1617E+00 0.2743E-02 -2.2000 0.1712E+00 0.1809E+00 0.9701E-02 -2.1000 0.1848E+00 0.1988E+00 0.1397E-01 -2.0000 0.2000E+00 0.2150E+00 0.1502E-01 -1.9000 0.2169E+00 0.2299E+00 0.1294E-01 -1.8000 0.2358E+00 0.2442E+00 0.8330E-02 -1.7000 0.2571E+00 0.2593E+00 0.2182E-02 -1.6000 0.2809E+00 0.2766E+00 0.4279E-02 -1.5000 0.3077E+00 0.2979E+00 0.9806E-02 -1.4000 0.3378E+00 0.3245E+00 0.1332E-01 -1.3000 0.3717E+00 0.3577E+00 0.1409E-01 -1.2000 0.4098E+00 0.3980E+00 0.1184E-01 -1.1000 0.4525E+00 0.4456E+00 0.6871E-02 -1.0000 0.5000E+00 0.5000E+00 0.0000E+00 -0.9000 0.5525E+00 0.5600E+00 0.7488E-02 -0.8000 0.6098E+00 0.6238E+00 0.1403E-01 -0.7000 0.6711E+00 0.6892E+00 0.1808E-01 -0.6000 0.7353E+00 0.7537E+00 0.1838E-01 -0.5000 0.8000E+00 0.8144E+00 0.1442E-01 -0.4000 0.8621E+00 0.8687E+00 0.6619E-02 -0.3000 0.9174E+00 0.9139E+00 0.3496E-02 -0.2000 0.9615E+00 0.9480E+00 0.1358E-01 -0.1000 0.9901E+00 0.9691E+00 0.2101E-01 0.0000 0.1000E+01 0.9762E+00 0.2375E-01 0.1000 0.9901E+00 0.9691E+00 0.2101E-01 0.2000 0.9615E+00 0.9480E+00 0.1358E-01 0.3000 0.9174E+00 0.9139E+00 0.3496E-02 0.4000 0.8621E+00 0.8687E+00 0.6619E-02 0.5000 0.8000E+00 0.8144E+00 0.1442E-01 0.6000 0.7353E+00 0.7537E+00 0.1838E-01 0.7000 0.6711E+00 0.6892E+00 0.1808E-01 0.8000 0.6098E+00 0.6238E+00 0.1403E-01 0.9000 0.5525E+00 0.5600E+00 0.7488E-02 1.0000 0.5000E+00 0.5000E+00 0.4441E-15 1.1000 0.4525E+00 0.4456E+00 0.6871E-02 1.2000 0.4098E+00 0.3980E+00 0.1184E-01 1.3000 0.3717E+00 0.3577E+00 0.1409E-01 1.4000 0.3378E+00 0.3245E+00 0.1332E-01 1.5000 0.3077E+00 0.2979E+00 0.9806E-02 1.6000 0.2809E+00 0.2766E+00 0.4279E-02 1.7000 0.2571E+00 0.2593E+00 0.2182E-02 1.8000 0.2358E+00 0.2442E+00 0.8330E-02 1.9000 0.2169E+00 0.2299E+00 0.1294E-01 2.0000 0.2000E+00 0.2150E+00 0.1502E-01 2.1000 0.1848E+00 0.1988E+00 0.1397E-01 2.2000 0.1712E+00 0.1809E+00 0.9701E-02 2.3000 0.1590E+00 0.1617E+00 0.2743E-02 2.4000 0.1479E+00 0.1421E+00 0.5809E-02 2.5000 0.1379E+00 0.1236E+00 0.1438E-01 2.6000 0.1289E+00 0.1077E+00 0.2112E-01 2.7000 0.1206E+00 0.9646E-01 0.2417E-01 2.8000 0.1131E+00 0.9110E-01 0.2202E-01 2.9000 0.1063E+00 0.9239E-01 0.1388E-01 3.0000 0.1000E+00 0.1000E+00 0.4122E-13 3.1000 0.9425E-01 0.1123E+00 0.1802E-01 3.2000 0.8897E-01 0.1261E+00 0.3713E-01 3.3000 0.8410E-01 0.1371E+00 0.5296E-01 3.4000 0.7962E-01 0.1398E+00 0.6021E-01 3.5000 0.7547E-01 0.1289E+00 0.5342E-01 3.6000 0.7163E-01 0.9980E-01 0.2817E-01 3.7000 0.6807E-01 0.5067E-01 0.1741E-01 3.8000 0.6477E-01 -0.1601E-01 0.8078E-01 3.9000 0.6169E-01 -0.9145E-01 0.1531E+00 4.0000 0.5882E-01 -0.1589E+00 0.2178E+00 4.1000 0.5615E-01 -0.1931E+00 0.2492E+00 4.2000 0.5365E-01 -0.1606E+00 0.2143E+00 4.3000 0.5131E-01 -0.2416E-01 0.7547E-01 4.4000 0.4912E-01 0.2497E+00 0.2006E+00 4.5000 0.4706E-01 0.6758E+00 0.6288E+00 4.6000 0.4513E-01 0.1227E+01 0.1181E+01 4.7000 0.4331E-01 0.1795E+01 0.1752E+01 4.8000 0.4160E-01 0.2141E+01 0.2099E+01 4.9000 0.3998E-01 0.1812E+01 0.1772E+01 5.0000 0.3846E-01 0.3846E-01 0.8408E-12,这结果对么?我用AI的代码,算出来的结果从有效数字第三位开始和我的不一样
最新发布
11-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值