Submission #1116995

#TimeUsernameProblemLanguageResultExecution timeMemory
1116995whoknowK blocks (IZhO14_blocks)C++17
100 / 100
130 ms3540 KiB
#include <bits/stdc++.h> #define ll long long #define int long long #define F first #define S second #define MAXN 100005 #define ii pair<int,int> #define bit(i,j) ((i>>j)&1) #define sz(i) (int)i.size() #define endl '\n' using namespace std; const ll INF = 1e18; int n, K; int a[MAXN]; namespace sub1 { ll dp[2][MAXN]; void solve() { for(int i = 1; i <= n; i++) dp[0][i] = max(dp[0][i - 1], a[i]); for(int j = 2; j <= K; j++) { stack<ii>st; for(int i = j; i <= n; i++) { int tmp = dp[0][i - 1]; while(sz(st) && st.top().S < a[i]) { tmp = min(tmp, st.top().F); st.pop(); } if(sz(st) == 0 || tmp + a[i] <= st.top().F + st.top().S) st.push({tmp,a[i]}); dp[1][i] = st.top().F + st.top().S; } for(int i = 1; i <= n; i++) dp[0][i] = dp[1][i]; } cout << dp[0][n]; } } main() { if(fopen("TEST.inp", "r")) { freopen("TEST.inp", "r", stdin); freopen("TEST.out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> K; for(int i = 1; i <= n; i++) cin >> a[i]; sub1::solve(); }

Compilation message (stderr)

blocks.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   43 | main()
      | ^~~~
blocks.cpp: In function 'int main()':
blocks.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("TEST.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen("TEST.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...