Why this greedy approach is correct?

Revision en1, by tgbaodeeptry, 2021-05-23 18:56:13

Hello guys, Today, I'm trying to code this problem: 545C - Woodcutters. I used DP to solve it and I have read problem's Editorial (https://mirror.codeforces.com/blog/entry/17982). And I knew that there still have another approach to solve it: Greedy.

Also this problem can be solved by the next greedy algoritm. Let's fell leftmost tree to the left (it always doesn't make an answer worse). After that, try to fell the next tree. If we can fell it to the left, let's do it (because it also always doesn't make an answer worse). If we can't, then try to fell it to the right. If it is possible, let's do it. Last step is correct because felling some tree to the right may only prevent the next tree's fallen. So we may "exchange" one tree to another without worsing an answer.

But I don't know, why if a tree can fall right we should do it? (I think there are some cases that it will make answer worse ( may be :( )

Thanks very much, guys

Tags greedy

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English tgbaodeeptry 2021-05-23 18:56:13 991 Initial revision (published)