제출 #1119891

#제출 시각아이디문제언어결과실행 시간메모리
1119891InvMODK개의 묶음 (IZhO14_blocks)C++14
100 / 100
291 ms81084 KiB
#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){
                ll best = dp[curK-1][i-1] + a[i], best_pos = i;
                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();
                }

                if(st.top() > 0){
                    best = min(best, dp[curK][st.top()]);
                    best = min(best, dp[curK-1][st.top()] + a[i]);
                }

                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;
}

컴파일 시 표준 에러 (stderr) 메시지

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