gawkmaster069's blog

By gawkmaster069, history, 6 months ago, In English

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.

Super Mario Problem

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.

  • Vote: I like it
  • -99
  • Vote: I do not like it

»
6 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

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

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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.

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think greedy is not a simple knowledge.

It can be more hard

It also can be very simple.

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    yeah it varies from very easy to sometimes very hard i dedicated this post only for newbies hence provided easier aspect of the same.

»
6 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

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.

  • »
    »
    6 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    why am i not banned then? i explained myself above.

    • »
      »
      »
      6 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      There exists something called false negatives. Anyways I have attached a different submission, try explaining?

      • »
        »
        »
        »
        6 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        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?

        • »
          »
          »
          »
          »
          6 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          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.

          • »
            »
            »
            »
            »
            »
            6 months ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it

            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.

            • »
              »
              »
              »
              »
              »
              »
              6 months ago, hide # ^ |
               
              Vote: I like it +3 Vote: I do not like it
              00:00:00  Participant has been assigned to the room 165
              00:08:35  A  Accepted [main tests] → 346671470
              00:11:39  A  has been locked
              00:29:27  C  Accepted [main tests] → 346683639
              00:30:53  C  has been locked
              00:56:54  B  Time limit exceeded on pretest 3 [pretests] → 346696071
              01:01:29  B  Wrong answer on pretest 1 [pretests] → 346698035
              01:03:56  B  Wrong answer on pretest 1 [pretests] → 346699047
              01:05:52  B  Memory limit exceeded on pretest 1 [pretests] → 346699848
              01:09:07  B  Wrong answer on pretest 1 [pretests] → 346701211
              01:15:15  E  Wrong answer on pretest 1 [pretests] → 346703729
              01:17:26  E  Memory limit exceeded on pretest 1 [pretests] → 346704561
              01:18:23  E  Time limit exceeded on pretest 4 [pretests] → 346704958
              01:20:01  E  Wrong answer on pretest 1 [pretests] → 346705582
              01:23:17  D  Accepted [main tests] → 346706791
              01:24:17  D  has been locked
              

              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.

              • »
                »
                »
                »
                »
                »
                »
                »
                6 months ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                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.

    • »
      »
      »
      5 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      banned

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i am a newbie- why so many downvotes for this blog ?