CodeForces 487A Fight the Monster

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int judge(int hy,int ay,int dy,int hm,int am,int dm)//计算特定的攻击与防御之下,需要加多少hp
 5 {
 6     if(am <= dy)
 7         return 0;
 8     int d1 = am - dy;
 9     //cout<<" d1 = "<<d1<<endl;
10     int t1 = (hy + d1 - 1) / d1;
11     //cout<<" t1 = "<<t1<<endl;
12     int d2 = ay - dm;
13     //cout<<" d2 = "<<d2<<endl;
14     int t2 = (hm + d2 - 1) / d2;
15     //cout<<" t2 = "<<t2<<endl;
16     if(t1 > t2)
17         return 0;
18     int t = t2 * d1 - hy + 1;
19     //cout<<" t = "<<t<<endl;
20     return t;
21 }
22 int main()
23 {
24     int hy,ay,dy,hm,am,dm,hp,ap,dp;
25     cin>>hy>>ay>>dy;
26     cin>>hm>>am>>dm;
27     cin>>hp>>ap>>dp;
28     long long ans = 0;
29     if(dy >= am)  //y防御高过m攻击时,只要攻击高过防御即可
30     {
31         if(ay > dm)
32             cout<<0<<endl;
33         else
34         {
35             ans = ap * (dm - ay + 1);
36             cout<<ans<<endl;
37         }
38     }
39     else
40     {
41         long long ans0 = 0;//保证y攻击高过m防御需要的代价
42         if(ay <= dm)
43         {
44             ans0 = ap * (dm - ay + 1);
45             ay = dm + 1;
46         }
47         //cout<<"ans = "<<ans<<endl;
48         ans = ans0 + judge(hy,ay,dy,hm,am,dm) * hp;
49         //cout<<"ans = "<<ans<<endl;
50         for(int i = 0; i*ap <= ans; i++) //枚举增加攻击防御的值并计算对应需要的hp,找最小值
51         {
52             for(int j = 0; i*ap + j*dp<= ans; j++)
53             {
54                 long long tmp = ans0 + i * ap + j * dp + judge(hy,ay+i,dy+j,hm,am,dm) * hp;
55                 if(tmp < ans)
56                 {
57                     //cout<<" i = "<<i<<" j = "<<j<<endl;
58                     ans = tmp;
59                 }
60             }
61         }
62         cout<<ans<<endl;
63     }
64     return 0;
65 }

如果想要优化时间,那么考虑时必须要分析好血量、攻击力、防御力的变化

方法二:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <stdio.h>
 8 #include <algorithm>
 9 #include <iostream>
10 #include <cstdio>
11 #include <cstring>
12 #include <algorithm>
13 #define sc1(x) scanf("%s",&(x))
14 #define sc(x) scanf("%d",&(x))
15 #define sc3(x,y,z) scanf("%d%d%d", &(x), &(y), &(z))
16 #define pf(x) printf("%d\n", x);
17 #define pf1(x) printf("%s\n", x);
18 #define p(x) printf("%d ", x)
19 #define P printf("\n")
20 #define pf2(x, y) printf("%d %d\n", x, y)
21 #define FOR(i,b,e) for(int i=b; i<=e; i++)
22 #define FOR1(i,b,e) for(int i=b; i>=e; i--)
23 #define CL(x,y) memset(x,y,sizeof(x))
24 #define INF 0x7fffffff
25 int min(int x, int y)
26 {
27         return x < y ? x : y;
28 }
29 int main()
30 {
31     int wh, wa, wd, mh, ma, md, ch, ca, cd;
32     sc3(wh, wa, wd);
33     sc3(mh, ma, md);
34     sc3(ch, ca, cd);
35     int cost = INF;
36
37     for(int da=0;da<=200;da++)
38     {
39         for(int dd=0; dd <=200;dd++)
40         {
41             for(int dh=0; dh<=1000;dh++)
42             {
43             if(wa+da-md <= 0) continue;
44             int t = (mh-1) / (wa+da-md) + 1;
45             if(dh < t*(ma-wd-dd)-wh+1)     continue;
46             cost = min(cost, ca*da+cd*dd+ch*dh);
47             }
48         }
49     }
50     pf(cost);
51     return 0;
52 }

直接暴力枚举,通过比较获取最小值

