ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1047. Simple Calculations

My solution
Posted by Gerasim Petrov Velchev 31 May 2009 15:41
It's my AC solution. I hope it will help you...
Idea: binary searching a[1]. Be carefull with epsilons!


#include<iostream>
#include<cmath>
using namespace std;
int N;
const double eps=1e-9;
double a[3010],c[3010],x;
void solve()
{
for(int i=2;i<=N;i++)a[i]=2*(a[i-1]+c[i-1])-a[i-2];
x=2*(a[N]+c[N])-a[N-1];
}
int main()
{
scanf("%d",&N);
scanf("%lf%lf",&a[0],&a[N+1]);
for(int i=1;i<=N;i++)cin>>c[i];
double l=-2000.000,r=2000.00;
while(l-r<eps)
{
double mid=(l+r)/2.0;
a[1]=mid;
solve();
if(fabs(a[N+1]-x)<eps){printf("%.2lf\n",mid);break;}
else if(a[N+1]-x<eps)r=mid;else l=mid;
}
system("pause");
return 0;
}