제출 #891813

#제출 시각아이디문제언어결과실행 시간메모리
891813CDuongDischarging (NOI20_discharging)C++17
100 / 100
106 ms15984 KiB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
 
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
 
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

struct Line {
    mutable i64 k, m, p;
    bool operator < (const Line &rhs) const { return k < rhs.k; }
    bool operator < (i64 x) const { return p < x; }
};

struct CDuonggsLineContainer : multiset<Line, less<>> {
    const static i64 inf = LLONG_MAX;

    i64 floor_div(i64 a, i64 b) {
        return a / b - ((a ^ b) < 0 && a % b);
    }

    bool isect(iterator x, iterator y) {
        if (y == end()) {
            x->p = inf;
            return false;
        }
        if (x->k == y->k) x->p = (x->m > y->m ? inf : -inf);
        else x->p = floor_div(x->m - y->m, y->k - x->k);
        return x->p >= y->p;
    }

    void add(i64 k, i64 m) {
        k = -k, m = -m;
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }

    i64 query(i64 x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return -(l.k * x + l.m);
    }
};
 
int n;
vector<pair<int, int>> a;

void solve() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int val; cin >> val;
        if (a.empty() || a.back().ff < val) a.emplace_back(val, 1);
        else ++a.back().ss;
    }
    CDuonggsLineContainer cht;
    int cur = 0;
    i64 pre = 0;
    for (auto [val, len] : a) {
        cht.add(-cur, pre);
        pre = 1ll * val * n + cht.query(val);
        cur += len;
    }
    cout << pre << endl;
}
 
signed main() {
 
#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif
 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();
 
#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
   cout << "Check array size pls sir" << endl;
#endif
 
}
#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...