AOE打怪

aoe:一个一维地图,上面有一些怪物,主角现在要打怪,每触发一次技能就会对范围内的怪物造成1点伤害,请问最少需要触发多少次技能才能消灭完怪物;
注:x为主角位置,伤害范围是 x-y 到 x+y ;
输入:第一行输入n表示怪物数量,y表示主角攻击范围;
接下来输入n组数据,每组包含两个值pos和ph,分别表示怪物的位置和血量;
输出:最少触发技能次数

eg:
输入:
3 3
3 4
1 10
10 5

输出:
15

#include<iostream>
#include<map>
using namespace std;

int func(map<int, int> monster, int y)
{
	int pos1, pos2;
	int count = 0;
	int tmp;
	map<int, int>::iterator it = monster.begin();
	pos1 = it->first;
	tmp = it->second;
	it++;
	for (; it != monster.end(); it++)
	{
		pos2 = it->first;
		if (pos2 - pos1 <= 2 * y)
		{
			tmp = it->second > tmp ? it->second : tmp;
		}
		else
		{
			count += tmp;
			pos1 = it->first;
			tmp = it->second;
		}
	}
	count += tmp;
	return count;
}

int main()
{
	int n, y;  //怪数量 人物伤害范围
	int pos, hp;  //怪位置 怪hp
	map<int, int> monster;
	while(cin >> n >> y)
    {
		for (int i = 0; i < n; i++)
		{
			cin >> pos >> hp;
			monster[pos] = hp;
		}
		cout << func(monster, y) << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值