ZOJ3741 状压DP Eternal Reality

E - Eternal Reality
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

In Academy City, most students have special abilities. Such as Railgun, Teleport, Telekinesis, AIM Stalker and so on. Here, AIM (An Involuntary Movement) is a term used to refer to the phenomenon in which an esper involuntarily produces an invisible energy field around the esper. Different students have different type of AIM dispersion field, so they also have different level of abilities.

Of course, a higher level students can often deal with more issues than a lower level students. To classify the students in Academy City, there are 7 levels in total:

LevelTermDescription
Level 0 Person with No Powers Most students of this level can't keep up at school. They might possess some degree of power, but unable to truly control it.
Level 1 Person with Low Powers Powers of the degree to bend a spoon, many students belong here.
Level 2 Person with Unusual Powers Just like Level 1, powers are not very useful in everyday life.
Level 3 Person with Strong Powers The degree when powers are considered convenient in everyday life, ability-wise this is the Level when one starts to be treated as part of the elite.
Level 4 Person with Great Powers Powers of an extent that their owner acquires tactical value of a military force.
Level 5 Person with Super Powers Powers of an extent that their owner can fight alone against a military force on equal terms.
Level 6 Person with Absolute Powers Powers of an extent that they're considered immeasurable. However, no one can achieve this level, even with the help of Level Upper (it has no effect on persons with super powers). Since this, many institutions have been doing long-term researches about it, such as the Radio Noise Project.

You are a student of level L in Academy City and you are going to take part in a sports competition. The competition consists of N consecutive matches. If you want to get a point in the i-th match, your must reach at least Ai level. According to the rules, you must compete in all matches one by one.

To tell the truth, it won't be easy to compete with so many high-level opponents. Fortunately, you got a special item called Level Upper. Generally, it can increase your level by 1 for a short time. If you use the Level Upper before the i-th match, it's effect will last during the matches [i, i + X - 1]. But it also has a side effect that will make your level become 0 during the matches [i + X, i + X + Y - 1]. After the side effect ends, your level will return to L and you can use the Level Upper again.

Please calculate the maximal points you can get if you properly use the Level Upper.

Input

There are multiple test cases (plenty of small cases with several large cases). For each test case:

The first line contains four integers L (0 <= L <= 5), N, X and Y (1 <= N, X, Y <= 100). The next line contains N integers Ai (0 <= Ai <= 6).

Output

For each test case, output the maximal points you can get.

Sample Input

3 6 1 2
1 3 4 5 6 4

Sample Output

4

Hint

Read the problem description carefully.

#include<stdio.h>
#include<string.h>

using namespace std;

int flag1[110]; // 第i关不用技能是否可行
int flag2[110]; // 第i关用技能是否可行
int flag3[110]; // 第i关是否为0
int flag[110][220]; // 表示第i关j状态是否可行
int dp[110][220]; // 第i关的状态为j的最大过关数

// 这里的状态范围为 [0, x+y] ,分为三类
// [0] 正常状态, [1,x] 使用技能+1状态,[x+1,x+y] 技能恢复中能力值为0状态

int max(int a, int b)
{
    return a > b ? a : b;
}

int main()
{
    int l, n, x, y;
    while(scanf("%d %d %d %d",&l, &n, &x, &y) == 4)
    {
        memset(flag1, 0, sizeof(flag1));
        memset(flag2, 0, sizeof(flag2));
        memset(flag3, 0, sizeof(flag3));
        memset(flag, 0, sizeof(flag));
        memset(dp, 0, sizeof(dp));
        int tmp;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&tmp);
            if(l >= tmp) flag1[i] = 1;
            if(l + 1 >= tmp && l != 5)  // 能力值为5,技能+1过关,不可行
            {
                flag2[i] = 1;
            }
            if(tmp == 0) flag3[i] = 1;
        }
        flag[0][0] = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j <= x + y; j++)
            {
                if((j == 0 || j == x + y) && flag[i-1][j])
                {
                    //如果前一个状态为 0 或 x+y 并且 前一关状态j可行
                    //那么下一个状态可以用技能到状态1或不用技能到状态0
                    dp[i][0] = max(dp[i][0], dp[i-1][j] + flag1[i]);//不用技能
                    dp[i][1] = max(dp[i][1], dp[i-1][j] + flag2[i]);//用技能
                    flag[i][0] = flag[i][1] = 1;
                }
                else if(j > 0 && j < x + y && flag[i-1][j])
                {
                    //当前状态为 >0 && <x+y 说明已经用过技能
                    //那么下一个状态只能为 [2, x+y] 范围内
                    //那么将前一个状态j可以分成 [1,x) 和 [x, x+y) 考虑
                    if(j < x)
                    {
                        dp[i][j+1] = max(dp[i][j+1], dp[i-1][j] + flag2[i]); //必须用技能
                    }
                    else
                    {
                        dp[i][j+1] = max(dp[i][j+1], dp[i-1][j] + flag3[i]); //技能冷却中,能力值为0
                    }
                    flag[i][j+1] = 1;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i <= x + y; i++)
        {
            ans = max(ans, dp[n][i]);
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/13224ACMer/p/4665448.html