现在的位置: 首页 > 综合 > 正文

POJ 2601 Simple calculations 利用单调性,二分找出答案

2013年09月03日 ⁄ 综合 ⁄ 共 1198字 ⁄ 字号 评论关闭

看完题目后,得到公式a[i]=2*a[i-1]-a[i-2]+2*c[i-1];  YY发现a[n+1]随a[1]增大而增大,那么就对a[1]二分吧,精度1e-4,1A。

Simple calculations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5989   Accepted: 3040

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

 

 

#include <stdio.h>
int n;
const int S=3050;
double a[S],c[S],anp1;
double fun(){
    int i;
    for (i=2;i<=n+1;i++)
        a[i]=2*a[i-1]-a[i-2]+2*c[i-1];
    return a[n+1];
}
int main(){
    int i;
    double l,r,mid,tmp;
    scanf("%d",&n);
    scanf("%lf %lf",&a[0],&anp1);
    for (i=1;i<=n;i++) scanf("%lf",&c[i]);
    l=-1005;r=1005;
    //        a[1]=27.85;fun();printf("%.2lf  ||||| \n",a[2]);
    while (r-l>1e-4){
        mid=(l+r)/2.0;
        a[1]=mid;
        tmp=fun();
        if (a[n+1]>anp1) {r=mid;}else if ( a[n+1]<anp1 ) {l=mid;} else break;
    }
    printf("%.2lf\n",mid);
    return 0;
}

 

抱歉!评论已关闭.