結果

問題 No.1334 Multiply or Add
ユーザー chocoruskchocorusk
提出日時 2021-01-09 20:55:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,702 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 130,412 KB
最終ジャッジ日時 2025-01-17 15:52:15
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 68 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
int main()
{
    int n; cin>>n;
    ll a[200020];
    ll tot=1;
    for(int i=0; i<n; i++){
        cin>>a[i];
        tot=min(MOD, tot*a[i]);
    }
    if(tot==MOD){
        ll ans=1;
        for(int i=0; i<n; i++){
            ans=ans*a[i]%MOD;
        }
        cout<<ans<<endl;
        return 0;
    }
    ll p[200020];
    p[0]=1;
    for(int i=0; i<n; i++){
        p[i+1]=p[i]*a[i];
    }
    using Pl=pair<ll, ll>;
    deque<Pl> deq;
    ll dp[200020];
    dp[0]=0;
    deq.push_back({1, 0});
    for(int i=0; i<n; i++){
        ll x=p[i+1];
        while(deq.size()>1){
            ll a1=deq[0].first, b1=deq[0].second;
            ll a2=deq[1].first, b2=deq[1].second;
            if(x/a1+b1>x/a2+b2) break;
            deq.pop_front();
        }
        dp[i+1]=x/deq[0].first+deq[0].second;
        ll a0=p[i+1], b0=dp[i+1];
        while(deq.size()>1){
            ll a1=deq[0].first, b1=deq[0].second;
            ll a2=deq[1].first, b2=deq[1].second;
            if((b1-b2)*a2*(a0-a1)>(b0-b1)*a0*(a1-a2)) break;
            deq.pop_front();
        }
        deq.push_front({a0, b0});
    }
    cout<<dp[n]<<endl;
    return 0;
}
0