uva1379思维DP

Posted hesorchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva1379思维DP相关的知识,希望对你有一定的参考价值。

题目

题面

解题思路

可以证明最多用五个人对抗一个球队。

AC代码

#include <bits/stdc++.h>
using namespace std;

const int N = 105, M = 35, G = 215;

struct node
{
    int v, id;
    bool operator<(const node &temp) const
    {
        return v > temp.v;
    }
};
vector<node> vec[M];   //最大的五个
int dp[G][6][6][6][6 ]; //前i场 前四个天用的是哪几个人
int a[N];
void init()
{
    memset(dp, -0x3f3f3f3f, sizeof dp);
    memset(a, 0, sizeof a);
    for (int i = 0; i < M; i++)
        vec[i].clear();
}
void solve()
{
    init();
    int n, m, g;
    cin >> n >> m >> g;
    g += 10;
    for (int j = 1; j <= m; j++)
    {
        vector<node> tempvec;
        for (int i = 1; i <= n; i++)
        {
            int temp;
            cin >> temp;
            tempvec.emplace_back(node{temp, i});
        }
        sort(tempvec.begin(), tempvec.end());
        vec[j].emplace_back(node{0, 0});
        for (int i = 0; i < 5; i++)
            vec[j].emplace_back(tempvec[i]);
    }
    for (int i = 0; i < 6; i++)
        vec[0].emplace_back(node{0, 0});
    for (int i = 1; i <= g; i++)
        cin >> a[i];
    //初始化前四场比赛
    for (int i1 = 0; i1 <= 5; i1++)
    {
        for (int i2 = 0; i2 <= 5; i2++)
        {
            for (int i3 = 0; i3 <= 5; i3++)
            {
                for (int i4 = 0; i4 <= 5; i4++)
                {
                    if (a[1] && !i1)
                        continue;
                    if (a[2] && !i2)
                        continue;
                    if (a[3] && !i3)
                        continue;
                    if (a[4] && !i4)
                        continue;
                    if (!a[1] && i1)
                        continue;
                    if (!a[2] && i2)
                        continue;
                    if (!a[3] && i3)
                        continue;
                    if (!a[4] && i4)
                        continue;
                    if ((vec[a[1]][i1].id != vec[a[2]][i2].id || !(i1 * i2)) &&
                        (vec[a[1]][i1].id != vec[a[3]][i3].id || !(i1 * i3)) &&
                        (vec[a[1]][i1].id != vec[a[4]][i4].id || !(i1 * i4)) &&
                        (vec[a[2]][i2].id != vec[a[3]][i3].id || !(i2 * i3)) &&
                        (vec[a[2]][i2].id != vec[a[4]][i4].id || !(i2 * i4)) &&
                        (vec[a[3]][i3].id != vec[a[4]][i4].id || !(i3 * i4)))
                    {
                        dp[4][i1][i2][i3][i4] = vec[a[1]][i1].v +
                                                vec[a[2]][i2].v +
                                                vec[a[3]][i3].v +
                                                vec[a[4]][i4].v;
                    }
                }
            }
        }
    }

    int ans = 0;
    for (int i = 5; i <= g; i++)
    {
        for (int i1 = 0; i1 <= 5; i1++)
        {
            for (int i2 = 0; i2 <= 5; i2++)
            {
                for (int i3 = 0; i3 <= 5; i3++)
                {
                    for (int i4 = 0; i4 <= 5; i4++)
                    {
                        for (int i5 = 0; i5 <= 5; i5++)
                        {
                            if (a[i - 4] && !i1)
                                continue;
                            if (a[i - 3] && !i2)
                                continue;
                            if (a[i - 2] && !i3)
                                continue;
                            if (a[i - 1] && !i4)
                                continue;
                            if (a[i] && !i5)
                                continue;
                            if (!a[i - 4] && i1)
                                continue;
                            if (!a[i - 3] && i2)
                                continue;
                            if (!a[i - 2] && i3)
                                continue;
                            if (!a[i - 1] && i4)
                                continue;
                            if (!a[i] && i5)
                                continue;
                            if ((vec[a[i - 4]][i1].id != vec[a[i - 3]][i2].id || !(i1 * i2)) &&
                                (vec[a[i - 4]][i1].id != vec[a[i - 2]][i3].id || !(i1 * i3)) &&
                                (vec[a[i - 4]][i1].id != vec[a[i - 1]][i4].id || !(i1 * i4)) &&
                                (vec[a[i - 4]][i1].id != vec[a[i]][i5].id || !(i1 * i5)) &&
                                (vec[a[i - 3]][i2].id != vec[a[i - 2]][i3].id || !(i2 * i3)) &&
                                (vec[a[i - 3]][i2].id != vec[a[i - 1]][i4].id || !(i2 * i4)) &&
                                (vec[a[i - 3]][i2].id != vec[a[i]][i5].id || !(i2 * i5)) &&
                                (vec[a[i - 2]][i3].id != vec[a[i - 1]][i4].id || !(i3 * i4)) &&
                                (vec[a[i - 2]][i3].id != vec[a[i]][i5].id || !(i3 * i5)) &&
                                (vec[a[i - 1]][i4].id != vec[a[i]][i5].id || !(i4 * i5)))
                            {
                                dp[i][i2][i3][i4][i5] = max(
                                    dp[i][i2][i3][i4][i5],
                                    dp[i - 1][i1][i2][i3][i4] + vec[a[i]][i5].v),
                                ans = max(ans, dp[i][i2][i3][i4][i5]);
                            }
                        }
                    }
                }
            }
        }
    }
    double res = 1.0 * ans / 100;
    printf("%.2f\\n", res);
}

int main()
{
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

以上是关于uva1379思维DP的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 1005D Polycarp and Div 3(思维贪心dp)

UVa 1057 - Routing

UVA 11584 DP

UVA 10201 DP

uva10048

uva 10003 Cutting Sticks 区间dp