Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
AOJ 0022 Maximum Sum Sequence 解説
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
kagamiz
March 30, 2013
Programming
1.6k
1
Share
AOJ 0022 Maximum Sum Sequence 解説
OkNCT-ICT 2013 春合宿 Day 6 (らしい) に解説したもの.
kagamiz
March 30, 2013
More Decks by kagamiz
See All by kagamiz
KCS v2. の開発
kagamiz
0
290
internship final presentation
kagamiz
0
1.3k
internship-middle term presentation
kagamiz
0
1.2k
すうがくのまほう
kagamiz
0
380
ご当地料理の紹介
kagamiz
0
490
オンラインジャッジシステムの実装
kagamiz
0
1.2k
AOJ 0557 A First Grader 解説
kagamiz
0
1k
JOI2013 本選1 Illumination 解説
kagamiz
0
400
AOJ 0186 Aizu Chicken 解説
kagamiz
0
330
Other Decks in Programming
See All in Programming
HTML-Aware ERB: The Path to Reactive Rendering @ RubyKaigi 2026, Hakodate, Japan
marcoroth
0
680
20260514 - build with ai 2026 - build LINE Bot with Gemini CLI
line_developers_tw
PRO
0
400
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
150
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
590
Making the RBS Parser Faster
soutaro
0
690
ソフトウェア設計の結合バランス #phperkaigi
kajitack
0
500
Surviving Black Friday: 329 billion requests with Falcon!
ioquatix
0
3k
ふにゃっとしない名前の付け方 〜哲学で茹で上げる、コシのあるソフトウェア設計〜
shimomura
0
120
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
490
AlarmKitで明後日起きれるアラームアプリを作る
trickart
0
130
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
200
実践ハーネスエンジニアリング:ステアリングループを実例から読み解く / Practical Harness Engineering: Understanding Steering Loops Through Real-World Examples
nrslib
5
5.1k
Featured
See All Featured
Fireside Chat
paigeccino
42
3.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.4k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Technical Leadership for Architectural Decision Making
baasie
3
360
New Earth Scene 8
popppiees
3
2.2k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Docker and Python
trallard
47
3.8k
Transcript
AOJ 0022 Maximum Sum Sequence 解説 @kagamiz
www.company.com 問題概要 • 長さがn の数列が与えられます. • 連続する1 個以上の項の和の最大値を求めてください. • n
≦ 5000 (Challenge ケースではn ≦ 10^6) • |ai| ≦ 100000
www.company.com 入力例の解析 • n = 7 数列 = {-5 -1 6 4 9 -6 -7} •
赤の部分列を選ぶと最適で, 和は19 になる. {-5 -1 6 4 9 -6 -7}
www.company.com 入力例の解析 • n = 13 数列 = {1 2 3 2 -2 -1 1 2 3 2 1 -2 1} •
赤の部分列を選ぶと最適で, 和は14 になる. {1 2 3 2 -2 -1 1 2 3 2 1 -2 1}
www.company.com 入力例の解析 • n = 3 数列 = {1000 -200 201} •
赤の部分列を選ぶと最適で, 和は1001 になる. {1000 -200 201}
www.company.com O(n^3) 解法 • すべての区間を試す. • 区間は左と右を決めれば一意に定まる ← O(n^2) 個
• 左から右までをfor 文で和を取る ← O(n)時間 • こうして調べた和の中の最大値が答え. • n ≦ 200 までならOK.
www.company.com O(n^2) 解法 • O(n^3) 解法の何が無駄だったか? • どの区間が最大になるかはよく分からないか ら、O(n^2) 個すべてを試したい.
• “左から右までをfor 文で和を取る ← O(n)時間” • これを高速にしよう
www.company.com O(n^2) 解法 • 累積和配列の利用. • S[i] = a[1] +
a[2] + … + a[i] という配列を用意. • S[1] から順番に計算するとO(n) 時間で構築できる. • 区間[l, r] の和はS[r] – S[l – 1] で求まる. • よって, 2 重ループがもっとも重くO(n^2) 時間となる. • n ≦ 5000 までならOK.
www.company.com O(n) 解法 • Challenge ケースでも満点を取るためにはどうすれば 良いか? → 動的計画法の利用. •
dp[i] : 区間[x, i]での部分列の和の最大値(a[i] は必ず含 む)とすると, dp[i] = max(dp[i – 1] + a[i], a[i]) となる. • 答えはdp[i] の最大値. • 今までの最大値を利用するか, 自分から始めるか.
www.company.com O(n) 解法 • n = 13 数列 = {1 2 3 2 -2 -1 1 2 3 2 1 -2 1}
dp[i]= {1 3 6 8 6 5 6 8 11 13 14 12 13} • これは, for 文で最初から計算すると線形時間で求まる. • これで, Challenge ケースでも満点が取れる! • お疲れ様でした.