제출 #1345765

#제출 시각아이디문제언어결과실행 시간메모리
1345765adscodingHolding (COCI20_holding)C++20
110 / 110
199 ms145140 KiB
#include <bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

constexpr int maxn = 105;
int n, L, R, K, a[maxn], sumA, preL[10005], preR[10005];
unordered_map<int, int> dpL[maxn][maxn], dpR[maxn][maxn];

// --------------------------------------------------------------------------------------------

void solve()
{
    cin >> n >> L >> R >> K;
    FOR(i, 1, n)
    {
        cin >> a[i];
    }
    FOR(i, L, R)
        sumA += a[i];

    if (L > 1)
    {
        dpL[1][L][0] = 0;
        FOR(i, 1, L)
            FOR(j, L, R + 1)
                for (const auto &p : dpL[i][j])
                {
                    int k = p.fi, w = p.se;
                    minimize(dpL[i][j + 1][k], w);
                    minimize(dpL[i + 1][j][k], w);
                    int new_k = k + abs(j - i), cost = - a[j] + a[i];
                    if (k + abs(j - i) <= K)
                        minimize(dpL[i + 1][j + 1][new_k], w + cost);
                }
    }


    if (R < n)
    {
        dpR[n][R][0] = 0;
        FORD(i, n, R)
            FORD(j, R, L - 1)
                for (const auto &p : dpR[i][j])
                {
                    int k = p.fi, w = p.se;
                    if (j - 1 >= 0) minimize(dpR[i][j - 1][k], w);
                    if (i - 1 >= 0) minimize(dpR[i - 1][j][k], w);
                    int new_k = k + abs(j - i), cost = - a[j] + a[i];

                    if (k + abs(j - i) <= K && i - 1 >= 0 && j - 1 >= 0)
                    {
                        minimize(dpR[i - 1][j - 1][new_k], w + cost);
                    }

                }
    }
//    dbg(dpR[R][L - 1][3]);

    int res = 0;
    FOR(i, L - 1, R)
    {
        FOR(k, 0, K)
        {
            preL[k] = dpL[L][i + 1][k];

            if (k)
                minimize(preL[k], preL[k - 1]);
        }
        FOR(k, 0, K)
        {
            preR[k] = dpR[R][i][k];
            if (k)
                minimize(preR[k], preR[k - 1]);
            res = min(res, preR[k] + preL[K - k]);
        }
    }

    cout << sumA + res;
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

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

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