zoukankan      html  css  js  c++  java
  • Hero

    Description

    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.
     

    Input

    The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)
     

    Output

    Output one line for each test, indicates the minimum HP loss.
     

    Sample Input

    1 10 2 2 100 1 1 100
     

    Sample Output

    20 201
     
    解题思路:题目大意是说,你的伤害值(dps)为1,生命值(hp)是无限的,现在要你用最少的生命值杀光所有的敌人。(贪心算法)
     
     
    过程中所需注意的细节问题:
    1.先杀与后杀的问题,我们不是先杀伤害最大的也不是先杀伤害最小的,先杀的应该是价值最大的(dps/hp取大的先杀)。
    2.用dps/hp时会遇到3/2与2/1的结果都是1,但是我们要先解决的是3/2的那个而不是2/1。所以在除的时候要换一下类型把int换成double。
     
     
     
     
     
    #include <stdio.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    struct p
    {
      int dps;
      int hp;
    }s[60];
    
    bool cmp(p a,p b)
    {
        return (double)a.dps/(double)a.hp>(double)b.dps/(double)b.hp;
    }
    
    int main()
    {
        int j,n,m;
        int  sum;
        while (scanf("%d",&n)!=EOF)
        {   sum=0;m=0;
            memset(s,0,sizeof(s));
            for(int i=0;i<n;i++)
            scanf("%d %d",&s[i].dps,&s[i].hp);
            sort(s,s+n,cmp);
            if(n==1) { printf("%d
    ",s[0].dps*s[0].hp); continue;}
            for(int i=0;i<n;i++)
           {
            m=0;
            m+=s[i].dps;
            for(j=i+1;j<n;j++)
            m+=s[j].dps;
    
            m*=s[i].hp;
            sum+=m;
    
    
           }
            printf("%d
    ",sum);
        }
        return 0;
    }
    

      

  • 相关阅读:
    windows下载
    vue-element-admin改造步骤
    js处理url
    好用的工具
    数据库设计工具
    虚拟机使用
    Mac上编译C
    MAC系统配置
    SQL语法
    SSMP一次请求数据处理过程分析
  • 原文地址:https://www.cnblogs.com/llfj/p/5689667.html
Copyright © 2011-2022 走看看