2020 HDU多校 第四场 02-Blow up the Enemy(贪心 + Map)

题目链接: 02-Blow up the Enemy

Description

题意:给出n把武器的伤害值和使用间隔,求选中互相攻击必胜的概率

Zhang3 is playing a shooting game with Father. In the game there are two players trying to kill each other to win the game.

The game provides n weapons, each has two properties: Damage and Delay. The ith weapon has Damage Ai and Delay Di. When a player shoots with this weapon, his enemy’s HP is reduced by Ai, then he must wait for Di ms before he can shoot again.
The game processes as follows:

  1. Before the game starts, Zhang3 and Father choose a weapon respectively. Father always randomly chooses one of the n weapons with equal probabilities. Each player can only use the chosen weapon during the game.
  2. When the game starts, Zhang3 and Father have 100 HP each. They make their first shot at the same time.
  3. They keep shooting as quickly as possible. That means, a player shoots instantly whenever he can shoot, until the game ends.
  4. When a player’s HP is reduced to 0 or lower, he dies and the game ends. If the other player is still alive (i.e. has HP higher than 0), then the living player wins the game; otherwise (if the two players die at the same time), each player has 50% probability to win the game.

Zhang3 wants to win the game. Please help her to choose a weapon so that the probability to win is maximized. Print the optimal probability.

Input

  • The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow.
  • For each test case, the first line contains an integer n(1≤n≤1000), the number of weapons in the game.
  • Then n lines follow, the ith of which contains two integers Ai,Di(1 ≤ Ai≤ 100,1 ≤ Di ≤ 10000), representing the Damage and the Delay of each weapon.
  • The sum of n in all test cases doesn’t exceed 2000.

Output

  • For each test case, print a line with a real number p(0≤p≤1), representing the optimal probability.
  • Your answers should have absolute or relative errors of at most 10−6.

Sample Input

2
1
100 100
4
50 50
40 20
30 10
20 100

Sample Output

0.5
0.875

Method

  • 虽然是签到题,但是还是有的一说,如何去选择让张三必胜,那必然张三每次都能选到最好的武器,所以关键点是张三他爸怎么选,一旦没有选到和张三同等级的武器就必输;
  • 所以如何设定武器的等级,显然是根据 伤害达到100时需要的时间的多少,所以用一个map记录不同伤害等级的武器的数量,同时对最高等级的武器进行标记,最后就可以很快的得到结果了(别忘了向上取整!)

Code

详见注释

#include <bits/stdc++.h>

using namespace std;
#pragma GCC optimize(2)
#define ll long long
const int Max = 1e6+3;
const int mod = 1e9+7;

int T, n, x, y;
float p;
map <ll, int> mp;

int main()
{
	scanf("%d", &T);
	while(T--)
	{
		int mint = 0xcffff;
		mp.clear();
		scanf("%d", &n);
		for(int i=1; i<=n; i++)
		{
			scanf("%d%d", &x, &y);
			if(x >= 100) {				//对秒杀等级的武器进行特判 
				mp[0]++;
				mint = min(mint, 0);
			}
			else {
				int temp;
				if(100 % x == 0) temp = (100/x-1)*y;	//向上取整求武器等级 
				else temp = (100/x)*y;
				mp[temp]++;
				mint = min(mint, temp);
			}
		}
		p = 1 - (mp[mint]/(n*1.0))*0.5;
		printf("%.6lf\n", p);
	}
	return 0;
}

蒟蒻一只,欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值