Submission #261330

#TimeUsernameProblemLanguageResultExecution timeMemory
261330NightlightDischarging (NOI20_discharging)C++14
100 / 100
248 ms30052 KiB
#include <bits/stdc++.h> #define pii pair<long long, long long> #define inter second #define slope first using namespace std; int N; long long A[1000005], ans = LLONG_MAX; long long T[1000005]; long long pos[1000005]; long long dp[1000005]; pii CHT[100005]; int sz, pointer; int p; inline double isect(pii a, pii b) {return (a.inter - b.inter) / (b.slope - a.slope);} inline bool bad(pii a, pii b, pii c) {return isect(c, a) < isect(a, b);} inline long long val(long long X, int id) {return CHT[id].slope * X + CHT[id].inter;} void insert(long long m, long long b) { pii L = {m, b}; while(sz > 1 && bad(CHT[sz - 1], CHT[sz], L)) sz--; CHT[++sz] = L; } long long query(long long X) { long long res = LLONG_MAX; // cout << X << "\n"; for(int i = 1; i <= sz; i++) { res = min(res, val(X, i)); // cout << CHT[i].first << ": " << val(X, i) << "\n"; } // cout << "\n"; return res; } int main() { scanf("%d", &N); for(int i = 1; i <= N; i++) { scanf("%lld", &T[i]); if(T[i] > A[p]) { A[++p] = T[i]; pos[p] = i; } } dp[p] = A[p] * (N - pos[p] + 1); ans = A[p] * N; insert(A[p], dp[p] + A[p] * pos[p]); for(int i = p - 1; i > 0; i--) { dp[i] = A[i] * (N - pos[i] + 1) + query(-pos[i + 1]); insert(A[i], dp[i] + A[i] * pos[i]); ans = min(ans, dp[i] + A[i] * (pos[i] - 1)); } printf("%lld\n", ans); cin >> N; }

Compilation message (stderr)

Discharging.cpp: In function 'int main()':
Discharging.cpp:38:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &N);
   ~~~~~^~~~~~~~~~
Discharging.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &T[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...