【POJ】【2601】Simple calculations

推公式/二分法


  好题!

  题解:http://blog.csdn.net/zck921031/article/details/7690288

  这题明显是一个方程组……可以推公式推出来……

  然而这太繁琐了!发现a[i]是满足单调性的话,我们就可以二分a[1],递推出a[n+1],进行验证……

  思维复杂度比推公式低到不知哪里去了,真是一种优秀的算法(然而我想不到,并没有什么*用……)

 1 Source Code
 2 Problem: 2601        User: sdfzyhy
 3 Memory: 736K        Time: 16MS
 4 Language: G++        Result: Accepted
 5 
 6     Source Code
 7 
 8     //PKUSC 2013 B
 9     //POJ 2601
10     #include<vector>
11     #include<cstdio>
12     #include<cstring>
13     #include<cstdlib>
14     #include<iostream>
15     #include<algorithm>
16     #define rep(i,n) for(int i=0;i<n;++i)
17     #define F(i,j,n) for(int i=j;i<=n;++i)
18     #define D(i,j,n) for(int i=j;i>=n;--i)
19     using namespace std;
20     typedef long long LL;
21     inline int getint(){
22         int r=1,v=0; char ch=getchar();
23         for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-1;
24         for(; isdigit(ch);ch=getchar()) v=v*10-'0'+ch;
25         return r*v;
26     }
27     const int N=3010;
28     const double eps=1e-4;
29     /*******************template********************/
30 
31     int n;
32     double a[N],c[N],ed;
33     inline double check(double x){
34         a[1]=x;
35         F(i,2,n+1) a[i]=2*(a[i-1]+c[i-1])-a[i-2];
36         return a[n+1];
37     }
38     int main(){
39     #ifndef ONLINE_JUDGE
40         freopen("B.in","r",stdin);
41         freopen("B.out","w",stdout);
42     #endif 
43         scanf("%d%lf%lf",&n,&a[0],&ed);
44         F(i,1,n) scanf("%lf",&c[i]);
45         double l=-1000,r=1000,mid;
46         while(r-l>eps){
47             mid=(l+r)/2;
48             if (check(mid)>ed) r=mid;
49             else l=mid;
50         }
51         printf("%.2f\n",l);
52         return 0;
53     }
View Code
Simple calculations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6646   Accepted: 3322

Description

There is a sequence of n+2 elements a0, a1, ..., an+1 (n <= 3000, -1000 <= ai <=1000). It is known that ai = (ai-1 + ai+1)/2 - ci for each i=1, 2, ..., n.
You are given a0, an+1, c1, ... , cn. Write a program which calculates a1.

Input

The first line of an input contains an integer n. The next two lines consist of numbers a0 and an+1 each having two digits after decimal point, and the next n lines contain numbers ci (also with two digits after decimal point), one number per line.

Output

The output file should contain a1 in the same format as a0 and an+1.

Sample Input

1
50.50
25.50
10.15

Sample Output

27.85

Source

[Submit]   [Go Back]   [Status]   [Discuss]

posted @ 2015-05-27 21:36  Tunix  阅读(202)  评论(0编辑  收藏  举报