贪心(1)--hdu3979(贪心进阶)

                                                                                                 Monster

                                          Time Limit: 3000 MS     Memory Limit: 32768 KB     64bit IO Format: %I64d & %I64u

Description

One day, v11 encounters a group of monsters in a foreast. In order to defend the homeland, V11 picks up his weapon and fights!

All the monsters attack v11 at the same time. Every enemy has its HP, and attack value ATK. In this problem, v11 has his ATK and infinite HP. The damage (also means reduction for HP) is exactly the ATK the attacker has. For example, if v11's ATK is 13 and the monster's HP is 27, then after v11's attack, the monster's HP become 27 - 13 = 14 and vice versa.

v11 and the monsters attack each other at the same time and they could only attack one time per second. When the monster's HP is less or equal to 0 , we think this monster was killed, and obviously it would not attack any more. For example, v11's ATK is 10 and a monster's HP is 5, v11 attacks and then the monster is killed! However, a monster whose HP is 15 will be killed after v11 attack for two times. v11 will never stop until all the monsters are killed ! He wants to minimum the HP reduction for the fight! Please note that if in some second, some monster will soon be killed , the monster's attack will works too.

Input

The first line is one integer T indicates the number of the test cases. (T <=100)

Then for each case, The first line have two integers n (0<n<=10000), m (0<m<=100), indicates the number of the monsters and v11's ATK . The next n lines, each line has two integers hp (0<hp<=20), g(0<g<=1000) ,indicates the monster's HP and ATK.

Output

Output one line.

First output “Case #idx: ”, here idx is the case number count from 1. Then output the minimum HP reduction for v11 if he arrange his attack order optimal .

Sample Input

2 
3 1 
1 10 
1 20 
1 40 
1 10 
7 3

Sample Output

Case #1: 110 
Case #2: 3

                        

         这道题是在团队内的组队赛时遇到的,下来后发现了一份思路清晰的题解,偷个懒,题解直接copy别人的,题意大致如下:一个游侠和多个怪兽决斗,怎么样击杀怪兽会使游侠的耗血最少。这其中的关键是怪兽不会一个一个的跟你打,他们也不傻,他们对你群殴,在这里给大家举个例子假如有两个怪兽A和B,A怪兽的血量为HPA,攻击力为SA,B怪兽的血量为HPB,攻击力为SB,游侠的攻击力为m,现在有两种方法,一:先击杀A后击杀B。二:先击杀B后击杀A。一耗血量:(HPA/m)*(SA+SB)+(HPB/m)*SB.同理二耗血量为:(HPB/m)*(SA+SB)+(HPA/m)*SA。接下来就是比较两种方法哪一种耗血比较少了。即比较HPA*SB与HPB*SA的大小。所以接下来的问题就迎刃而解了。下面附上的AC代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#define LL long long
using namespace std;
struct node
{
	LL hp,at;
};
node mon[10005];
bool cmp(node a,node b)
{
	return a.hp*b.at<a.at*b.hp; 
}
int main()
{
	LL T,i,j;
	LL  n,m;
	scanf("%lld",&T);
	LL cas=1;
	while(T--)
	{
		long long sum=0,ans=0;
		scanf("%lld %lld",&n,&m);
		for(i=0;i<n;i++)
		{
			scanf("%lld%lld",&mon[i].hp,&mon[i].at);                             
			mon[i].hp=(LL)ceil((double)mon[i].hp/(double)m);   //必须对精度进行处理,不然wrong answer 
			sum+=(long long)mon[i].at;
		} 
		sort(mon,mon+n,cmp);
		for(i=0;i<n;i++)
		{
			ans+=(long long)sum*mon[i].hp;
			sum-=mon[i].at;
		}
		printf("Case #%lld: ",cas++);
		printf("%lld\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值