Submission #1116989

#TimeUsernameProblemLanguageResultExecution timeMemory
1116989whoknowK blocks (IZhO14_blocks)C++17
100 / 100
181 ms4220 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<int>st1, st2;
        for(int i = j; i <= n; i++)
        {
            int tmp = dp[0][i - 1];
            while(sz(st2) && st2.top() < a[i])
            {
                st2.pop();
                if(sz(st1))
                {
                    tmp = min(tmp, st1.top());
                    st1.pop();
                }
            }
            if(sz(st1) && sz(st2) && tmp + a[i] <= st1.top() + st2.top())
            {
                st1.push(tmp);
                st2.push(a[i]);
            }
            if(!sz(st1))
                st1.push(tmp);
            if(!sz(st2))
                st2.push(a[i]);
            dp[1][i] = st1.top() + st2.top();
        }
        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:54:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   54 | main()
      | ^~~~
blocks.cpp: In function 'int main()':
blocks.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |         freopen("TEST.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         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...