1. 程式人生 > >【Codeforces Round 332 (Div 2)C】【貪心】Day at the Beach 最多區間數劃分使得區間排序構成全域性排序

【Codeforces Round 332 (Div 2)C】【貪心】Day at the Beach 最多區間數劃分使得區間排序構成全域性排序

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

At the end of the day there werencastles built by friends. Castles are numbered from1ton

, and the height of thei-th castle is equal tohi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that conditionhi ≤ hi + 1holds for allifrom1ton - 1.

Squidward suggested the following process of sorting castles:

  • Castles are split intoblocks — groups ofconsecutivecastles. Therefore the block fromitojwill include castlesi, i + 1, ..., j. A block may consist of a single castle.
  • The partitioning is chosen in such a way that every castle is a part ofexactlyone block.
  • Each block is sorted independently from other blocks, that is the sequenceh
    i, hi + 1, ..., hj
    becomes sorted.
  • The partitioning should satisfy the condition that after each block is sorted, the sequencehibecomes sorted too. This may always be achieved by saying that the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.