結果

問題 No.731 等差数列がだいすき
ユーザー SugarDragon5SugarDragon5
提出日時 2018-09-07 21:48:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,692 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 170,900 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-07 01:01:58
合計ジャッジ時間 9,033 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,812 KB
testcase_01 AC 6 ms
6,944 KB
testcase_02 AC 7 ms
6,940 KB
testcase_03 AC 118 ms
6,944 KB
testcase_04 AC 318 ms
6,940 KB
testcase_05 AC 122 ms
6,940 KB
testcase_06 WA -
testcase_07 AC 254 ms
6,940 KB
testcase_08 AC 313 ms
6,944 KB
testcase_09 AC 397 ms
6,944 KB
testcase_10 WA -
testcase_11 AC 19 ms
6,944 KB
testcase_12 AC 216 ms
6,940 KB
testcase_13 AC 293 ms
6,944 KB
testcase_14 AC 350 ms
6,940 KB
testcase_15 WA -
testcase_16 AC 232 ms
6,944 KB
testcase_17 WA -
testcase_18 AC 546 ms
6,940 KB
testcase_19 AC 533 ms
6,940 KB
testcase_20 AC 534 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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=300;
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;
}
0