Submission #1300137

#TimeUsernameProblemLanguageResultExecution timeMemory
1300137ThanhsDischarging (NOI20_discharging)C++20
11 / 100
305 ms127244 KiB
#include <bits/stdc++.h>
using namespace std;

#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()

mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 1e6 + 5;

int a[NM], n, f[NM];
deque<pair<int, list<int>>> v;
multiset<int> s;

bool check(int k1, int m1, int k2, int m2, int k3, int m3)
{
    double inter1 = double(m2 - m1) / (k1 - k2), inter2 = double(m3 - m1) / (k1 - k3);
    return inter1 >= inter2;
}

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    v.push_back({0, {}});
    v.back().se.push_back(0);
    s.insert(0);
    for (int i = 1; i <= n; i++)
    {
        bool siu = 0;
        while (v.size() && v.back().fi <= a[i])
        {
            int t = *v.back().se.begin();
            s.erase(s.find(f[t] + v.back().fi * (n - t)));
            if (v.size() > 1 && v[v.size() - 2].fi <= a[i])
            {
                v[v.size() - 2].se.splice(v[v.size() - 2].se.end(), v.back().se);
                v.pop_back();
            }
            else
                break;
        } 
        if (v.back().fi <= a[i])
        {
            v.back().fi = a[i];
            auto& li = v.back().se;
            while (li.size() >= 2)
            {
                auto beg = li.begin();
                int x = *beg, y = *next(beg);
                if (f[x] + a[i] * (n - x) >= f[y] + a[i] * (n - y))
                {
                    li.erase(beg);
                    continue;
                }
                if (li.size() >= 3 && check(f[x], n - x, f[y], n - y, f[*next(beg, 2)], n - *next(beg, 2)))
                {
                    li.erase(next(beg));
                    continue;
                }
                break;
            }
            int x = *li.begin();
            s.insert(f[x] + a[i] * (n - x));
        }
        // cout << i << endl;
        // for (auto t : v.back().se)
        //     cout << t << ' ';
        // cout << endl;
        f[i] = *s.begin();
        if (v.back().fi > a[i])
        {
            v.push_back({a[i], {i}});
            s.insert(f[i] + a[i] * (n - i));
        }
        else
            v.back().se.push_back(i);
        // cout << f[i] << endl;
    }
    cout << f[n];
}

signed main()
{
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    else if (fopen(name".inp", "r"))
    {
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int tc = 1; 
    // cin >> tc;
    while (tc--)
        solve();
}

Compilation message (stderr)

Discharging.cpp: In function 'int main()':
Discharging.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...