HDU 4970 Killing Monsters 【搜索】

来源:互联网 发布:网络招商平台 编辑:程序博客网 时间:2024/05/21 03:25

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970

题目大意:给你一些防御塔的位置和其攻击力,以及一些怪物的血量和位置,问走到终点还有几个怪物活着。

题目思路:最开始看题目的时候就是区间更新的过程觉得会花很多时间,然后用的树状数组,后来发现用的一个机智方法可以过,简单了很多。

区间更新的主要时间是花在塔的伤害,(L,R)D伤害上面,我们用一个sttack数组记录伤害,在attack[L]+=D,在attack[R]-=D,然后从前往后扫一遍,可以得到每个点的伤害,在从后往前扫一遍可以得到每个点走到终点需要受到多少伤害。挺好的方法。

#include <stdio.h>#include <cstring>#include <iostream>using namespace std;const int MAX_N = 100002;int n, m;long long attack[MAX_N];void solve(){    long long hp;    int total = 0, K, pos;    scanf("%d", &K);    for (int i = 0; i < K; i++){        scanf("%I64d%d", &hp, &pos);        if (attack[pos] < hp) total++;    }    printf("%d\n", total);}int main(){while (1){    scanf("%d%d", &n, &m);    if (!n) break;    int x, y, d;    memset(attack, 0, sizeof(attack[0]) * (n + 1));    for (int i = 0; i < m; i++){        scanf("%d%d%d", &x, &y, &d);        attack[x] += d;        attack[y + 1] -= d;    }    for (int i = 1; i <= n; i++) attack[i] += attack[i - 1];    for (int i = n - 1; i >= 1; i--) attack[i] += attack[i + 1];    solve();}    return 0;}



0 0
原创粉丝点击