SlideShare una empresa de Scribd logo
1 de 29
競技プログラミングのすゝめ
spin13
競技プログラミングとは?
・問題が複数問出題される
・一定時間にどれだけ多く解けるかを競う
・早いだけではダメ! 正確さも必要
問題A
問題B
問題C
問題D
競技プログラミングとは?
a, b という入力が与えられ
るので, aをbで割った余り
を出力しなさい.
問題
自分の書いたソースコード
Judge Server
Submit
Judge Serverに
用意された入力
に正しく出力を
返せれば点数に
どんなことが養われるのか
・与えられた問題に対する考察力
・アルゴリズムを考える力
・その問題を解決する実装力
例1
0と1で構成された入力が与えられるので,いくつかを0 または 1 に変換して交互
に並んでいるようにしたい.つまり0の隣は常に1で,1の隣は常に0.
いくつ変換すれば交互に並んでいるようにできるか,その最小値を返してくださ
い.
010001010 010101010
例1
特に考えずに,最初の位置を基点として1つ前の数字と次の数字が違うように並
び替えてやればいい?
001011101
010101010
答えは
7!
交互に並んでいる状態での最小
001011101
010101010
101010101
7
2
問題の考察
・0から始まって交互になるように変換
・1から始まって交互になるように変換
2パターンあるので注意が必
要
いやいや.....
2パターンしかな
い!
答え1
与えられた文字列長で0と1が交互に並んだ2パターンを用意しておい
て,2つと比べて違う部分の数を数えればいいんじゃね?
入力
000110101001
010101010101
101010101010
用意!
答え2
000110101001
010101010101
101010101010
7
5
片方と比較して出た答えともう片方と比較して出た答えを足すと文字列長Nに等し
くなる
ans = min(ans, strLeng - ans);
いろいろな解き方がある
解法は1つじゃない!
例2
整数Nが与えられるので,1~Nに含まれる素数の数を答えよ.
制約
1 ≦ N ≦ 1000000
2s以内(多くの問題はこのようにプログラムが答えを返すまでの時間に制限があ
る)
例2
普通に素数求めればええんじゃろ?
2からループ回していってその数が素数がどうか調べればい
いんじゃないの?
例2
制約
1 ≦ N ≦ 1000000
2s以内(多くの問題はこのようにプログラムが答えを返すまでの時間に制限があ
る)
→このやり方では1000000が与えられたら2s以
内に答えがでない
例2
愚直にがんばると計算量はO(N^2)くらい
100万*100万の計算量だと到底間に合わない><
→もっと高速なアルゴリズムでやる必要がある
エラトステネスの篩
・見つけた素数の倍数を撃ち落とす
エラトステネスの篩
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
エラトステネスの篩
まずは最初の素数である2より大きい2の倍数を消してい
く
この時点で2が素数であることが確定
エラトステネスの篩
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
次の素数である3より大きい3の倍数を消していく
3が素数に確定
エラトステネスの篩
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
5より大きい5の倍数を消していく
5が素数に確定
エラトステネスの篩
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
29以下の全ての素数が求まった!
素数アルゴリズム
他にも
・Atkinの篩
・ミラーラビン素数判定法
など色々....
アルゴリズムを
学び
実装する
楽しさ!
競技プログラミング(コンテスト)ができるサイト
・Topcoder(英語)
・Codeforces(英語)
・AtCoder(日本語)
・yukicoder(日本語)
等
練習
まずはとにかく過去問を解く!
前ページのサイトに加え
・Aizu Online Judge
・PKU Judge Online
等
時間の長いコンテスト
一般的にはマラソンマッチと呼ばれる
競技時間は数週間なのでのんびりできる
(基本的には)
最適解が定まらない問題をテーマになるべくスコアの高い回
答を出すプログラムを書く

Más contenido relacionado

Destacado

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
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)
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
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...
 

20160620 競技プログラミングのすゝめ