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 }
如果想要优化时间,那么考虑时必须要分析好血量、攻击力、防御力的变化
想做掉怪物首先需要保证本身的攻击力高过怪物的防御,然后枚举攻击力与防御力的增量,求出对应需要的hp,然后找出最小的值,数据范围都在100以内,因此枚举也可做,建议使用比较好分别的命名方式,否则用的时候各种串......
方法二:
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 }