B. Ultraman vs. Aodzilla and Bodzilla
time limit per test
2.0 s
memory limit per test
1024 MB
input
standard input
output
standard output
Six months ago our hero, formerly known as Huriyyah, was beating all monsters in the land. Now he changed his name to Ultraman and left his beloved land. He is ready to take on a new challenge.
In a remote land, local citizens are suffering from the harassment of two powerful and horrible monsters: Aodzilla and Bodzilla. They eat small children who go out alone and even kill innocent persons. The apprehension of being attacked has overwhelmed people for several decades.
For the good of these unfortunate citizens, Ultraman sets out for the forest which is the main lair of Aodzilla and Bodzilla. In the forest, he faces these two fierce and cruel monsters and fights with them. The health points of Aodzilla and Bodzilla are HPAHPA and HPBHPB respectively, and their attack values are ATKAATKA and ATKBATKB respectively.
They fight in a cave through turn-based battles. During each second, the Ultraman will be attacked by monsters at first, and the damage is the sum of attack values of all alive monsters. Then he will select exactly one monster which is still alive and attack it. The selected monster will suffer a damage of value ii (i.e. its health point will be decreased by ii) where ii represents that Ultraman has launched iiattacks in total, from the beginning to the present, to these two monsters (and the current attack is the ii-th one). That is to say, during the 11-st second, one of these two monsters will be under an attack of damage 11, during the 22-nd second, one of them, if alive, will be under an attack of damage 22, during the 33-rd second, one of them, if alive, will be under an attack of damage 33, and so on. If at some time, the health point of a monster is less than or equal to zero, it will die immediately. The Ultraman will win if both monsters have been killed.
Now, you are asked to develop a strategy to minimize the total damage Ultraman should suffer before he wins the battle. A strategy can be described as a string whose length is the total time that the battle will last. The ii-th character in the string is 'A', if the Ultraman chooses to attack Aodzilla during the ii-th second; otherwise, the ii-th character is 'B', which means Bodzilla will be the target during that second. You are also asked to find the optimal strategy whose string description is the smallest in lexicographical order among all possible optimal strategies.
For two distinct strings ss and tt, if one string is a prefix of the other, then the one with a shorter length is smaller in lexicographical order. In other cases, ss is smaller than tt in lexicographical order if the first character of ss is smaller than the first character of tt, or in case they are equivalent, the second character of ss is smaller than the second character of tt, etc. The case tt is smaller than ss in lexicographical order is defined similarly as the former case.
Input
The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 105105.
For each test case, the only one line contains four integers HPAHPA, HPBHPB, ATKAATKA and ATKBATKB, where 1≤HPA,HPB,ATKA,ATKB≤1091≤HPA,HPB,ATKA,ATKB≤109.
We guarantee that there are at most 100100 test cases with max{HPA,HPB}>103max{HPA,HPB}>103.
Output
For each test case, output a line containing an integer indicating the minimal total damage Ultraman should suffer, and a string describing the optimal strategy such that the string description is the smallest in lexicographical order among all possible optimal strategies. You should output exactly one whitespace between the number and the string.
Example
input
Copy
2 5 15 5 25 5 15 25 5
output
Copy
155 BBBBBA 105 AAABBB
预处理出奥特曼攻击值的前缀和,然后分情况讨论:
一、先打A
先算出打死A B一共所用的时间last,pos为打死A所用的时间,然后若if(s[last]-s[pos]>=hb)可以后面直接打死B,否则需要在前面穿插打一下B,打B的是第num[pos]-lb天。
二、先打B
pos为打死B所用的时间,处理出可以保留的时间为locb=s[pos]-hb;,然后找到大于locb的第一个位置,减去1 lb1,若s[lb1]+s[last]-s[pos](差值为bac)大于la,那么可以按ABA的顺序放置。
否则,需要间断性的放A,定义 left=ha-bac,即打死a还需要的时间。如果 if(left-i>i||left==i),则放A,否则放B,最后再放A,使得A可以被消灭掉。----------太难了吧....
#include //STL通用算法
#include //STL位集容器
#include
#include
#include
#include
#include //复数类
#include
#include
#include
#include
#include //STL双端队列容器
#include //异常处理类
#include
#include //STL定义运算函数(代替运算符)
#include
#include //STL线性列表容器
#include