SlideShare a Scribd company logo
1 of 7
Download to read offline
イベント問題解説
本の整理
アルゴリズムの改善
5432 1
i = 1に立つ→赤い本(ID=1)の本まで歩く→ j = 4
i ≠ j なので入れ替える
i = 2 に立つ→オレンジの本(ID=2)の本まで歩く→ j = 4
i ≠ j なので入れ替える
541 3 2
i = 3 に立つ→黄色の本(ID=3)の本まで歩く→ j = 4
i ≠ j なので入れ替える
541 32
i = 4 に立つ→青色の本(ID=5)の本まで歩く→ j = 4
i = jなので入れ替えない
541 32
挿入ソート的な手順。計算量的には O(N^2 )
本の整理
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
min_index = A.index(min(A[i::]))
if A[i] != A[min_index]:
A[i], A[min_index] = A[min_index], A[i]
cnt += 1
print(cnt)
Pythonのコードはこんな感じ・・・計算量は O(N^2)
N=100000
100000^2だと10000000000 中々厳しい・・・
ここで考えるのは・・・・
一番小さい数字の本のところまで歩いて確認して行くのに時間がかかっています。
じゃあそれを楽にしようと考えます。 Pythonのコードで言うとmin()関数の部分
一番小さい数字というのは、並べ替えていくので、必ず 1, 2 ,3 … N と順に見ていく事になります。
それを考えると1が何番目にある、2が何番目にあるというように記録しておくと良いことに気が付きます。
5432 1 4 1 2 3 5 1が4番目にある
2が1番目にある
3が2番目にある
4が3番目にある
5が5番目にある
というように配列に記録しておく
この作業自体は N 回の操作
(1からN番目まで歩いて記録していけば )のみで済む
5432 1 4 1 2 3 5
本の i = 1 を見た時に→オレンジ本のID=2
i = 1 が ID=2では当然交換が必要。
交換対象は ID=1 の本なので位置を確認。
本は ID=1 の本の位置と ID=2 の本の位置の交換
記録は ID=1 の本の記録と ID=2 の本の記録の交換
をする
同じように i = 2 を見た時は
本は ID=2 の本の位置と ID=3 の本の位置を交換
記録は ID=2 の本の位置と ID=3 の本の位置を交換
ID=1の本の位置を確認
1 4 2 3 5
ID=2の本の位置を確認
543 21
……
このようにすると、本の位置を確認する際に順に見る必要がないので交換する位置を O(1) で確認できます。
(先の手順では最大で一番うしろまで順に見るので最悪 O(N) )
よって計算量は O(N) となります。
N = int(input())
A = list(map(lambda a: int(a)-1, input().split()))
R = [0]*N
for i in range(N):
R[A[i]] = i
ans = 0
for i in range(N):
if A[i] != i:
tmp = R[i]
R[i], R[A[i]] = R[A[i]], R[i]
A[i], A[tmp] = A[tmp], A[i]
ans += 1
print(ans)
Pythonでのコードは以下の通り
この問題の意図は…
ここまで説明するとバケット(バケツ)ソートを連想した人がいるかも…?
各要素が1つしか存在しないバケットソートを実装するのと同等となります。
バケットソートを発想することができたり
ソートアルゴリズムを自ら実装出来るかを問うています

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

3 25 本の整理問題の解説