二、stl ,模拟,贪心等 [Cloned] Q - 贪心

原题:

When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN. 

There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS. 

To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero's HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds. 

Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss.

题意:

(猪一样的队友问题(滑稽)),打dota的时候你自己面对对面几个英雄,这些英雄有他们自己的攻击值和血量值,回合制度,你每次只能打掉一个英雄的一滴血,然后对面活着的英雄全部用他们自己的攻击力打击你一次(然而你血量无限并不会死),为了损失更少的血量,需要找出合理的进攻顺序,也就是先打死对面哪些英雄。

题解:

因为每次只能打对面一个英雄,而且只能打一滴血,那显然要先灭掉那些攻击力/生命值的比值大的英雄,创建结构体包括攻击力,生命值,和两者的比值,然后compare函数对结构体数组的重要程度比值进行排序。然后将比值从高到小依次灭掉,算出损失的生命总和就可以了。

代码:AC

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef struct{
	int dps;
	int hp;
	double value;
}hero;
hero A[100];
bool compare(hero a,hero b)
{
	return a.value>b.value;
}

int main()
{
	int n;
	while(cin>>n)
	{
		int i,alldps=0;
		for(i=0;i<n;i++)
		{
			cin>>A[i].dps>>A[i].hp;
			alldps+=A[i].dps;
			A[i].value=A[i].dps*1.0/A[i].hp;
		}
		sort(A,A+n,compare);
		int sum=0;
		for(i=0;i<n;i++)
		{
			while(A[i].hp--)
				sum+=alldps;
			alldps-=A[i].dps;
		}
		cout<<sum<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81395816
今日推荐