Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define all(x) x.begin(),x.end()
- #define endl '\n'
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- const int INF = 0x3f3f3f3f;
- const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
- const int MOD = 1000000007;
- const int dx[] = { 0, 0, -1, 1, 1, -1, 1, -1};
- const int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1};
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(NULL);
- int t;
- cin >> t;
- while(t--){
- ll hpA, hpB, atkA, atkB;
- cin >> hpA >> hpB >> atkA >> atkB;
- int mx = 0;
- ll total = 0;
- while(total < hpA + hpB){
- mx++;
- total += mx;
- }
- pair<ll, string> ans(INFLL, string(mx, 'B'));
- {// Kill A first
- string opS(mx, 'B');
- ll op = 0;
- ll sum = 0;
- int i=1;
- //Add 'A' while is needed
- for(; sum < hpA; i++){
- sum += i;
- opS[i-1] = 'A';
- op += atkA + atkB;
- }
- if(total-sum < hpB){
- for(int j=i-1; j>=1; j--){
- if(opS[j] == 'A' and sum-j >= hpA and total-sum+j >= hpB){
- opS[j - 1] = 'B';
- break;
- }
- }
- }
- for(; i<=mx; i++){
- op += atkB;
- }
- ans = min(ans, {op, opS});
- }
- { // Kill first B
- string opS(mx, 'A');
- ll op = 0;
- ll sum = 0;
- int i=1;
- for(; sum < hpB; i++){
- sum += i;
- opS[i-1] = 'B';
- op += atkA + atkB;
- }
- int last = -1;
- for(int j=1; j<i; j++){
- if(sum-j >= hpB){
- opS[j-1] = 'A';
- sum -= j;
- last = j;
- }
- }
- if(total-sum < hpA){
- assert(last != -1);
- opS[last - 1] = 'B';
- sum += last;
- ll x = hpA - (total - sum);
- opS[x - 1] = 'A';
- }
- for(; i<=mx; i++){
- op += atkA;
- }
- ans = min(ans, {op, opS});
- }
- cout << ans.F << " " << ans.S << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement