結果
| 問題 |
No.731 等差数列がだいすき
|
| コンテスト | |
| ユーザー |
SugarDragon5
|
| 提出日時 | 2018-09-07 21:47:35 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,693 bytes |
| コンパイル時間 | 1,454 ms |
| コンパイル使用メモリ | 170,660 KB |
| 実行使用メモリ | 17,216 KB |
| 最終ジャッジ日時 | 2024-11-29 12:29:58 |
| 合計ジャッジ時間 | 42,621 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 3 TLE * 15 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,n,m) for(int i=(n);i<(m);i++)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) for(int i=(n);i>=0;i--)
#define all(vec) vec.begin(),vec.end()
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using P=pair<int,int>;
using PP=pair<int,P>;
using Pl=pair<ll,ll>;
using PPl=pair<ll,Pl>;
using vs=vector<string>;
#define fi first
#define se second
#define pb push_back
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
const ll MOD=1000000007LL;
const int INF=1<<30;
const ll LINF=1LL<<60;
using dP=pair<double,double>;
double g(const vector<double>& vec,double a,double b){
double res=0;
REP(i,vec.size()){
res+=pow(vec[i]-(a*i+b),2);
}
return res;
}
const double lb=-1e8,ub=1e8;
const int N=1000;
dP f(const vector<double>& vec,double x){
double l=lb,r=ub;
REP(_,N){
double a=(l*2+r)/3;
double b=(l+r*2)/3;
double p=g(vec,x,a);
double q=g(vec,x,b);
if(p>q){
l=a;
}else{
r=b;
}
}
return make_pair(g(vec,x,l),l);
}
int main(){
int n;
cin>>n;
vector<double> vec(n);
REP(i,n){
cin>>vec[i];
}
double l=lb,r=ub;
REP(_,N){
double a=(l*2+r)/3;
double b=(l+r*2)/3;
dP p=f(vec,a);
dP q=f(vec,b);
if(p>q){
l=a;
}else{
r=b;
}
}
dP ans=f(vec,l);
cout<<fixed<<setprecision(10)<<ans.se<<" "<<l<<endl<<ans.fi<<endl;
return 0;
}
SugarDragon5