12-10 DP总结

· · 算法·理论

比赛链接

比赛情况:

木棍加工 最佳课题选择 书本整理 小杨买饮料 砝码称重 总分
9pts 0pts 0pts 60pts 0pts 69pts

属实是惨不忍睹

题目

木棍加工

wow,我竟然把最长上升子序列的状态转移方程写错了?!

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct node
{
    int l, w;
} a[5005];
int dp[5005];
bool cmp(node x, node y)
{
    if (x.l == y.l)
        return x.w > y.w;
    return x.l > y.l;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l >> a[i].w;
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
            if (a[j].w <= a[i].w)
                dp[j] = max(dp[j], dp[i] + 1);
    int ans = 0;
    for (int i = 1; i <= n; i++)
        ans = max(ans, dp[i]);
    cout << ans;
    return 0;
}

正确代码

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int l, w;
} a[5005];
int dp[5005];
bool cmp(node x, node y)
{
    if (x.l != y.l)
        return x.l > y.l;
    return  x.w > y.w;
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].l  >> a[i].w;
    sort(a + 1, a + 1 + n, cmp);
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        dp[i] = 1;
        for (int j = 1; j < i; j++)
            if (a[j].w < a[i].w)
                dp[i] = max(dp[i], dp[j] + 1);
        ans = max(dp[i], ans);
    }
    cout << ans;
    return 0;
}

最佳课题选择

这道题当时是这么写的:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[25], b[25];
int dp[205];
int pow_(int x, int y)
{
    int res = 1;
    while (y--)
        res *= x;
    return res;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> a[i] >> b[i];
    memset(dp, 0x3f, sizeof dp); 
    dp[0] = 0;
    for (int i = 1; i <= m; i++)
    {
        for (int j = a[i]; j <= n; j++)
        {
            int w = a[i] * pow_(j, b[i]);
            dp[j] = min(dp[j], dp[j - w] + w);
        }
    }
    cout << dp[n] - 1;
    return 0;
}

看出问题来了吧,我没有枚举可以给第 i 篇分配的论文数量啊,所以加上枚举 k(k 就是可以给第 i 篇分配的论文数量) 然后代码就变成了这样:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[105], b[25], dp[25][205];
int p(int a, int b)
{
    int res = 1;
    while (b--)
    {
        res *= a;
    }
    return res;
}
signed main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> a[i] >> b[i];
    memset(dp, 0x3f, sizeof dp);
    dp[0][0] = 0;
    for (int i = 1; i <= m; i++)
        for (int j = 0; j <= n; j++)
            for (int k = 0; k <= j; k++)
                dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + a[i] * p(k, b[i]));
    cout << dp[m][n];
    return 0;
}

然后就A了(服了)

书本整理

完整代码:

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int h, w;
} a[105];
int dp[105][105];
bool cmp(node x, node y)
{
    return x.h < y.h;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i].h >> a[i].w;
    sort(a + 1, a + 1 + n, cmp);
    memset(dp, 0x3f, sizeof dp);
    for (int i = 0; i <= n; i++)
        dp[i][0] = dp[i][1] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= i; j++)
            for (int p = j - 1; p < i; p++)
                dp[i][j] = min(dp[i][j], dp[p][j - 1] + abs(a[i].w - a[p].w));
    int ans = 0x3f3f3f3f;
    for (int i = n - k; i <= n; i++)
        ans = min(ans, dp[i][n - k]);
    cout << ans;
    return 0;
}

小杨买饮料

代码:

#include <bits/stdc++.h>
using namespace std;
int c[505], l[505];
long long dp[2005];
int main()
{
    int n, L;
    cin >> n >> L;
    for (int i = 1; i <= n; i++)
        cin >> c[i] >> l[i];
    memset(dp, 0x3f, sizeof dp);
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
        for (int j = L; j >= 0; j--)
            if (dp[j] < 1e9)
            {
                int pos = min(L, j + l[i]);
                dp[pos] = min(dp[pos], dp[j] + c[i]);
            }
    if (dp[L] > 1e9)
        cout << "no solution";
    else cout << dp[L];
    return 0;
}

砝码称重

DFS 暴搜竟然会递归次数太多,从而 RE,太可恶了╰(‵□′)╯