方法三:暴力+剪枝

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int hy,ay,dy,hm,am,dm,h,a,d;
 6     cin>>hy>>ay>>dy>>hm>>am>>dm>>h>>a>>d;
 7     int i,j,k,ans=100000,sum,tmp;
 8     for(j=0;j<=max(0,dm+hm-ay);j++)
 9     {
10         for(k=0;k<=max(0,am-dy);k++)
11         {
12             if(ay+j-dm<=0)
13                 break;
14             if(ay+j-dm>0)
15             {
16                 tmp=hm/(ay+j-dm);
17                 if(hm%(ay+j-dm)!=0)
18                     tmp++;
19                 i=max(0,tmp*max(0,(am-dy-k))-hy+1);
20             }
21             sum=i*h+j*a+k*d;
22            if(ans>sum)
23                 ans=sum;
24         }
25     }
26     cout<<ans<<endl;
27     return 0;
28 }

时间: 2024-11-01 18:23:37

CodeForces 487A Fight the Monster的相关文章

C - Fight the Monster(暴力)

题意就不说了 思路:要分析好对什么进行枚举,如果对血量也进行枚举的话,至少要枚举到10^4,加上对攻击力和防御力进行枚举10^2 * 10^2,可能会TLE. 因为monster的血量是固定的,所以可以通过对攻击力和防御力枚举,求出最少需要的攻击次数,和这个攻击次数下保证的血量,每次比较取max即可... 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring&g

CodeForces - 954D Fight Against Traffic

Fight Against Traffic Little town Nsk consists of n junctions connected by m bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junc

Codeforces - 102222H - Fight Against Monsters - 贪心

https://codeforc.es/gym/102222/problem/H 题意:有一堆怪兽,怪兽有HP和ATK.你有一个英雄,英雄每次先被所有怪兽打,然后打其中一个怪兽.打的伤害递增,第一次1,第二次2,以此类推. 为什么感觉是贪心呢?证明一波. 首先开始打一个怪兽肯定一直打到死为止.那么打死他要求的次数可以二分出来(其实暴力也可以).两只怪兽交换打的顺序会不会变好? 先打第一只怪兽: \(num_1*sumatk+num_2*(sumatk-atk_1)\) 先打第二只怪兽: \(nu

Codeforces 1296D - Fight with Monsters

题目大意: n 只怪兽,每只的血量为 h[i] ,你的攻击力为 a ,你的对手攻击力为 b 打每只怪兽时,都是你先出手,然后你的对手出手,这样轮流攻击 如果是你给予了怪兽最后一击,你就能得到一分 你还有 k 次机会能让你的对手暂停行动一回合 问你最多能拿到多少分 解题思路: 记你加上你的对手两个人各攻击一次造成的伤害为 s=a+b 贪心可得,如果想节省那 k 次机会,应该和对手一起把怪兽耗到只剩一点血,即能在最后一回合杀死的情况时 即怪兽减去的血量为 s 的倍数,表示经过了这么多回合后,怪兽的血

【codeforces #278(div 1)】ABCD题解

A. Fight the Monster time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each hav

(一)、写一个怪物的类,类中有属性姓名(name),攻击力(attack),有打人的方法(fight)。(方法的重写)

(一).写一个怪物的类,类中有属性姓名(name),攻击力(attack),有打人的方法(fight).再写两个子类:红猪的类:类中有属性姓名(name),攻击力(attack),颜色(color).红猪也有一个打人的方法(fight),要求在此方法中输出"XX猪是XX颜色的,打掉了人XX点血".蛇类:类中有属性姓名(name),有攻击力属性(attack),有几条脚属性(foot).蛇也有一个打人的方法(fight),要求在此方法中输出"XX蛇用XX条脚跑到人面前去打人,打

Running Hero.

Running Hero开发流程 一.需求分析 Running Hero是在接到大作业以前就已经有的想法,一直没有好的契机好好策划实现,正巧通过此次作业的机会,将它实现出来了. Running Hero 的初步定义是一款结合了传统的横版闯关以及当下最流行的酷跑模式的新型横版手游,大体上分成两个模块,单机闯关以及网络联机对战,由于技术不太成熟以及时间原因,暂时只开发了单机闯关板块. Running Hero 整体风格设计为卡通类型的,素材(能力有限,非原创)出自66rpg素材网,游戏流程设计为:

Codeforces 918C - The Monster

918C - The Monster 思路1: 右键在新窗口打开图片 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) int main(){ ios::sync_with_stdio(false); cin.tie(0); string s; cin>>s; int ans

Codeforces #617 (Div. 3) D. Fight with Monsters(贪心,排序)

There are nn monsters standing in a row numbered from 11 to nn . The ii -th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp. You and your opponent are fighting these