P4685

dp #贪心

\(dp_{i,j,k}\) 表示考虑前 \(i\) 段,已经用了 \(j\) 个字符,其中 \(k\) 个字符已经确定最后一个

转移可以每次枚举 \(j,k\) 的变化量,然后贪心的分配

具体的贪心为将 第一次出现的放在当前最后,最后一次出现的放最前

// Author: xiaruize
#ifndef ONLINE_JUDGE
bool start_of_memory_use;
#endif
#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
clock_t start_clock = clock();
#endif
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define rep(i, a, n) for (int i = (a); i <= (n); ++i)
#define per(i, n, a) for (int i = (n); i >= (a); --i)
int max(int a, int b)
{
    if (a > b)
        return a;
    return b;
}
int min(int a, int b)
{
    if (a < b)
        return a;
    return b;
}
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 50 + 10;

int n;
int p[N];
int dp[55][30][30];

void solve()
{
    cin >> n;
    rep(i, 1, n) cin >> p[i];
    mms(dp, 0x3f);
    dp[0][0][0] = 0;
    rep(i, 1, n)
    {
        int lim = min(26ll, p[i]);
        rep(j, 0, 26)
        {
            rep(k, 0, j)
            {
                if (dp[i - 1][j][k] >= INF)
                    continue;
                rep(a, max(0, lim - j + k), min(lim, 26 - j))
                {
                    rep(b, 0, j + a - k)
                    {
                        int x = min(b, min(lim - a, j - k));
                        int t = j - k - x;
                        int y = a - b + x;
                        int tmp = x * (x + 1) / 2 + y * (y - 1) / 2;
                        if (t)
                            tmp += t * p[i];
                        else
                            tmp += max(0, p[i] - 26);
                        dp[i][j + a][k + b] = min(dp[i][j + a][k + b], dp[i - 1][j][k] + tmp);
                    }
                }
            }
        }
    }
    int res = INF;
    rep(i, 0, 26) res = min(res, dp[n][i][i]);
    cout << res << endl;
}

#ifndef ONLINE_JUDGE
bool end_of_memory_use;
#endif

signed main()
{
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int testcase = 1;
    // cin >> testcase;
    while (testcase--)
        solve();
#ifndef ONLINE_JUDGE
    cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl;
    cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
#endif
    return 0;
}
posted @ 2024-03-30 16:48  xiaruize  阅读(10)  评论(0)    收藏  举报