Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2

题目大意

有排成一排的n个弓箭手,第i个弓箭手有 hi 的血量。主角可以使用火球术去攻击弓箭手。被直接攻击的弓箭手i受到a点伤害,同时第i?1个和第i+1个弓箭手会受到b点伤害。当弓箭手的血量小于0时弓箭手死亡。问最少需要释放多少个火球术,每个火球术攻击的是第几个弓箭手。

解题思路

这是一个动态规划问题。定义一个四维dp数组。对于dp[i][j][k][l]来说表示了当第i?1个弓箭手的血量是j,第i个弓箭手的血量是k,第i+1个弓箭手的血量是l时释放火球的最小数目。初始值dp[1][h[0]][h[1]][h[2]]=0,其他为无穷大,其中h[i]代表第i个弓箭手的血量。

题目代码

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <time.h>
#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
using namespace std;

int n,a,b,h[15];

int dp[15][20][20][20];

struct node
{
    int i,j,k,l;
} p[15][20][20][20],temp;

int main()
{
    scanf("%d%d%d",&n,&a,&b);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&h[i]);
        h[i]++;
    }
    clearall(dp,0x3f);
    dp[1][h[0]][h[1]][h[2]]=0;
    int x,y,z,d1,d2,d3,init = dp[0][0][0][0];
    //cout<<init;
    for(int i=1; i<n-1; i++)
    {
        for(int j=h[i-1]; j>=0; j--)
        {
            for(int k=h[i]; k>=0; k--)
            {
                for(int l=h[i+1]; l>=0; l--)
                {
                    if(dp[i][j][k][l]!=init)
                    {
                        d1=max(0,j-b);
                        d2=max(0,k-a);
                        d3=max(0,l-b);
                        if(dp[i][d1][d2][d3]>dp[i][j][k][l]+1)
                        {
                            dp[i][d1][d2][d3]=dp[i][j][k][l]+1;
                            p[i][d1][d2][d3].j=j;
                            p[i][d1][d2][d3].k=k;
                            p[i][d1][d2][d3].l=l;
                            p[i][d1][d2][d3].i=i;
                        }
                        if(d1==0&&dp[i+1][d2][d3][h[i+2]]>dp[i][j][k][l]+1)
                        {
                            dp[i+1][d2][d3][h[i+2]]=dp[i][j][k][l]+1;
                            p[i+1][d2][d3][h[i+2]].i=i;
                            p[i+1][d2][d3][h[i+2]].j=j;
                            p[i+1][d2][d3][h[i+2]].k=k;
                            p[i+1][d2][d3][h[i+2]].l=l;
                        }
                        if(j==0&&dp[i+1][k][l][h[i+2]]>dp[i][j][k][l])
                        {
                            dp[i+1][k][l][h[i+2]]=dp[i][j][k][l];
                            p[i+1][k][l][h[i+2]]=p[i][j][k][l];
                        }
                    }
                }
            }
        }
    }

    printf("%d\n",dp[n-1][0][0][0]);
    temp.i=n-1;
    temp.j=0;
    temp.k=0;
    temp.l=0;
    while(!(temp.i==1&&temp.j==h[0]&&temp.k==h[1]&&temp.l==h[2]))
    {
        temp = p[temp.i][temp.j][temp.k][temp.l];
        printf("%d ",temp.i+1);
    }
    return 0;
}
时间: 2024-10-11 07:36:27

Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2的相关文章

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1

水题 Codeforces Beta Round #70 (Div. 2) A. Haiku

题目传送门 1 /* 2 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 3 下午网速巨慢:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 cons

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #9 (Div. 2 Only)

Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7