結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 15 ms
6,944 KB
testcase_04 AC 38 ms
6,940 KB
testcase_05 AC 15 ms
6,940 KB
testcase_06 WA -
testcase_07 AC 31 ms
6,940 KB
testcase_08 AC 37 ms
6,940 KB
testcase_09 AC 47 ms
6,940 KB
testcase_10 AC 55 ms
6,940 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 26 ms
6,940 KB
testcase_13 AC 34 ms
6,944 KB
testcase_14 AC 41 ms
6,944 KB
testcase_15 WA -
testcase_16 AC 28 ms
6,940 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 63 ms
6,940 KB
testcase_20 AC 62 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=100;
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