Submission #1119906

# Submission time Handle Problem Language Result Execution time Memory
1119906 2024-11-27T15:02:23 Z InvMOD K blocks (IZhO14_blocks) C++14
100 / 100
254 ms 80896 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;


int n, k, a[N];

namespace Subtask1{
    bool ckSub(){
        return n <= 1e2;
    }

    const int N = 105, K = 105;

    int dp[K][N];

    void process()
    {
        FOR(i, 0, k){
            FOR(j, 0, n){
                dp[i][j] = 1e9 + 1;
            }
        }

        dp[0][0] = 0;
        FOR(i, 1, k){
            FOR(r, 1, n){

                int Mx = a[r];
                for(int l = r; l >= 1; l--){
                    ckmx(Mx, a[l]);
                    ckmn(dp[i][r], dp[i-1][l-1] + Mx);
                }
            }
        }

        cout << dp[k][n] <<"\n";
        return;
    }
}

namespace Subtask2{
    bool ckSub(){
        return n <= N;
    }

    const int K = 105;

    ll dp[K][N];

    void process()
    {
        FOR(i, 0, k){
            FOR(j, 0, n){
                dp[i][j] = INF;
            }
        }

        dp[0][0] = 0;

        a[0] = 1e9;
        FOR(curK, 1, k){
            stack<int> st; st.push(0);
            FOR(i, 1, n){
                // first optimal will be i
                ll best = dp[curK-1][i-1] + a[i], best_pos = i;

                // if a[i] >= a[st.top()], it can get optimal position from st.top()
                while(!st.empty() && a[i] >= a[st.top()]){
                    if(ckmn(best, dp[curK-1][st.top()-1] + a[i])){
                        best_pos = st.top();
                    }
                    st.pop();
                }

                // a[i] < a[st.top()] then we can increase st.top() block length
                // ~or it can create a maximum len array from st.top() + 1 -> a[i]~
                // not really need to create a block from st.top() -> a[i] (it can be a block from st.top() -> i-1 already)
                if(st.top() > 0){
                    best = min(best, dp[curK][st.top()]);
                }

                dp[curK][i] = min(dp[curK][i], best);
                st.push(best_pos), st.push(i);
            }
        }

        cout << dp[k][n] <<"\n";
        return;
    }
}

void solve()
{
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];

    //Subtask1::process();
    Subtask2::process();
    return;

    if(Subtask1::ckSub()){
        return Subtask1::process(), void();
    }
    else{
        return Subtask2::process(), void();
    }
    return;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message

blocks.cpp: In function 'int main()':
blocks.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  142 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 464 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 508 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 472 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 508 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 504 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 592 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 404 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 336 KB Output is correct
49 Correct 1 ms 336 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 1 ms 592 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 2 ms 1104 KB Output is correct
54 Correct 2 ms 1104 KB Output is correct
55 Correct 1 ms 592 KB Output is correct
56 Correct 1 ms 1104 KB Output is correct
57 Correct 1 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 472 KB Output is correct
27 Correct 2 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 508 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 504 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 592 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 1 ms 404 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 336 KB Output is correct
49 Correct 1 ms 336 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 1 ms 592 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 2 ms 1104 KB Output is correct
54 Correct 2 ms 1104 KB Output is correct
55 Correct 1 ms 592 KB Output is correct
56 Correct 1 ms 1104 KB Output is correct
57 Correct 1 ms 1104 KB Output is correct
58 Correct 6 ms 2640 KB Output is correct
59 Correct 5 ms 1616 KB Output is correct
60 Correct 10 ms 3660 KB Output is correct
61 Correct 85 ms 28128 KB Output is correct
62 Correct 237 ms 80524 KB Output is correct
63 Correct 14 ms 4576 KB Output is correct
64 Correct 119 ms 37452 KB Output is correct
65 Correct 1 ms 592 KB Output is correct
66 Correct 2 ms 592 KB Output is correct
67 Correct 1 ms 592 KB Output is correct
68 Correct 1 ms 592 KB Output is correct
69 Correct 1 ms 336 KB Output is correct
70 Correct 1 ms 592 KB Output is correct
71 Correct 30 ms 8784 KB Output is correct
72 Correct 2 ms 592 KB Output is correct
73 Correct 7 ms 2640 KB Output is correct
74 Correct 5 ms 1616 KB Output is correct
75 Correct 10 ms 3676 KB Output is correct
76 Correct 81 ms 28116 KB Output is correct
77 Correct 254 ms 80456 KB Output is correct
78 Correct 17 ms 4432 KB Output is correct
79 Correct 114 ms 37536 KB Output is correct
80 Correct 11 ms 2912 KB Output is correct
81 Correct 38 ms 9920 KB Output is correct
82 Correct 239 ms 80416 KB Output is correct
83 Correct 1 ms 336 KB Output is correct
84 Correct 1 ms 592 KB Output is correct
85 Correct 22 ms 9040 KB Output is correct
86 Correct 2 ms 848 KB Output is correct
87 Correct 6 ms 2604 KB Output is correct
88 Correct 5 ms 1624 KB Output is correct
89 Correct 13 ms 3656 KB Output is correct
90 Correct 64 ms 28056 KB Output is correct
91 Correct 211 ms 80896 KB Output is correct
92 Correct 13 ms 4944 KB Output is correct
93 Correct 106 ms 38084 KB Output is correct
94 Correct 3 ms 592 KB Output is correct
95 Correct 27 ms 8784 KB Output is correct
96 Correct 1 ms 592 KB Output is correct
97 Correct 1 ms 336 KB Output is correct
98 Correct 1 ms 464 KB Output is correct
99 Correct 1 ms 336 KB Output is correct