看的是这个巨佬的博客
看来这篇博客,然后一点点看懂的,太具有思维性了,详解了一下。(没得思维
#include<bits/stdc++.h>
#define il inline
#define pb push_back
#define ms(_data,v) memset(_data,v,sizeof(_data))
#define SZ(a) int((a).size())
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int N=1e5+5;
//il int Add(int &x,ll y) {return x=x+y>=mod?x+y-mod:x+y;}
//il int Mul(int &x,ll y) {return x=x*y>=mod?x*y%mod:x*y;}
ll num[N];
int main() {
for (ll i=1; i<N; ++i) {
num[i]=num[i-1]+1LL*i;
}
int T;
scanf("%d", &T);
while(T--) {
ll ha,hb,ta,tb;
ll resa=0,resb=0;
scanf("%lld%lld%lld%lld",&ha,&hb,&ta,&tb);
string ansa,ansb;
//想使得收到的伤害最小,那么A和B必须先解决一个,分类讨论
/*先解决A*/
ll pos = lower_bound(num+1,num+100004,ha)-num;//A,解决A最小的时间
ll last = lower_bound(num+1,num+100004,ha+hb)-num;//A+B,总时间,因为伤害最小,所以必须而且能在last时结束所有怪物
ll duo = num[pos]-ha;
ll tsa = 1LL*last*tb+1LL*pos*ta;//总共承受的攻击
if(num[last]-num[pos]>=hb) {//如果后面的可以直接解决B,那就没必要在前面插B
for (ll i = 1; i <= pos; ++i) {
ansa+='A';
}
} else { //反之插在最优的地方,num[pos]-ha(可以解决A的情况下,给B最多的伤害,也是能放最后的地方,满足字典序最小
for (ll i = 1; i <= pos; ++i) {
if(i!=duo) ansa+='A';
else ansa+='B';
}
}
for (ll i = pos+1; i <= last; ++i) {//后面都填B
ansa+='B';
}
/*先解决B*/
pos = lower_bound(num+1,num+100004,hb)-num;//B 解决B最快的时间
duo = num[pos]-hb; //解决B还多出来的部分
ll pos2 = upper_bound(num+1,num+100004,duo)-num-1;//A从前往后放可以放到哪
ll hou = num[last]-num[pos];//后面还有的伤害
ll tsb = 1LL*last*ta + 1LL*pos*tb; //总共承受的攻击
if(hou+num[pos2]>=ha) {//后面的伤害加上A全放前面的伤害足够打死A
for (ll i = 1; i <= pos2; ++i) {//全部插在最前面
ansb+='A';
}
for (ll i = pos2+1; i <= pos; ++i) {
ansb+='B';
}
for (ll i = pos+1; i <= last; ++i) {
ansb+='A';
}
} else {
ll left = ha - hou ;//前面还需要解决A的血量,(前面是肯定可以解决的,因为last时肯定可以完成
for (ll i = 1; i <= pos; ++i) {
if(left-i>i || left==i) { //保证后面的可以将left解决
left-=i;
ansb+='A';
}
else ansb+='B';
}
for (ll i = pos+1; i <= last; ++i) {
ansb+='A';
}
}
//根据伤害量和字典序选择最优方案
if(tsb<tsa) {
printf("%lld ",tsb);
cout<<ansb<<endl;
}
else if(tsb>tsa) {
printf("%lld ",tsa);
cout<<ansa<<endl;
}
else if(ansa<ansb) {
printf("%lld ",tsa);
cout<<ansa<<endl;
}
else {
printf("%lld ",tsb);
cout<<ansb<<endl;
}
}
}
本文通过算法探讨了在游戏战斗中如何使角色受到的伤害最小化,使用C++编程实现,通过预计算和分类讨论策略,找到最优的攻击序列,确保在限定时间内击败所有敌人,同时最小化承受的总伤害。
593

被折叠的 条评论
为什么被折叠?



