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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值