What is Greedy?
A greedy algorithm is a simple strategy where, at every step, you choose the option that looks the best at that moment. You don’t worry about future consequences or long-term planning. You simply pick the move that gives the maximum immediate benefit. The idea is that by always taking the locally best choice, you aim to reach an overall good or optimal solution during your loop process.
A Newbie Problem Example.
t = int(input())
for _ in range(t):
a, b, k = map(int, input().split())
total = a * b
rows = a
cols = b
moves = 0
while total > k:
if b >= a and rows > 0:
total -= b
rows -= 1
else:
total -= a
cols -= 1
moves += 1
print(moves)
Above is the solution for the given problem , explanation below.
In the above given problem you don't think much , you keep reducing the rows and column until m*n falls below k which is condition for the army retreatment, however sometimes removing a row might give more advantage than removing a column so for that you check each row and column value too before removing and keep the loop on till it reaches the value k.
How to spot a greedy problem?
You can easily spot such greedy problems by seeing if the numbers or the given numbers needs to be changed in the process and a condition being linked to that, these type of questions always follow greedy approach however there might be much more complex greedy problems much more deeper than the above stated one but this is fine for a good start.
Open for suggestions in the comments.








Just curious ... during Codeforces Global Round 30 (Div. 1 + Div. 2) on the greedy problem 2164C - Dungeon, after struggling for about 30 minutes with several submissions, how did you manage to debug everything so quickly and switch from a Python WA to a Java AC in just two minutes? 347729427 and 347730432
my python code had correct logic at a point giving only tle, after seeing that i switched to code same logic in java while side by side i tried to make minor changes in python code to see if it passes because usually switching languages attracts unwanted attention but attempts with python failed and the same logic in java passed.
I think greedy is not a simple knowledge.
It can be more hard
It also can be very simple.
yeah it varies from very easy to sometimes very hard i dedicated this post only for newbies hence provided easier aspect of the same.
Why are you fighting for legitimacy by making such stupid blogs, cheater.
Your submission: 346706791, some random telegram skipped solution 346713465, not mentioning the fact that you are proficient in a dozen of languages.
why am i not banned then? i explained myself above.
There exists something called false negatives. Anyways I have attached a different submission, try explaining?
i used ideone on public by mistake and it got leaked from there and maybe then later landed on telegram. but after reading the blog about codeforces skipping i realized it and stopped using ideone. i got the skip warning and didn't get banned everyone makes some mistake ig?
Nice defence, anyways here's a solution submitted before you 346706107. Yes everyone makes mistake, just like you did by submitting exact same code but only changing the language.
ideone was public anyone can copy my codes while i am testing them in my local system and submit it before me, while i know all major languages intermediate level. don't accuse if you can't prove with 100% accurate accusations.
Surely you were working on D while submitting E and B midway, somebody stole your code, submitted accepted YAY!. You too got it right in 3 minutes.
Anyways I hope you do get banned in the near future and get back to your miserable life.
yeah, can't one solve more than one question at time? that day queue was very long so needed to solve multiple at the same time. come with better solid proofs.
banned
i am a newbie- why so many downvotes for this blog ?