G.Eating Together(LIS,LDS)
牛客假日團隊賽7:
鏈接:https://ac.nowcoder.com/acm/contest/997/G
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
The cows are so very silly about their dinner partners. They have organized themselves into three groups (conveniently numbered 1, 2, and 3) that insist upon dining together. The trouble starts when they line up at the barn to enter the feeding area.
Each cow i carries with her a small card upon which is engraved Di (1 ≤ Di ≤ 3) indicating her dining group membership. The entire set of N (1 ≤ N ≤ 30,000) cows has lined up for dinner but it’s easy for anyone to see that they are not grouped by their dinner-partner cards.
FJ’s job is not so difficult. He just walks down the line of cows changing their dinner partner assignment by marking out the old number and writing in a new one. By doing so, he creates groups of cows like 111222333 or 333222111 where the cows’ dining groups are sorted in either ascending or descending order by their dinner cards.
FJ is just as lazy as the next fellow. He’s curious: what is the absolute mminimum number of cards he must change to create a proper grouping of dining partners? He must only change card numbers and must not rearrange the cows standing in line.
輸入描述:
- Line 1: A single integer: N
- Lines 2…N+1: Line i describes the i-th cow’s current dining group with a single integer: Di
輸出描述: - Line 1: A single integer representing the minimum number of changes that must be made so that the final sequence of cows is sorted in either ascending or descending order
示例1
輸入
復制
輸出
復制
說明
We would need at least two changes to turn this into an increasing sequence (changing both non-1’s to a 1).
However, changing the first “1” to a “3” yields a decreasing sequence with just one change, which is optimal.
題意:
一個只由1,2,3組成的序列,改變最少的數使序列成為上升或下降序列。
很明顯就是求一個最長上升子序列和最長下降子序列,需要修改的數量ans = N -兩個子序列中長度的較大值。
N達到3e4,所以用基于貪心的O(NlogN)解法
AC_code:
總結
以上是生活随笔為你收集整理的G.Eating Together(LIS,LDS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客假日团队赛8:F.Telephone
- 下一篇: P1038 神经网络(拓扑排序)