Submission #671107

#TimeUsernameProblemLanguageResultExecution timeMemory
671107vuavisaoDischarging (NOI20_discharging)C++14
100 / 100
100 ms19484 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define ll long long #define ld long double using namespace std; const ll N = 1e6 + 10; const ll INF = 1e18; struct line { ll a = 0, b = 0; line() {}; line(ll _a, ll _b) { a = _a; b = _b; } ll Fn(const ll& x) { return this -> a * x + this -> b; } }; bool bad(line A, line B, line C) { return (ld) (C.b - A.b) / (A.a - C.a) < (ld) (B.b - A.b) / (A.a - B.a); } struct CHT { vector<line> memo = {}; ll ptr = 0; /// for a query where x has been sorted void add_line(const line& cur) { ll k = memo.size(); while(k >= 2 && bad(memo[k - 2], memo[k - 1], cur)) { memo.pop_back(); -- k; } memo.push_back(cur); ++ k; if(ptr >= k) ptr = k - 1; } ll Query_bs(const ll& x) { if(memo.empty()) return INF; ll l = 0, r = memo.size() - 1; while(l < r) { ll mid = (l + r) >> 1; if(memo[mid].Fn(x) < memo[mid + 1].Fn(x)) r = mid; else l = mid + 1; } return memo[l].Fn(x); } ll Query_ptr(const ll& x) { if(memo.empty()) return INF; while(ptr < memo.size() - 1 && memo[ptr].Fn(x) > memo[ptr + 1].Fn(x)) ++ ptr; return memo[ptr].Fn(x); } void clear() { this -> memo.clear(); this -> ptr = 0; } }; ll n, a[N]; ll dp[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("Discharging.inp", "r")) { freopen("Discharging.inp", "r", stdin); freopen("Discharging.out", "w", stdout); } cin >> n; for(ll i = 1; i <= n; ++ i) cin >> a[i]; reverse(a + 1, a + 1 + n); deque<ll> dq = {}; for(ll i = 1; i <= n; ++ i) { while(!dq.empty() && a[dq.back()] <= a[i]) dq.pop_back(); dq.push_back(i); } CHT cht = {}; cht.add_line(line(a[dq[0]], 0)); for(ll i = 1; i <= (ll) dq.size(); ++ i) { dp[i] = cht.Query_ptr(dq[i - 1]); if(i < (ll) dq.size()) cht.add_line(line(a[dq[i]], dp[i])); } cout << dp[dq.size()]; return 0; } /// Code by vuavisao

Compilation message (stderr)

Discharging.cpp: In member function 'long long int CHT::Query_ptr(const long long int&)':
Discharging.cpp:50:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         while(ptr < memo.size() - 1 && memo[ptr].Fn(x) > memo[ptr + 1].Fn(x)) ++ ptr;
      |               ~~~~^~~~~~~~~~~~~~~~~
Discharging.cpp: In function 'int32_t main()':
Discharging.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen("Discharging.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen("Discharging.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...