제출 #1085090

#제출 시각아이디문제언어결과실행 시간메모리
1085090ZeroCool수열 (APIO14_sequence)C++14
100 / 100
1971 ms87228 KiB
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define ld long double
#define ar array

const ll INF = 1e18;

const int N = 1e5 + 20;

struct LINE {
    ll a, b, id ;
 
    LINE (ll a, ll b, ll id ) : a(a), b(b), id(id) {}
 
    ll operator() (const ll &x) {
        return a * x + b;
    }
};
 
struct convex_hull_trick {
    vector<LINE> op;
    void init () {
        vector<LINE> ().swap(op);
    }
    bool bad (LINE ln1, LINE ln2, LINE ln3) {
        __int128 hi = 1;
        return hi * (ln3.b - ln1.b) * (ln1.a - ln2.a) <= hi * (ln2.b - ln1.b) * (ln1.a - ln3.a);
    }
 
    void add (LINE ln) {
        while (op.size() >= 2 && bad(op[op.size() - 2], op[op.size() - 1], ln)) {
            op.pop_back();
        }
        op.push_back(ln);
    }
 
    ar<ll,2> get (int x) {
        if (op.empty()) return ar<ll,2>{INF, -1l};
        int lf = 0, rt = op.size() - 1;
        while (lf < rt) {
            int mid = (lf + rt) >> 1;
            if (op[mid](x) <= op[mid + 1](x)) rt = mid;
            else lf = mid + 1;
        }
        return ar<ll,2>{op[lf](x), op[lf].id};
 
    }
}CHT;

ll pref[N];
ll dp[N][2];
int prv[202][N];
ll A[N];

signed main() {ios_base::sync_with_stdio(false);cin.tie(0);
    int n, k;
    cin>>n>>k;
   // ll A[n];
    for(int i = 0;i < n;i++)cin>>A[i];
    //ll pref[n];
    //int prv[k][n];
    pref[0] = A[0];
    for(int i= 1;i < n;i++)pref[i] = pref[i - 1] + A[i];
    //ll dp[n][k + 1];
    for(int i = 0;i < n;i++)dp[i][0] = 0;

    for(int j = 1;j < k;j++){
        CHT.init();
        for(int i = 0;i < n;i++){
            ar<ll, 2> c = CHT.get(pref[i]);
            dp[i][j & 1] = -c[0];
            prv[j][i] = c[1]; 
            CHT.add(LINE{-pref[i], -(dp[i][j & 1 ^ 1] - pref[i] * pref[i]), i});
        }
    }
    for(int j = 1;j < k;j++){
        for(int i = 1;i < n;i++){
            //cout<<prv[j][i]<<" ";
        }
            //cout<<'\n';
    }
    ll res = -1;
    int opt = -1;
    for(int i = 0;i < n;i++){
        if(res < dp[i][(k - 1) & 1] + (pref[n - 1] - pref[i]) * pref[i]){
            res = dp[i][(k - 1) & 1] + (pref[n - 1] - pref[i]) * pref[i];
            opt = i;

        }
    }
    cout<<res<<'\n';
    for(int j = k - 1;j >= 0;j--){
        cout<<opt + 1<<" ";
        opt = prv[j][opt];
    }
    //for(auto u:ans)cout<<u + 1<<" ";
}

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp: In function 'int main()':
sequence.cpp:75:46: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   75 |             CHT.add(LINE{-pref[i], -(dp[i][j & 1 ^ 1] - pref[i] * pref[i]), i});
      |                                            ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...