SlideShare a Scribd company logo
イベント問題解説
本の整理
アルゴリズムの改善
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

Recently uploaded

Sales Hub_Kuzen_Our Service Introduction
Sales Hub_Kuzen_Our Service IntroductionSales Hub_Kuzen_Our Service Introduction
Sales Hub_Kuzen_Our Service Introduction
nomofuruse1
 
Business Strategy - ITIL Case Studies by Using ITIL 2011 (Japanese)
Business Strategy -  ITIL Case Studies by Using ITIL 2011 (Japanese)Business Strategy -  ITIL Case Studies by Using ITIL 2011 (Japanese)
Business Strategy - ITIL Case Studies by Using ITIL 2011 (Japanese)
Jerimi Soma
 
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
chiakiota2
 
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
Jun Chiba
 
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
ryooya0127
 
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
otakai1201
 
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
ssuser8de8212
 
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
Management Soluions co.,ltd.
 
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
chiakiota2
 
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
recruit9
 
smartcamp_RECRUIT_deck 2024060606 dddddd
smartcamp_RECRUIT_deck 2024060606 ddddddsmartcamp_RECRUIT_deck 2024060606 dddddd
smartcamp_RECRUIT_deck 2024060606 dddddd
design777859
 
20240606_Service Design Exercise_Visual Report_vol.1.pdf
20240606_Service Design Exercise_Visual Report_vol.1.pdf20240606_Service Design Exercise_Visual Report_vol.1.pdf
20240606_Service Design Exercise_Visual Report_vol.1.pdf
jun_suto
 
orderbusters_servicedocument_batton_saleshub.pdf
orderbusters_servicedocument_batton_saleshub.pdforderbusters_servicedocument_batton_saleshub.pdf
orderbusters_servicedocument_batton_saleshub.pdf
sano54
 
うりぼーギルドの歩き方
うりぼーギルドの歩き方うりぼーギルドの歩き方
うりぼーギルドの歩き方
Kota
 
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
Eiichi Mano
 

Recently uploaded (15)

Sales Hub_Kuzen_Our Service Introduction
Sales Hub_Kuzen_Our Service IntroductionSales Hub_Kuzen_Our Service Introduction
Sales Hub_Kuzen_Our Service Introduction
 
Business Strategy - ITIL Case Studies by Using ITIL 2011 (Japanese)
Business Strategy -  ITIL Case Studies by Using ITIL 2011 (Japanese)Business Strategy -  ITIL Case Studies by Using ITIL 2011 (Japanese)
Business Strategy - ITIL Case Studies by Using ITIL 2011 (Japanese)
 
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
【slideshare】オンライン二次健診サービス_20240607.pptx.pdf
 
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
クイズでレクリエーション!企業向けクイズワークショップ「みんなのクイズ」の説明資料
 
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
ドコモgaccoサービス紹介資料(リベラルアーツ思考ビジネスプログラム、AIプロンプト思考ビジネスプログラム、DX人材育成プログラム)
 
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
画像生成AIのビジネス活用術をご紹介! 5つの活用シーンと具体的事例も併せて紹介します
 
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
【簡易版】DMMチャットブーストCV サービス該当の紹介資料.pptx.pdf
 
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
株式会社マネジメントソリューションズ(MSOL エムソル) サスティナビリティレポート
 
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
【slideshare】オンライン二次健診サービス_20240607.pptx (1).pdf
 
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
株式会社VISIONARY JAPAN_エンジニアチーム 採用ピッチ資料(ver2.2).pdf
 
smartcamp_RECRUIT_deck 2024060606 dddddd
smartcamp_RECRUIT_deck 2024060606 ddddddsmartcamp_RECRUIT_deck 2024060606 dddddd
smartcamp_RECRUIT_deck 2024060606 dddddd
 
20240606_Service Design Exercise_Visual Report_vol.1.pdf
20240606_Service Design Exercise_Visual Report_vol.1.pdf20240606_Service Design Exercise_Visual Report_vol.1.pdf
20240606_Service Design Exercise_Visual Report_vol.1.pdf
 
orderbusters_servicedocument_batton_saleshub.pdf
orderbusters_servicedocument_batton_saleshub.pdforderbusters_servicedocument_batton_saleshub.pdf
orderbusters_servicedocument_batton_saleshub.pdf
 
うりぼーギルドの歩き方
うりぼーギルドの歩き方うりぼーギルドの歩き方
うりぼーギルドの歩き方
 
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
その街の今がわかる。ビッグデータから得られた時間帯別推計流動人口統計 あさひる統計プロダクト資料(v202403/2024Q1)
 

Featured

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)
contently
 
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
Albert Qian
 
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)
 
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
Search Engine Journal
 
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
SpeakerHub
 
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
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
Tessa Mero
 
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
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
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
MindGenius
 
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...
RachelPearson36
 
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...
Applitools
 
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
GetSmarter
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
Alireza Esmikhani
 
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
Project for Public Spaces & National Center for Biking and Walking
 
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...
DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
Erica Santiago
 
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 本の整理問題の解説