Upgrade to Pro — share decks privately, control downloads, hide ads and more …

響け!動的計画法 (DP) 入門〜個人的まとめ

Tomoya
September 18, 2019

響け!動的計画法 (DP) 入門〜個人的まとめ

動的計画法(DP)の基礎知識から例題、考え方、解説コードまで記載した個人的メモ

Tomoya

September 18, 2019
Tweet

Other Decks in Programming

Transcript

  1. ༷ʑͳ%1 w໯͏%1 w഑Δ%1 w֬཰%1 wCJU%1 wܻ%1 wظ଴஋%1 w۠ؒ%1 w໦%1 πϦʔ%1

     wήʔϜ%1   w෦෼ྻ%1 wૠೖ%1 wൈڈ%1 w໭͢%1 w࿈݁%1 ϑϩϯςΟΞ๏  w5SJF%1 wશํҐ໦%1 wΠϯϥΠϯ%1 w"MJFO%1 ࠓ೔͸͜͜·Ͱʂ ໰୊ࢀরઌ"U$PEFS ղ౴63-Λ֤εϥΠυʹهࡌ͍ͯ͠·͢
  2.  φοϓαοΫ໰୊   ༰ྔa඼෺ ΓΜ͝ ͪ͘Θ Έ͔Μ ΋΍͠ 

          Ձ஋ ॏ͞ ΓΜ͝ ԁ  ͪ͘Θ ԁ  Έ͔Μ ԁ  ΋΍͠ ԁ  ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ԁ ྫ୊  C = 7 N = 4 φοϓαοΫʹೖͬͨ඼෺ͷՁ஋ςʔϒϧ ԁ ԁ ԁ ԁ ԁ ԁ ԁ 2 Ձ஋ॏ͞ ॱͰᩦཉʹ
 ଍͠߹ΘͤΕ͹͍͍ͷͰ͸  "࠷దղʹͳΒͳ͍ ༰ྔ ͷφοϓαοΫͱ छྨͷ඼෺ Ձ஋ ॏ͞ ͕༩͑ΒΕͨ࣌ 
  Λ௒͑ͳ͍ൣғͰՁ஋૯࿨Λ࠷େԽ͢ΔΑ͏ͳ඼෺ͷબͼํΛ౴͑Α C N C 1 ≤ N ≤ 100 1 ≤ C ≤ 105 1 ≤ wi ≤ C 1 ≤ vi ≤ 109 ඵҎ಺ vi wi &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ%,OBQTBDLIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@E
  3. ೋछྨͷه๏   w ઴Խࣜ
 
 
 w ϝϞԽ࠶ؼ 1SPT


    ؔ਺ݺͼग़͠ʹΑΔελοΫΦʔόʔϑϩʔ̈͸ൃੜ͠ͳ͍ ΠϯϥΠϯ%1 ηά໦ͰՃࢉ ͷΑ͏ͳߴ଎ԽςΫ͕ద༻Ͱ͖Δ ίʔφʔέʔεߟྀͷͨΊͷJGจ͕૿͑ΔIUUQJEFPOFDPN-J;R ̈࠶ؼͱελοΫɾΦʔόʔϑϩʔ IUUQXXXGPTLVJTLZPUPVBDKQdJHBSBTIJDMBTTQMSFDJUFSIUNM 1SPT
 τϙϩδΧϧιʔτ̉ෆཁ ίʔφʔέʔεͰίʔυ͕؆ૉ ̉τϙϩδΧϧιʔτ IUUQTFOXJLJQFEJBPSHXJLJ5PQPMPHJDBM@TPSUJOH dp[i][c] = dp[i − 1][c] JG඼෺Λબ͹ͳ͍ JG඼෺ΛબͿ͔ͭ c − wi ≥ 0 max(dp[i − 1][c − wi ] + vi , dp[i − 1][c]) {  ·Ͱͷ඼෺ͷத͔Βॏ͕͞ʹͳΔΑ͏ʹબΜͩ࣌ͷՁ஋ͷ૯࿨ͷ࠷େ஋ dp[i][c] i c Long long solve(long long i, long long c){ if(dp[i][c] != -1) return dp[i][c]; if(i == 0) return dp[i][c] = 0; if((c-w[i]) >= 0) return dp[i][c] = max(solve(i-1,c-w[i])+v[i], solve(i-1,c)); else return dp[i][c] = solve(i-1,c); } IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT
  4. ໯͏%1഑Δ%1 w ໯͏%1   dp[i][c] = dp[i − 1][c]

    JG඼෺Λબ͹ͳ͍ JG඼෺ΛબͿ͔ͭ c − wi ≥ 0 max(dp[i − 1][c − wi ] + vi , dp[i − 1][c]) { &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ%,OBQTBDLIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@E dp[i + 1][c + wi ] = max(dp[i][c] + vi , dp[i + 1][c + wi ]) JG  ഑Γઌ͕༰ྔҎ಺ c + wi ≤ C dp[i + 1][c] = max(dp[i + 1][c], dp[i][c]) IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT ʜ J J J  ʜ DXJ EQ<J><DX@J> D EQ<J><D> EQ<J><D> EQ<J ><D> D XJ EQ<J ><D X@J> w ഑Δ%1  ΁ঢ়ଶΛભҠ dp[i][c]  ͔Βঢ়ଶΛભҠ dp[i][c]  ·Ͱͷ඼෺ͷத͔Βॏ͕͞ʹͳΔΑ͏ʹબΜͩ࣌ͷՁ஋ͷ૯࿨ͷ࠷େ஋ dp[i][c] i c
  5. ֬཰%1   w ֬཰Ͱঢ়ଶΛද͢%1 ྫEQ<J>ʙͱͳΔ֬཰  Λਖ਼ͷح਺ͱ͠·͢ɻ  ຕͷίΠϯ͕͋Γ·͢ɻίΠϯʹ͸

    ͱ൪߸͕ৼΒΕ͍ͯ·͢ɻ ֤ ʹ͍ͭͯɺίΠϯΛ౤͛Δͱɺ֬཰ Ͱද͕ग़ͯɺ֬཰ Ͱཪ͕ग़·͢ɻ ଠ࿠܅͸ ຕͷίΠϯΛ͢΂ͯ౤͛·ͨ͠ɻ ͜ͷͱ͖ɺදͷݸ਺͕ཪͷݸ਺Λ্ճΔ֬཰ΛٻΊ͍ͯͩ͘͞ɻ
 
 ೖྗ͸͢΂ͯ੔਺Ͱ͋Δɻ   N N 1,2,…, N i (1 ≤ i ≤ N) i pi 1 − pi N 1 ≤ N ≤ 102 1 ≤ a1 < a2 < ⋯ < aN ≤ K &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ*$PJOTIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@J EQ<><> GPS JOUJJOJ GPS JOUKKJ K \ EQ<J ><K > EQ<J><K> Q<J> EQ<J ><K> EQ<J><K>  Q<J>  ^ EPVCMFBOT GPS JOUJO JO J BOT EQ<O><J> DPVUTFUQSFDJTJPO  BOTFOEM IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT EQ<J><K>Jຕ໨ͷίΠϯ·Ͱ౤͛ͨͱ͖  ද͕KຕͰΔ֬཰ w ํ਑ͱղ౴ྫ ঢ়ଶͷભҠΛߟ͑Δͱ ҎԼͷࣜʹͳΔ
 ίΠϯJ͕දͷ৔߹  ίΠϯ͕දͷ֬཰ + = dp[i][j] * i  ίΠϯ͕ཪͷ֬཰ + = dp[i][j] * i dp[i + 1][j + 1] dp[i + 1][j] ίΠϯJ͕ཪͷ৔߹
  6. CJU%1   w CJUͰঢ়ଶΛද͢%1 ྫEQ<CJU><J>  w ੍໿͕/ͳͲͱখ͍࣌͞͸େ఍͜Ε 

    ඖͷ͏͗͞ ʙ/ ͕͍·͢ɻ͜ΕΒ ඖͷ͏͕͗͞ెڝ૸Λ͠·ͨ͠ɻ
 ಉண͸͍·ͤΜͰͨ͠ɻߴڮ܅͸ ਓͷ؍٬͔Β৘ใΛूΊ·ͨ͠ɻ ൪໨ͷ؍٬ʹΑΔͱɺ͏͗͞ ͸͏͗͞ ΑΓ΋ઌʹΰʔϧͨͦ͠͏Ͱ͢ɻ ͢΂ͯͷ؍٬ͷ৘ใʹ߹க͢ΔΑ͏ͳணॱ͕Կ௨Γߟ͑ΒΕΔ͔ٻΊ͍ͯͩ͘͞ɻ          ͷ૊͸͢΂ͯ૬ҟͳΔɻ ͢΂ͯͷ؍٬ͷ৘ใʹ߹க͢ΔΑ͏ͳணॱ͕গͳ͘ͱ΋ͻͱͭଘࡏ͢Δɻ N N M i xi yi 2 ≤ N ≤ 16 1 ≤ M ≤ N(N − 1)/2 1 ≤ xi , yi ≤ N xi ≠ yi (xi , yi ) "U$PEFS#FHJOOFS$POUFTU%ెڝ૸IUUQTBUDPEFSKQDPOUFTUTBCDUBTLTBCD@E ྫ   N = 4 M = 3 w ͸ΑΓઌʹΰʔϧ w ͸ΑΓઌʹΰʔϧ w ͸ΑΓઌʹΰʔϧ ύλʔϯaணॱ Ґ Ґ Ґ Ґ                              ͏͗͞ શ෦Ͱ௨Γ
  7. CJU%1ίʔυ   IUUQTBUDPEFSKQDPOUFTUTBCDTVCNJTTJPOT EQ<CJU>౸ணॱͷ௨Γ਺ EQ<> GPS JOUCJUCJU O CJU

    \CJU͔ΒCJU·Ͱ GPS JOUKKOK \ JG CJUK  DPOUJOVFطʹ౸ணͨ͠K͸ύεɹ CPPMGUSVF
 H<K><L>K͸LΑΓઌʹ౸ண L͕طʹ౸ண͍ͯͯ͠K͕ະ౸ணͳΒGBMTF GPS JOULLOL JG CJUL  H<K><L> GGBMTF JG G EQ<CJUc K > EQ<CJU>ྫ͑͹ CJUʹCJUΛ଍͠߹ΘͤΔ ^ ^ DPVUEQ< O >FOEM "U$PEFS#FHJOOFS$POUFTU%ెڝ૸IUUQTBUDPEFSKQDPOUFTUTBCDUBTLTBCD@E w ํ਑ͱղ౴ྫ w CJUͷ֤ܻ͕Λද͠ ౸ண ະ౸ண
 ྫCJUʜ ͕౸ண  ͸ະ౸ண w CJUঢ়ଶͷભҠΛߟ͑Δ CJU CJU CJU
  8. ܻ%1   w ৚݅Λຬ͔ͨ͢ ্ݶ஋͔൱͔ ౳ͷঢ়ଶΛද͢%1
 ྫEQ<ܻ਺><৚݅><ະຬPS౳͍͠ͷϑϥά>৔߹ͷ਺  w

    TNBMMFSϑϥάΛઃ͚ͯ ৚݅΍্ݶͷ؅ཧΛߦ͏ w ੍໿͕,?ͳͲͱେ͖͍࣌͸େ఍͜Ε  Ҏ্ ҎԼͷ੔਺ͷ͏ͪɺेਐදهʹ͓͚Δ֤ܻͷ਺ࣈͷ૯࿨͕ ͷഒ਺Ͱ͋ΔΑ ͏ͳ΋ͷ͸ԿݸͰ͠ΐ͏͔  Ͱׂͬͨ༨ΓΛٻΊ͍ͯͩ͘͞ɻ    1 K D 1e9 + 7 1 ≤ K < 1010000 1 ≤ D ≤ 100 &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ4%JHJU4VNIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@T ྫ, %  EHU㱡 ෦ ͕ҎԼ ,ҎԼ ͳΒՃࢉ͢Δ  EHU㱡͕ҎԼ͔ͭEHU㱢 ෦ ͕ҎԼͳΒՃࢉ  EHU㱡͕ҎԼ ,ະຬ ͳΒՃࢉ EHUͷ࣌ ʜ ʜ w ํ਑ͱղ౴ྫ w ,ҎԼͷ੔਺ΒΛઌ಄͔Βܻͣͭ਺ࣈΛݟΔ w ֤ܻͷ਺ࣈͷ૯࿨ʹ͍ͭͯɺ%Ͱ৒༨ΛͱΔ w ͋Δ੔਺ͷ਺஋͕,ҎԼͳΒ͹ɺՃࢉ͢Δ
  9. ܻ%1ίʔυ   &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ4%JHJU4VNIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@T IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT EQ<EHU><E><JTMFTT>ઌ಄͔Β֬ఆܻͨ͠EHU ֤ܻͷ਺ࣈͷ࿨Λ%Ͱׂͬͨ஋Ë 
 ,ະຬஸ౓ͷϑϥάJTMFTTΛ࣋ͭ੔਺ͷ਺ EQ<><><>

    GPS JOUEHUEHU/EHU GPS JOUEE%E GPS JOUJTMFTTJTMFTTJTMFTT \ GPS JOUOYUOYUOYU \ JG OYU,<EHU> ͋Δ੔਺ͷܻEHU͕,ͷEHUΑΓখ͍͞৔߹ʜJTMFTTʹՃࢉ
  EQ<EHU >< E OYU %><> EQ<EHU><E><JTMFTT>.0% .0% FMTFJG OYU,<EHU> ͋Δ੔਺ͷܻEHU͕,ͷEHUͱಉ͡৔߹ʜ֤JTMFTTʹՃࢉ  EQ<EHU >< E OYU %><JTMFTT> EQ<EHU><E><JTMFTT>.0% .0% FMTF͋Δ੔਺ͷܻEHU͕,ͷEHUΑΓେ͖͍৔߹ʜJTMFTTͳΒ͹Ճࢉ JG JTMFTT  EQ<EHU >< E OYU %><JTMFTT> EQ<EHU><E><JTMFTT>.0% .0% ^ ,ͱ౳͍͠ ,ະຬ DPVU EQ</><><> EQ</><><> .0% .0% .0%FOEM ̈ " # $͸"$ #$ͱ౳Ձ
  10. ༷ʑͳ%1 w໯͏%1 w഑Δ%1 w֬཰%1 wCJU%1 wܻ%1 wظ଴஋%1 w۠ؒ%1 w໦%1 πϦʔ%1

     wήʔϜ%1   w෦෼ྻ%1 wૠೖ%1 wൈڈ%1 w໭͢%1 w࿈݁%1 ϑϩϯςΟΞ๏  w5SJF%1 wશํҐ໦%1 wΠϯϥΠϯ%1 w"MJFO%1 ໰୊ࢀরઌ"U$PEFS ղ౴63-Λ֤εϥΠυʹهࡌ͍ͯ͠·͢
  11. ظ଴஋%1   &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ+4VTIJIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@K w ظ଴஋Ͱঢ়ଶΛද͢%1 ྫEQ<J>ʙͱͳΔظ଴஋ /ຕͷࡼ͕͋Γ·͢ɻࡼʹ͸ ͱ൪߸͕ৼΒΕ͍ͯ·͢ɻ ࠷ॳɺ֤

    ʹ͍ͭͯɺࡼJʹ͸  ݸͷण͕࢘ஔ͔Ε͍ͯ·͢ɻ ͢΂ͯͷण͕࢘ແ͘ͳΔ·Ͱɺଠ࿠܅͸࣍ͷૢ࡞Λ܁Γฦ͠ߦ͍·͢ɻ  ͷ໨͕౳֬཰Ͱग़ΔαΠίϩΛৼΓɺग़໨Λͱ͢Δɻ ࡼʹण͕࢘͋Δ৔߹ɺࡼͷण࢘Λݸ৯΂Δɻࡼʹण͕࢘ແ͍৔߹ɺԿ΋ߦΘͳ͍ɻ ͢΂ͯͷण͕࢘ແ͘ͳΔ·Ͱͷૢ࡞ճ਺ͷظ଴஋ΛٻΊ͍ͯͩ͘͞ɻ
 
 ೖྗ͸͢΂ͯ੔਺Ͱ͋Δɻ   1,2,…, N i (1 ≤ i ≤ N) ai (1 ≤ ai ≤ 3) 1,2,…, N i i i i 1 ≤ N ≤ 300 1 ≤ ai ≤ 3 IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT EQ<J><K><L> ͷࡼ਺  ͷࡼ਺   ͷࡼ਺ʹ͓͚Δૢ࡞ճ਺ͷظ଴஋ ai = 1 i ai = 2 j ai = 3 k w ํ਑ͱղ౴ྫ EPVCMFEGT MMJ MMK MML \ JG EQ<J><K><L> SFUVSOEQ<J><K><L> JG JKL SFUVSO EPVCMFJKL EPVCMF J K LEPVCMFSFTOJKL JG L SFT EGT J K  L   EPVCMF LJKL JG K SFT EGT J  K L   EPVCMF KJKL JG J SFT EGT J K L   EPVCMF JJKL SFUVSOEQ<J><K><L>SFT ^ w શࡼ͸౳֬཰Ͱબ͹ΕΔͷͰɺ ͷॱ൪
 ͸Өڹ͠ͳ͍ w ঢ়ଶͱͯ͠ࡼ਺͝ͱͷૢ࡞ճ਺ͷظ଴஋
 Λߟ͑Δ ai
  12. ۠ؒ%1   /ඖͷεϥΠϜ͕ԣҰྻʹฒΜͰ͍·͢ɻ࠷ॳɺࠨ͔ΒJ൪໨ͷεϥΠϜͷେ͖͞͸B@JͰ͢ɻ ଠ࿠܅͸ɺ͢΂ͯͷεϥΠϜΛ߹ମͤͯ͞ඖͷεϥΠϜʹ͠Α͏ͱ͍ͯ͠·͢ɻ εϥΠϜ͕ඖʹͳΔ·Ͱɺଠ࿠܅͸࣍ͷૢ࡞Λ܁Γฦ͠ߦ͍·͢ɻ ࠨӈʹྡΓ߹͏ඖͷεϥΠϜΛબͼɺͦΕΒΛ߹ମͤͯ͞৽͍͠ඖͷεϥΠϜʹ͢Δɻ ߹ମલͷඖͷεϥΠϜͷେ͖͞Λ Y͓ΑͼZͱ͢Δͱɺ߹ମޙͷεϥΠϜͷେ͖͞͸Y ZͱͳΔɻ

    ͜ͷͱ͖ɺଠ࿠܅͸Y ZͷίετΛࢧ෷͏ɻ ͳ͓ɺ߹ମͷલޙͰεϥΠϜͨͪͷҐஔؔ܎͸มΘΒͳ͍ɻ ଠ࿠܅͕ࢧ෷͏ίετͷ૯࿨ͷ࠷খ஋ΛٻΊ͍ͯͩ͘͞ɻ     ೖྗ͸͢΂ͯ੔਺Ͱ͋Δɻ 2 ≤ N ≤ 400 1 ≤ ai ≤ 109 w ࠨӈͷ۠ؒͷঢ়ଶΛ؅ཧ͢Δ%1
 ྫEQ<-FGU><3JHIU>۠ؒ<-FGU 3JHIU ͷղ &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ/4MJNFTIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@O w ํ਑ͱղ౴ྫ w ٯʹʮεϥΠϜ͕෼྾͢ΔࡍʹɺεϥΠϜ ͷେ͖͚ͩ͞ίετ͕͔͔Δʯͱߟ͑Δ w ෼྾ޙͷͭεϥΠϜͷେ͖͕͞࠷খͱͳ ΔҐஔͰ෼ׂ͢Δ                   ૯࿨ ૯࿨ ૯࿨
  13. ۠ؒ%1ίʔυ   ྦྷੵ࿨ΛԿ΋ߟ͑ͣʹॻ͚ΔΑ͏ʹ͢ΔʂIUUQTRJJUBDPNESLFOJUFNTBCFEFGGD IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT EQ<M><S>۠ؒ<M S ʹ͓͚Δίετ૯࿨ͷ࠷খ஋ C<J>۠ؒ<J>ͷྦྷੵ࿨ 

     C<>B<> GPS JOUJJOJ  C<J>C<J> B<J>  ྦྷੵ࿨͔Β۠ؒ࿨औಘ MMHFU MMM MMS \ MMSFTC<S> JG M SFTC<M> SFUVSOSFT ^ MMEGT MMM MMS \ JG MS SFUVSO JG WJTJUFE<M><S> SFUVSOEQ<M><S> WJTJUFE<M><S>USVF MMSFT*/' ෼ׂΛ͢΂ͯࢼ͠ ࠷΋ίετ͕খ͍͞ҐஔͰ෼ׂ GPS JOUJMJSJ SFTNJO SFT EGT M J EGT J  S HFU M S   SFUVSOEQ<M><S>SFT ^
  14. ໦%1 πϦʔ%1    ݸͷౡ͕͋Γ·͢ɻౡʹ͸ ͔Β ·Ͱͷ൪߸͕͍͍ͭͯ·͢ɻ·ͨɺ ݸͷڮ͕͋Γ·͢ɻ ൪໨ͷڮ͸

    ൪ͷౡͱ ൪ͷౡΛͭͳ͍Ͱ͍·͢ɻ Ͳͷౡ͔ΒͲͷౡ΁΋ڮΛ͍͔ͭ͘ܦ༝ͯ͠౸ୡͰ͖Δ͜ͱ͕Θ͔͍ͬͯ·͢ɻ ͢͵͚܅͸ɺͦΕͧΕͷౡΛന·ͨ͸ࠇʹృΔ͜ͱʹ͠·ͨ͠ɻ ͨͩ͠ɺ྆୺ͷౡ͕ࠇͰృΒΕ͍ͯΔΑ͏ͳڮ͕͋ͬͯ͸͍͚·ͤΜɻ ৭ͷృΓํ͕Կ௨Γ͋Δ͔ɺ Ͱׂͬͨ༨ΓΛٻΊ͍ͯͩ͘͞ɻ     Ͳͷౡ͔ΒͲͷౡ΁΋ڮΛ͍͔ͭ͘ܦ༝ͯ͠౸ୡͰ͖Δ N 1 N N − 1 i ai bi 109 + 7 1 ≤ N ≤ 105 1 ≤ ai , bi ≤ N w ໦ߏ଄্Ͱܭࢉ͢Δ%1 "U$PEFS#FHJOOFS$POUFTU%ృΓֆIUUQTBUDPEFSKQDPOUFTUTBCDUBTLTBCD@E TUBUJDJOUEQ<><> GVODUJPOWPJE JOU JOU EGT<> JOUV JOUQSF \ EQ<V><>EQ<V><> GPS JOUW(<V> \(<V><W>௖఺Vͷࢠ௖఺W JG WQSF DPOUJOVF༿ͳΒDPOUJOVF EGT W V ࢠ௖఺WͷEQ<W>< >Λߋ৽ EQ<V><> EQ<V><>  EQ<W><> EQ<W><> .0%ന ࠇ EQ<V><> EQ<V><> EQ<W><> .0%നͷΈ ^ SFUVSO ^ EGT    DPVU EQ<><> EQ<><> .0%FOEM IUUQTBUDPEFSKQDPOUFTUTBCDTVCNJTTJPOT EQ<V><>௖఺VΛ਌ͱ͢Δ෦෼໦ʹ
 ؚ·ΕΔશ௖఺Λന·ͨ͸ࠇͰృΔ௨Γ਺ EQ<V><>௖఺V͕നͷ৔߹ʹ͓͚Δ্ه w ํ਑ͱղ౴ྫ w ෦෼໦ͷ௖఺Jʹ͓͚Δ௨Γ਺Λ਺͑Δ ઴ԽࣜΛཱͯͯʮUSFF%1໰୊ʯΛղ͘% ృΓֆ w Լهࢀর
  15. ήʔϜ%1    ݸͷਖ਼੔਺͔ΒͳΔू߹ ͕͋Γ·͢ɻ
 ଠ࿠܅ͱ࣍࿠܅͕࣍ͷήʔϜͰউෛ͠·͢ɻ ࠷ॳʹɺ ݸͷੴ͔ΒͳΔࢁΛ༻ҙ͠·͢ɻೋਓ͸࣍ͷૢ࡞Λަޓʹߦ͍·͢ɻ
 ઌख͸ଠ࿠܅Ͱ͢ɻ

    ͷݩΛͻͱͭબͼɺࢁ͔Βͪΐ͏ͲݸͷੴΛऔΓڈΔɻ ઌʹૢ࡞Λߦ͑ͳ͘ͳͬͨਓ͕ෛ͚Ͱ͢ɻ ೋਓ͕࠷దʹߦಈ͢ΔͱԾఆͨ͠ͱ͖ɺͲͪΒ͕উ͔ͭΛ൑ఆ͍ͯͩ͘͠͞ɻ ೖྗ͸͢΂ͯ੔਺Ͱ͋Δɻ     N A = a1 , a2 , …, aN K A x x 1 ≤ N ≤ 102 1 ≤ K ≤ 105 1 ≤ a1 < a2 < ⋯ < aN ≤ K &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ,4UPOFTIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@L IUUQTBUDPEFSKQDPOUFTUTEQTVCNJTTJPOT EQ<J>࢒Γͷੴ͕Jݸʹ͓͚Δ ଠ࿠ͷউͪঢ়ଶϑϥά w ํ਑ͱղ౴ྫ w ,ͷ࣌ʜෛ͚ঢ়ଶ ଠ࿠ͷෛ͚  w ,Jͷ࣌ʜ,dJͰෛ͚ঢ়ଶ͕
 ͋ΔͳΒ͹ ෛ͚Λ૬खʹԡ͠෇͚Δ w ࠨӈͷ۠ؒͷঢ়ଶΛ؅ཧ͢Δ%1
 ྫEQ<-FGU><3JHIU>۠ؒ<-FGU 3JHIU ͷղ GPS JJL J \GPS KKOK \ B<K>ݸͷੴΛऔΕΔͳΒ औΔ EQ<J>USVF͕ͭͰ΋͋Ε͹ USVF JG JB<K> \ EQ<J>cEQ<JB<K>> ^ ^^
  16. ෦෼ྻ%1   ௕͞ ͷจࣈྻ ͕༩͑ΒΕΔɻ  ͷ෦෼จࣈྻ ۭจࣈؚΉ ͱͯ͠ߟ͑ΒΕΔ΋ͷͷݸ਺Λ਺্͑͛Αɻ

    ͨͩ͠౴͕͑ͱͯ΋େ͖͘ͳΔ͜ͱ͕͋ΔͷͰɺݸ਺Λ   Ͱׂͬͨ͋·ΓΛٻΊΑɻ    ͷ֤จࣈ͸খจࣈΞϧϑΝϕοτͷΈ छྨ n S S 1 ≤ n ≤ 105 S 2JJUB෦෼จࣈྻͷ਺্͑͛෦෼ྻ%1จࣈྻͷ෦෼จࣈྻΛॏෳͳ͘૸ࠪ͢Δ%1ͷಛू ղ౴ͳ͠ EQ<J>࢒Γͷੴ͕Jݸʹ͓͚Δ ଠ࿠ͷউͪঢ়ଶϑϥά w ํ਑ͱղ౴ྫ w ෦෼จࣈྻʹؔͯ͠ܭࢉ͢Δ%1
 ྫ͋Δจࣈྻʹ͓͍ͯ෦෼จࣈྻͷଘࡏ਺Λ਺্͑͛ WFDUPSWFDUPSJOUOFYUDBMD/FYU 4  EQ<>ۭจࣈ΋ؚΉ GPS JOUJJO J \ GPS JOUKK K \ JG OFYU<J><K>O DPOUJOVF EQ<OFYU<J><K> >ʹEQ<J>ΛՃࢉ BEE EQ<OFYU<J><K> > EQ<J>  ^ ^ MPOHMPOHBOT GPS JOUJJO J BEE BOT EQ<J>  DPVUBOTFOEM OFYU<J><D>4ͷJจࣈ໨Ҏ߱Ͱ ࠷ॳʹD͕ొ৔͢ΔJOEFY WFDUPSWFDUPSJOUDBMD/FYU DPOTUTUSJOH4 \ JOUO JOU 4TJ[F  WFDUPSWFDUPSJOUOFYU O  WFDUPSJOU  O  GPS JOUJOJJ \ GPS JOUKK K OFYU<J><K>OFYU<J ><K> OFYU<J><4<J>B>J ^ SFUVSOOFYU ^ EQ<J>จࣈྻ4ͷ͏͔ͪΒJ൪໨ͷ෦෼ ΑΓಘΒΕΔ෦෼จࣈྻͷݸ਺ ͨͩ͠ ಉ͡෦෼จࣈྻΛੜ੒͢ΔͳΒબ୒JOEFY͕ࣙॻॱ࠷খʹͳΔΑ͏ʹ
  17. ૠೖ%1   ҎԼͷ৚݅Λຬͨ͢จࣈྻͷݸ਺ΛNPE   ͰٻΊΑɻ BΛ ݸɺCΛ ݸɺ[Λ

    ݸؚΉ ଞͷจࣈ͸ؚ·ͳ͍ ɻ ಉ͡จࣈ͕ྡΓ߹͏͜ͱ͸ͳ͍ɻ    ͷ͏ͪ গͳ͘ͱ΋Ұͭ͸ΑΓେ͖͍ͱ͢Δɻ freq1 freq2 freq26 0 ≤ freqi ≤ 10 freqi w ॱྻ΍จࣈྻʹૠೖ͢Δ͜ͱͰભҠ͢Δ%1 5ZQJDBM%1$POUFTU0จࣈྻIUUQTUEQDDPOUFTUBUDPEFSKQUBTLTUEQD@TUSJOH EQ<J><K>Jจࣈ໨·Ͱʹ͓͍ͯ  ࿈ଓ͍ͯ͠ΔՕॴ OHͳՕॴ ͕ Kݸ͋ΔΑ͏ͳจࣈྻͷݸ਺ w ํ਑ͱղ౴ྫ w จࣈͷؒΛݟͯ྆୺͕ಉจࣈ ͳΒOH ҧ͏จࣈͳΒPL w GSFR<J>ͷจࣈΛ෼ׂͯ͠  PLOHͳՕॴʹೖΕ͍ͯ͘ "MHPPHMF͍͢όΧ೔ࢽ EFpOFSFQJ J B C GPS JOUJ JOU B J JOU C J  EFpOFSFQ J O SFQJ J  O  JOUTPMWF \ DDPNCJOBUJPO ৔߹ͷ਺ ύεΧϧͷࡾ֯ܗͳͲͰٻΊΔ G<J>GSFR<J> TVN<J>J൪໨·Ͱͷ૯จࣈ਺ SFQ J O TVN<J >TVN<J> G<J> EQ<><> SFQ J O SFQ K TVN<J>  SFQJ L  G<J>  SFQ M NJO K L   BEE EQ<J ><KM G<J>L>  NVM EQ<J><K> NVM D<G<J>><L> NVM D<K><M> D<TVN<J> K><LM>  SFUVSOEQ<O><> ^ ෼ׂ਺L ݱࡏͷPLͳՕॴͷ਺TVN<J> K ࣍ͷOHͳՕॴͷ਺KM GSFR<J>L dp[i + 1][j − l + freq[i] − k] + =
  18. ൈڈ%1   w ૠೖ%1ͱ͸ٯʹൈڈ͢Δ%1 "3$&4PSUFEBOE4PSUFEIUUQTCFUBBUDPEFSKQDPOUFTUTBSDUBTLTBSD@D ͦΕͧΕ ͔Β ͷ੔਺͕ ͭͣͭॻ͔Εͨന͍Ϙʔϧͱࠇ͍Ϙʔϧ͕߹Θͤͯ

    ݸҰྻʹฒ ΜͰ͍·͢ɻ ࠨ͔Β ݸ໨ͷϘʔϧʹॻ͍ͯ͋Δ਺͸ Ͱɺ৭͸ Ͱද͞Ε·͢ɻ  ͷͱ͖Ϙʔϧ͕ന͍͜ͱΛɺ ͷͱ͖Ϙʔϧ͕ࠇ͍͜ͱΛද͠·͢ɻ ਓؒͷߴڮ܅͸࣍ͷΑ͏ͳ໨ඪΛୡ੒͍ͨ͠Ͱ͢ɻ  Λຬͨ͢೚ҙͷ੔਺ͷ૊ ʹରͯ͠ɺ͕ॻ͔Εͨന͍Ϙʔϧͷํ͕͕ॻ͔Εͨ ന͍ϘʔϧΑΓࠨʹ͋Δ  Λຬͨ͢೚ҙͷ੔਺ͷ૊ ʹରͯ͠ɺ͕ॻ͔Εͨࠇ͍Ϙʔϧͷํ͕͕ॻ͔Εͨ ࠇ͍ϘʔϧΑΓࠨʹ͋Δ ໨ඪΛୡ੒͢ΔͨΊʹߴڮ܅͸࣍ͷΑ͏ͳૢ࡞͕Ͱ͖·͢ɻ ྡΓ߹͏ೋͭͷϘʔϧΛεϫοϓ͢Δ໨ඪΛୡ੒͢ΔͨΊʹඞཁͳૢ࡞ճ਺ͷ࠷খ஋ΛٻΊͯͩ͘ ͍͞ɻ      ·ͨ͸   ͳΒ 1 N 1 2N i (1 ≤ i ≤ 2N) ai ci ci = W ci = B 1 ≤ i < j ≤ N (i, j) i j 1 ≤ i < j ≤ N (i, j) i j 1 ≤ N ≤ 2000 1 ≤ ai ≤ N ci = W ci = B i ≠ j (ai , ci ) ≠ (aj , cj )
  19. ൈڈ%1ίʔυ   "3$&4PSUFEBOE4PSUFEIUUQTCFUBBUDPEFSKQDPOUFTUTBSDUBTLTBSD@D EQ<C><X>ࠇͷ਺C നͷ਺X·Ͱઌ಄ʹ࣋ͬͯ དྷ͍ͯΔ৔߹ͷૢ࡞ճ਺ͷ࠷খ஋ w ํ਑ͱղ౴ྫ w

    നͱࠇʹ͍͔ͭͯΒॱ൪ʹ ઌ಄ʹऔͬͯ͘Δૢ࡞Λߦͬ ͍ͯ͘ w ॏཁߟ࡯ʮനͷX·Ͱɺࠇͷ C·Ͱઌ಄ʹऔ͖ͬͯͨ৔߹ ʹ࢒ΔϘʔϧͷྻ͸Ұҙʹఆ ·Δʯ
 ͭ·ΓɺനͱࠇͷͲͷ਺·Ͱ ઌ಄ʹऔ͖͔ͬͯͨͰঢ়ଶΛ వΊΔ͜ͱ͕Ͱ͖ͦ͏ w ͦΕΛLFZͱͨ͠%1ͩͱ 0 /? Ͱɺߋ৽΋0  Ͱߦ ͚ͦ͏ IUUQTBUDPEFSKQDPOUFTUTBSDTVCNJTTJPOT JOUG JOUC JOUX \ JG C/BOEX/ SFUVSO JG EQ<C><X> SFUVSOEQ<C><X> JOUSFTJOG JG C/ \ JOUDOU JG JEY#<C > DOU TN#<C ><JEY#<C >> JG JEY#<C > DOU TN8<X ><JEY#<C >> DINJO SFT G C  X  DOU  ^ JG X/ \ JOUDOU JG JEY8<X > DOU TN#<C ><JEY8<X >> JG JEY8<X > DOU TN8<X ><JEY8<X >> DINJO SFT G C X   DOU  ^ SFUVSOEQ<C><X>SFT ^ ͸·΍Μ͸·΍Μ͸·΍Μ w Լهࢀর
  20. ໭͢%1   w EQ<J >͔ΒEQ<J>Λ෮ݩ͢Δ͜ͱͰ໰୊Λղ͘%1 "3$%஫จͷଟ͍ߴڮ঎ళIUUQTBSDDPOUFTUBUDPEFSKQUBTLTBSD@ ߴڮ঎ళͰ͸ छྨͷ঎඼͕ചΒΕ͍ͯ·͢ɻʮͲͷछྨͷ঎඼͕͍ͭ͋͘Δ͔ʯͷ৘ใ͕༩͑ ΒΕΔͷͰɺʮ߹ܭ

    ݸͷ঎඼ΛબͿํ๏ʯͷ਺ΛٻΊͯԼ͍͞ɻͨͩ͠ɺಉ͡छྨͷ঎඼͸۠ ผ͠ͳ͍͜ͱͱ͠·͢ɻ ͍΍ɺ͜Ε͸গ͠؆୯ա͗ΔͷͰɺͪΐͬͱͨ͠஫จ΋௥Ճ͠Α͏ɻ੔਺ ͔ΒͳΔ ݸͷ஫ จΛ༻ҙͨ͠ͷͰɺͦΕͧΕʹ͍ͭͯʮ छྨ໨ͷ঎඼Λͪΐ͏Ͳ ݸબ͹ͳ͚Ε͹ͳΒͳ͍ͱ ͖ɺ߹ܭ ݸͷ঎඼ΛબͿํ๏ʯͷ਺ΛٻΊͯԼ͍͞ɻ            N M k, x Q ki xi M 1 ≦ N ≦ 2000 1 ≦ M ≦ 2000 1 ≦ Q ≦ 500,000 1 ≦ ai ≦ M 1 ≦ ki ≦ N 1 ≦ xi ≦ aki EQ<J><K>J൪໨·ͰͰKݸͷ঎඼ΛબͿ௨Γ਺ EQ<J><K>/छྨͷબͼํ͔Βͭר͖໭͢༻ͷ഑ྻ w ํ਑ͱղ౴ྫ w ॏෳ૊߹ͤΛٻΊΔ w ঎඼ͷݟ͍ͯ͘ॱ൪͸໰Θͳ͍ ఺ʹண໨ͯ͠ ࣜมܗ w มܗʹΑΓʮ/छྨͷ঎඼Λબ Ϳબͼํ͔Βͭר͖໭͢ʯ͜ ͱ͕Ͱ͖Δ LNKQTCMPHπόαͷඋ๨࿥ DUZMTQSPCMFNTPMWJOHNBZPLP`TEJBSZ w Լهࢀর
  21. ໭͢%1ίʔυ   "3$%஫จͷଟ͍ߴڮ঎ళIUUQTBSDDPOUFTUBUDPEFSKQUBTLTBSD@ '03 J /  EQ<J><> ͜͜͸௨ৗ௨ΓEQΛຒΊΔ

    '03 J / '03 K . \ EQ<J ><K >EQ<J ><K> EQ<J><K > JG K"<J> EQ<J ><K > .0%EQ<J><K"<J>> EQ<J ><K >.0% ^ લ͔ΒஞҰܭࢉ͢ΔͷΛ΍Ίͯ ໭͢%1Ͱܭࢉ͢Δ '03 J / '03 K .  \ EQ<J><K>EQ</><K> JG K EQ<J><K> .0%EQ</><K> JG K"<J> EQ<J><K> EQ<J><K"<J>> EQ<J><K>.0% ^ EQ<J><K>J൪໨·ͰͰKݸͷ঎඼ΛબͿ௨Γ਺ EQ<J><K>/छྨͷબͼํ͔Βͭר͖໭͢༻ͷ഑ྻ
  22. ࿈݁%1   w ࿈݁ؔ܎ʹجͮ͘%1
 ྫEQ<J><K>Jྻ໨·ͰΛ࢖ͬͨ࣌ʹ Jྻ໨ͷ Ϛεಉ࢜ͷ࿈݁ؔ܎͕KͰ͋ΔΑ͏ͳృΓํ  w

    6OJPOpOEͰ࿈݁͢Δ 5ZQJDBM%1$POUFTU4Ϛε໨IUUQTUEQDDPOUFTUBUDPEFSKQUBTLTUEQD@HSJE ͢͵͚܅͸ɺ ͷϚε໨ΛനͱࠇʹృΓ෼͚Δ͜ͱʹͨ͠ɻ ࠨ্ͷϚε໨ͱӈԼͷϚε໨͸ࠇʹృΔɻ ࠨ্ͷϚε໨͔Βɺࠇ͍ϚεΛ্ԼࠨӈʹͨͲͬͯɺӈԼͷϚε໨ʹߦ͘͜ͱ͕Ͱ͖Δɻ ৚݅Λຬͨ͢ృΓ෼͚ํͷݸ਺ΛNPE   ͰٻΊΑɻ    H × W 2 ≤ H ≤ 6 2 ≤ W ≤ 100 EQ<J><K>Jྻ໨·Ͱݟͨ࣌ɼJྻ໨ͷ֤ Ϛεͷ࿈݁ؔ܎͕KͰ͋ΔΑ͏ͳృΓํ w ํ਑ͱղ౴ྫ w ͦΕͧΕͷϚεͷ࿈݁ؔ܎Λ΋ͪͳ͕ Β ྻΛࠨ͔Βӈʹݟ͍ͯ͘%1Λߟ͑ Δ w ਐ਺CJU  ͰҎԼͷΑ͏ʹߟ͑Δ
 ന
 ࠇ͔ͭ   ͱ࿈݁
 dࠇ͔ͭ   ͱ࿈݁Ͱͳ͍ɽ ಉ͡൪߸͕ৼΒΕ͍ͯΕ͹ɼͦΕΒͷ Ϛε͸࿈݁Ͱ͋Δͱߟ͑Δɽ = 7H ͍͢όΧ೔ࢽLNKQTCMPHTJNF[J@UBOͷ೔ه GPS JOUJJX J GPS JOUKKQX<I> K \࣍ঢ়ଶ JG DVS<K> DPOUJOVF GPS JOULL I  L \ VOJPO@pOEVG I WFDUPSJOUQ I VG࿈݁ؔ܎ΛٻΊΔ GPS JOUMMI M \Q࣍ঢ়ଶͷ֤ߦͷ࿈݁ؔ܎Λอଘ JG LM \࣍ঢ়ଶͷMߦ໨͕ࠇ JG KQX<M> Q<M>ݱঢ়ଶ͕࿈݁ͳΒ࣍΋࿈݁ GPS JOUNM NI N \࿈݁ؔ܎ΛVGͰ·ͱΊΔ JG LN \ JG NM  VGVOJUF M N MͱM ߦ໨͕ࠇͳΒ࿈݁ FMTF\ JOUVKQX<M> ݱঢ়ଶͷMߦ໨ Nߦ໨ͷ࿈݁ؔ܎ WKQX<N> JG VVW VGVOJUF M N ݱঢ়ଶͰಉ͡࿈݁ؔ܎ ^ʜͳΒ࣍ঢ়ଶͰ΋࿈݁
  23. 5SJF%1   w 5SJF໦ͷ্Ͱܭࢉ͢Δ%1 "U$PEFSఱԼҰϓϩάϥϚʔίϯςετຊઓ$ͨΜͨ͘͝͞Μ IUUQTUFOLBpOBMPQFODPOUFTUBUDPEFSKQUBTLTUFOLB@@pOBM@D จࣈྻ4ͱɺཁૉ਺.ͷ୯ޠͷू߹1\1@ 1@ ʜ

    1@.^͕༩͑ΒΕ·͢ɻ୯ޠ1@J͸ɺ੔਺ ͷॏΈ8@JΛ͍࣋ͬͯ·͢ɻ จࣈྻ4͔Βɺ1ʹؚ·ΕΔ୯ޠΛॏͳΓ߹Θͳ͍Α͏ʹऔΓग़͢͜ͱΛߟ͑·͢ɻ୯ޠͷॏΈͷ ૯࿨͕࠷େ஋ΛͱΔΑ͏ʹऔΓग़͢ͱ͖ɺͦͷ࠷େ஋͸͍ͭ͘Ͱ͠ΐ͏͔ʁ ͳ͓ɺಉ͡୯ޠΛෳ਺ճऔΓग़ͨ͠৔߹ɺͦΕΒͷ୯ޠ͸ผʑʹ਺͑Δ͜ͱͱ͠·͢ɻ ≤c4c≤ ≤.≤ ≤c1Jc≤ ≤8J≤ 4 1J͸ӳখจࣈ͔ΒͳΔจࣈྻ w ํ਑ͱղ౴ྫ w ༩͑ΒΕΔ୯ޠ͢΂ͯͰ5SJF໦Λͭ͘ Δ w จࣈྻ4ͷJจࣈ໨T@JΛࠨ୺ͱ͢Δ4ͷ ෦෼จࣈྻͰ ୯ޠͱ׬શҰக͢Δ΋ ͷΛߟ͑Δ w ୯ޠͷ௕͕͞ߴʑͳͷͰ ෦෼จ ࣈྻͷબͼํ͸௨Γ͔͠ͳ͍ IPHFDPEFS w Լهࢀর EQ<J>Jจࣈ·Ͱݟͨ࣌ͷ࠷େ஋ 3&1 J / \ 5SJF DVSUSJF SFQR L   \ JG J L/ DPOUJOVF DIBSUBSHFUT<J L> 5SJF OFYU JG DVS 5SJF  \ OFYUUSJFpOE UBSHFU DVS DVSOFYU DINBY EQ<J L>  DVS 5SJF   EQ<J> DVSTDPSFEQ<J>  ^FMTFDINBY EQ<J L> EQ<J>  ^^
  24. શํҐ໦%1   w ೚ҙͷ௖఺Λࠜͱͨ͠໦ʹ͓͚Δ%1 "J[V0OMJOF+VEHF%JBNFUFSPGB5SFFIUUQKVEHFVBJ[VBDKQPOMJOFKVEHFEFTDSJQUJPOKTQ JE(3-@@"MBOHKQ ඇෛͷॏΈΛ΋ͭແ޲ͷ໦Tͷ௚ܘΛٻΊ͍ͯͩ͘͞ɻ໦ͷ࠷ԕ௖఺ؒͷڑ཭Λ໦ͷ௚ܘͱ͍͍·͢ɻ  

     1 ≤ n ≤ 100,000 0 ≤ wi ≤ 1,000 FJͷ೔ه w Լهࢀর w ํ਑ͱղ౴ྫ w ద౰ͳ௖఺Λࠜͱͯ͠ ໦Λ༗ ޲໦ͱΈͳ͢ w ೚ҙͷ௖఺Λࠜͱͨ͠෦෼໦ʹ ͍ͭͯ ෦෼໦ͷࠜʹlඞཁͳ ৘ใ%@JΛٻΊΔ
 %@J*൪໨ͷ௖఺͔Βͦͷ௖఺ͷ෦෼ ໦಺Ͱ࠷΋ԕ͍௖఺ ༿ ·Ͱͷڑ཭ w ೚ҙͷ௖఺Λࠜͱͨ͠໦શମʹ ͍ͭͯ ໰୊ͷղΛٻΊΔ WPJEEGT JOUJEY JOUQBS \GPS FEHFFH<JEY> \ JG FUPQBS DPOUJOVF EGT FUP JEY  EJTU<JEY>NBY EJTU<JEY> EJTU<FUP> FDPTU  ^^ JOUEGT JOUJEY JOUE@QBS JOUQBS \ WFDUPSQBJSJOU JOUE@DIJME E@DIJMEFNQMBDF@CBDL    GPS FEHFFH<JEY> \ JG FUPQBS E@DIJMEFNQMBDF@CBDL E@QBS FDPTU FUP  FMTFE@DIJMEFNQMBDF@CBDL FDPTU EJTU<FUP> FUP  ^ TPSU E@DIJMESCFHJO E@DIJMESFOE  JOUSFUE@DIJME<>pSTU E@DIJME<>pSTU GPS FEHFFH<JEY> \ JG FUPQBS DPOUJOVF SFUNBY SFU EGT FUP E@DIJME<E@DIJME<>TFDPOEFUP>pSTU JEY  ^ SFUVSO SFU  ^ EGT /   DPVUEGT /   FOEM
  25. ΠϯϥΠϯ%1   w ߋ৽Օॴ͕গͳ͍৔߹ʹ 
 ଞͷྖҬΛ࢖͍·Θ͢͜ͱͰߴ଎Խ͢Δ%1 w 4FHUSFF΍#*5Λ༻͍ͯߦ͏ &EVDBUJPOBM%1$POUFTU%1·ͱΊίϯςετ2'MPXFSTIUUQTBUDPEFSKQDPOUFTUTEQUBTLTEQ@R

     ຊͷՖ͕ԣҰྻʹฒΜͰ͍·͢ɻ֤ ʹ͍ͭͯɺࠨ͔ΒJ൪໨ͷՖͷߴ͞͸ Ͱɺඒ͠͞͸  Ͱ͢ɻ ͨͩ͠ɺ ͸͢΂ͯ૬ҟͳΓ·͢ɻ ଠ࿠܅͸Կຊ͔ͷՖΛൈ͖ڈΔ͜ͱͰɺ࣍ͷ৚͕݅੒ΓཱͭΑ͏ʹ͠Α͏ͱ͍ͯ͠·͢ɻ ࢒ΓͷՖΛࠨ͔ΒॱʹݟΔͱɺߴ͕͞୯ௐ૿Ճʹͳ͍ͬͯΔɻ ࢒ΓͷՖͷඒ͠͞ͷ૯࿨ͷ࠷େ஋ΛٻΊ͍ͯͩ͘͞ɻ ೖྗ͸͢΂ͯ੔਺Ͱ͋Δɻ     ͸͢΂ͯ૬ҟͳΔɻ N i (1 ≤ i ≤ N) hi ai h1 , h2 , …, hN 1 ≤ N ≤ 2 × 105 1 ≤ hi ≤ N h1 , h2 , …, hN 1 ≤ ai ≤ 109 w ํ਑ͱղ౴ྫ w EQ<J>NBY EQ<J> NBY EQ<>  EQ<>  EQ<)<J>>  "<J>ͱ͍͏ ͷΛ/ճߦ͏ w EQ<I>Ͱ۠ؒNBYͷηά໦Λ࡞Δ EQ<I>ӈ୺ͷՖͷߴ͕͞IʹͳΔΑ͏ͳ ฒ΂ํͷதʹ͓͚Δඒ͠͞ͷ૯࿨ͷ࠷େ஋ SFQ J  / \ MMPQUEQHFU  )<J>  "<J> EQVQEBUF )<J> NBY EQ<)<J>> PQU  ^ DPVUEQHFU   FOEM ͸·΍Μ͸·΍Μ͸·΍Μ w Լهࢀর EQ<J><K>NBY\EQ<J—><L>c≤LK^ B<J>JGI<J>K PUIFSXJTFEQ<J—><K> ͜ͷ࣌ EQ<J>ͷߋ৽͸ՕॴͷΈ JJͷભҠ͕ݻఆ ͳͷͰ JΛঢ়ଶʹ΋ͭඞཁ͕ͳ͍ఴࣈΛলུͰ͖Δߴ଎Խ
  26. "MJFO%1   w ಛఆ৚݅ԼͰ0 /? Λ0 /?MPH/ ʹམͱ͢%1 $4"DBEFNZ0S1SPCMFNIUUQTDTBDBEFNZDPNDPOUFTUSPVOEUBTLPSQSPCMFN

    :PVBSFHJWFOBOBSSBZ"PG/JOUFHFST 5IFDPTUPGBTVCBSSBZJTEFpOFEBTUIFCJUXJTFPSWBMVFPGBMMJUTFMFNFOUT 4VQQPTFZPVTQMJUUIFBSSBZJOUP,,OPOFNQUZTVCBSSBZTBOEZPVDPNQVUFUIFTVNPG UIFJSDPTUT 8IBUJTUIFNBYJNVNWBMVFZPVDBOHFU  
 1≤K≤N≤2∗10^5 , 0 \leq A_i < 2^{20} IUUQTDTBDBEFNZDPNTVCNJTTJPO ʮG K EQ<J><K>ʯͱͨ͠ͱ͖ʹʮG K  G K 㱡G K G K ʯΛຬ͍ͨͯ͠Ε͹ɺܭࢉྔΛམͱ͢ ͜ͱ͕Ͱ͖Δ  lԿ͔Λ,ճ࢖ͬͯίετΛ࠷খԽ͍ͯͩ͘͠͞ɼͰɼEQ<QPT><࢖ͬͨճ਺>ͩͱ஗͗͢Δͱ͖ʹɼ ,ճ࢖͏୅ΘΓʹճ࢖͏ͱYԁേۚɼʹ͢Ε͹EQ<QPT>ʹग़དྷΔ ͜ͱ΋͋Δ ςΫ YͰೋ෼୳ࡧ z CZ!ZPTVQPU ͸·΍Μ͸·΍Μ͸·΍Μɹ$PEFGPSDFTղઆ w Լهࢀর w ίʔυ