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;
}