多校第12場 FZU H Monster (簡單貪心)

來源:互聯網
上載者:User

類似排隊打水問題;

定義每個怪物的性價比bi=怪物的攻擊力/v11對怪物的擊打次數。

對bi進行一次sort 從大到小,一次加和即可。

可以證明先消滅性價比小的 後消滅大的 會使損失值增大,對性價比相同的怎麼處理都不影響結果。

代碼:

要用longlong 。

 

#include <cstdio>#include <algorithm>using namespace std;const int maxn=10010;typedef long long typec;struct Monster{    typec hp,g,cnt;    double bi;}m[maxn];bool cmp (Monster a,Monster b){    return a.bi>b.bi;}int main (){    int cas;    scanf("%d",&cas);    for (int I=1 ; I<=cas ; ++I)    {        int n,atk;        scanf("%d%d",&n,&atk);        for (int i=0 ; i<n ; ++i)        {            scanf("%lld%lld",&m[i].hp,&m[i].g);            m[i].cnt=((m[i].hp+atk-1)/atk);            m[i].bi=(double)m[i].g/(double)m[i].cnt;        }        sort (m,m+n,cmp);        typec ans=0,cnt=0;        for (int i=0 ; i<n ;++i)        {            cnt+=m[i].cnt;            ans+=cnt*m[i].g;        }        printf("Case #%d: %lld\n",I,ans);    }    return 0;}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.