zoukankan      html  css  js  c++  java
  • HDU6803 Blow up the Enemy(贪心/概率)

    Problem Description

    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 A**i

    and Delay D**i

    . When a player shoots with this weapon, his enemy's HP is reduced by A**i

    , then he must wait for D**i

    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 A**i,D**i(1≤A**i≤100,1≤D**i≤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
    

    擦,比赛的时候没仔细看题以为张三也是随机选。。

    签到。由于由于血量已知,很容易就能知道一把武器经过多长时间就能把一个人干趴下。如果一把武器的伤害a能整除100,说明恰好攻击了(frac{100}{a})次,则间隔有(frac{100}{a} - 1)个;如果不能整除,则需要攻击(frac{100}{a} +1)次(向下取整),有(frac{100}{a})个间隔(向下取整)。因此对于所有武器,按把人干翻的时间排序,张三直接拿时间最小的那一把,最终求个概率就行。

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    struct weapon
    {
    	int a, d;
    	int time;
    };
    bool cmp(weapon a, weapon b)
    {
    	return a.time < b.time;
    }
    int main()
    {
    	int t;
    	cin >> t;
    	while(t--)
    	{
    		cin >> n;
    		vector<weapon> v;
    		for(int i = 1; i <= n; i++)
    		{
    			int a, d;
    			scanf("%d%d", &a, &d);
    			int temp;
    			if(100 % a == 0) temp = (100 / a - 1) * d;
    			else temp = 100 / a * d;
    			v.push_back({a, d ,temp});
    		}
    		sort(v.begin(), v.end(), cmp);
    		double ans = 0;
    		for(int i = 0; i < v.size(); i++)
    		{
    			if(v[i].time == v[0].time)
    			{
    				ans += 0.5 * 1ll / n;
    			}
    			else ans += 1ll / n;
    		}
    		cout << 1 - ans << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    MongoDB 学习笔记之 MongoDB导入导出
    快学Scala 第十四课 (读取行,读取字符, 控制台读取)
    MongoDB 学习笔记之 权限管理基础
    MongoDB 学习笔记之 索引
    MongoDB 学习笔记之 游标
    MongoDB 学习笔记之 查询表达式
    MongoDB 学习笔记之 基本CRUD
    MongoDB 学习笔记之 入门安装和配置
    Eclipse设置JVM的内存参数
    cron表达式详解
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13406035.html
Copyright © 2011-2022 走看看