Luogu 🟢 P2627 [USACO11OPEN] Mowing the Lawn G
題目的難度顏色使用 Luogu 上的分級,由簡單到困難分別為 🔴🟠🟡🟢🔵🟣⚫。
🔗 🟢 P2627 [USACO11OPEN] Mowing the Lawn G
tags: 動態規劃(Dynamic Programming)
單調佇列(Monotone Queue)
單調佇列優化DP
題意
給定長度為 的數列代表效率值,其中第 個元素的值為 。
你可以從中選擇一些元素,要求任意連續被選的元素數量不超過 個。
請計算在此限制下,所能取得的效率值最大總和。
約束條件
思路:單調佇列優化 DP
設 表示前 個元素所能取得的效率值最大總和,那麼我們可以決定上一個閒置(不選)的位置 ,使得 滿足:
- ,即兩個被選元素之間最多間隔 個元素(題目條件)
- 可以等於 ,表示閒置的元素是第 個元素
則狀態轉移方程為:
注意轉移時不能取 ,這是因為根據定義, 可能包含第 個元素,這與我們想要的不選第 個元素的定義不符。故為了符合定義,需要取 。
為了方便計算區間和,定義前綴和:
則狀態轉移可以改寫為:
進一步整理,得到:
注意到查詢區間隨著 遞增「向右平移」,且要求在每個 求 (定義為 ),因此我們可以利用 單調佇列(Monotone Queue) 來動態維護一個遞減序列以便在 時間內取得區間內最大值,整體時間複雜度降低至 。
但需要注意,按照此定義,當 時,需要考慮 ,會出現下標越界,這裡的處理方式是定義 ,並且定義 ,故 ,這樣可以保證 的定義在 時是合法的。
複雜度分析
- 時間複雜度:
- 空間複雜度:
1 | from collections import deque |
類題
寫在最後
PROMPT
masterpiece, best quality, high quality, highres, extremely detailed CG unity 8k wallpaper, extremely detailed, High Detail, vibrant colors, anime style, cover art,
1girl, solo, Koyori, Hakui Koyori, Hakui Koyori (hololive), pink hair, long hair, braids in hair, purple eyes, fox ears, fox tail, black choker, white short-sleeved blouse, pink bow, pink trim on blouse, pink and purple plaid pleated skirt, white thigh garter on left leg, white knee-high socks, lace-trimmed socks, brown loafers,
reaching up to touch cherry blossoms, sitting on stone wall, spring season, cherry blossom trees in full bloom, soft dreamy lighting, urban background with buildings, gentle breeze, sakura petals falling, peaceful atmosphere, outdoors, clear spring day, looking up with wonder
In this enchanting spring scene, Hakui Koyori sits gracefully on a stone wall beneath a canopy of blooming cherry trees. The soft pink petals create a dreamlike atmosphere around her as she reaches up with one hand to gently touch the blossoms. Her expression captures a moment of serene wonder as she gazes upward at the beautiful sakura, with delicate petals occasionally drifting down in the gentle spring breeze.