City Destruction Kattis - city dp

/**
题目:City Destruction Kattis - city
链接:https://vjudge.net/problem/Kattis-city
题意:有n个怪兽,排成一行。每个怪兽有一个生命值和一个爆炸值。每次可以选择一个怪兽攻击早造成d伤害。
如果生命值<=0;那么怪兽i死亡并且爆炸。i+1,i-1第怪兽都会受到
第i个怪兽的爆炸值伤害。如果第i-1被第i炸死,第i-1怪兽不会继续炸别人。
不会传递。也就是只有攻击者攻击致死的怪兽才会对i-1,i+1造成爆炸值伤害。
求最少攻击多少次才能杀死所有的怪兽。
思路:
定义dp[i][j]表示前i个怪兽,j=0表示i-1比i先死,j=1表示i-1比i后死需要的最少攻击次数。
///本题直接写递推方程我写不出来,因为i和i-1以及未知的i+1都有关联。i+1是未知的。
所以不好表示,本人菜鸡。如果dfs记忆化做法,倒是清晰。

*/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
#define ms(x,y) memset(x,y,sizeof x)
typedef long long LL;
const int N = 1e4;
const LL INF = 1e18;
LL dp[N][2];///j=0表示i-1比i先死,j=1表示i-1比i晚死。
int h[N], e[N];
int n, d;
LL cal(LL r)
{
    if(r<=0) return 0;
    if(r%d==0){
        return r/d;
    }
    return r/d+1;
}
LL dfs(int i,int j)
{
    if(i==n){
        return 0;
    }
    LL &res = dp[i][j];
    if(res!=-1) return res;
    res = INF;
    LL r = h[i];
    if(j==0){
        r -= e[i-1];
        res = min(res,dfs(i+1,0)+cal(r));
        if(i<n-1){
            res = min(res,dfs(i+1,1)+cal(r-e[i+1]));
        }
    }else
    {
        res = min(res,dfs(i+1,0)+cal(r));
        if(i<n-1){
            res = min(res,dfs(i+1,1)+cal(r-e[i+1]));
        }
    }
    return res;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&d);
        for(int i = 0; i < n; i++){
            scanf("%d",&h[i]);
        }
        for(int i = 0; i < n; i++){
            scanf("%d",&e[i]);
        }
        memset(dp, -1, sizeof dp);
        printf("%lld\n",dfs(0,1));
    }
    return 0;
}
时间: 2024-08-26 10:57:26

City Destruction Kattis - city dp的相关文章

HDU 1505 City Game (hdu1506 dp二维加强版)

F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1505 Appoint description: Description Bob is a strategy game programming specialist. In his new city building game the gaming enviro

bzoj:1687;poj 2434:[Usaco2005 Open]Navigating the City 城市交通

Description A dip in the milk market has forced the cows to move to the city. The only employment available is in the venerable field of taxi-driving. Help the cows learn their way around the city. Given a city map with E (1 <= E <= 40) east/west st

HDOJ 5352 MZL&#39;s City 匈牙利匹配

求年份和城市的匹配 MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 539    Accepted Submission(s): 180 Problem Description MZL is an active girl who has her own country. Her big country has N

HDU 4849-Wow! Such City!(最短路)

Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 824    Accepted Submission(s): 310 Problem Description Doge, tired of being a popular image on internet, is considering moving

HDU-4849 Wow! Such City! (单源最短路)

Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his country there are N (2 ≤N≤ 1000) cities labeled 0 . . . N - 1. He is currently in city 0. Meanwhile, for each pa

ZOJ 3042 City Selection II 【序】【离散化】【数学】

题意: 输入数据n,m.n代表工厂的数量,m代表城市的数量. 接下来n+m行为工厂和城市的坐标. 规定如图所示方向刮风,工厂的air会污染风向地区的air. 注意,工厂和城市的坐标表示的是从x到x+1从y到y+1之间小正方形都是工厂区域,规定如果只有一个coner的air被污染那么该地区视为无污染. 要求输出有多少不被污染的城市. 坑: 思考了很多问题.... 1.加入某城市的正下方是工厂怎么办...这个是否算污染. 2.假如有两个角被污染了怎么办...算污染吗. 坑了一整天.没办法找大神的代码

hdu4849 Wow! Such City!(最短路dijkstra)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849 Problem Description Doge, tired of being a popular image on internet, is considering moving to another city for a new way of life. In his co

MZL&#39;s City(网络流)

MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 496    Accepted Submission(s): 164 Problem Description MZL is an active girl who has her own country. Her big country has N cities num

三级联动 City.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%> <script language="JavaScript">         var req = null; function test() { var province = document.all("province