Submission #956084

#TimeUsernameProblemLanguageResultExecution timeMemory
956084wibulordK blocks (IZhO14_blocks)C++14
0 / 100
68 ms160848 KiB
#include <bits/stdc++.h> #define endl '\n' #define ll long long #define fi first #define se second #define pb emplace_back #define pii pair<int, int> #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x >> (i)) & 1) #define sz(s) (int)((s).size()) #define all(a) a.begin(), a.end() #define uni(a) (sort(all(a)), a.resize(unique(all(a))-a.begin())) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i) #define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--) using namespace std; template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;} template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;} const int MOD = (int)1e9 + 7; const int mod = 998244353; const int N = 2e5 + 1, M = 101; const long long INF = (long long)(4e18 + 7); /* /\_/\ (= ._.) / >? \>$ */ int n, s, l[N]; long long a[N], dp[N][M]; void sol(void){ cin >> n >> s; FOR(i, 1, n) cin >> a[i]; stack<int> st; FOR(i, 1, n){ while(sz(st) && a[st.top()] <= a[i]) st.pop(); l[i] = sz(st) ? st.top() : 0; st.push(i); } memset(dp, 0x3f, sizeof(dp)); dp[1][1] = a[1]; FOR(i, 2, n) dp[i][1] = max(dp[i-1][1], 1LL * a[i]); FOR(k, 2, s){ deque<int> dq; FOR(i, 1, n){ dp[i][k] = dp[l[i]][k]; while(sz(dq) && dq.front() < l[i]) dq.pop_front(); if(sz(dq)) dp[i][k] = min(dp[i][k], dp[dq.front()][k-1] + a[i]); while(sz(dq) && dp[dq.back()][k-1] >= dp[i][k-1]) dq.pop_back(); dq.push_back(i); } } // FOR(j, 1, s) FOR(i, 1, n) cout << dp[i][j] << " \n"[i == n]; cout << dp[n][s] << '\n'; } signed main(void){ #define TASK "nhap" if(fopen(TASK".inp", "r")){ freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while(t--) sol(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n"; }

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen(TASK".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...