Can somebody explain why the Greedy solution work for this question and why it works only when using armor not health as a rule to make a descision and what will be its time complexity.
Question link — https://www.spoj.com/problems/DIEHARD/
#include<iostream>
#include<bits/stdc++.h>
#define ll long long int
#define F first
#define S second
#define foi(i,s,e) for(ll i = s;i <= e;i++)
#define fod(i,s,e) for(ll i = s;i >= e;i--)
using namespace std;
#define mod 1000000007
void fast()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
struct cmp{
bool operator()(pair<ll,ll>a,pair<ll,ll>b)
{
return a.first < b.first;
}
};
int main()
{
fast();
ll t;
cin >> t;
foi(z,0,t-1)
{
int health, armor;
cin >> health >> armor;
int time = 0;
while(1)
{
if(health <= 0 || armor <= 0)
break;
if(time % 2 == 0)
{
health += 3;
armor += 2;
}
else
{
if(armor > 10)
{
health -= 5;
armor -= 10;
}
else
{
health -= 20;
armor += 5;
}
}
time++;
}
cout << time-1 << "\n";
}
}