SlideShare a Scribd company logo
1 of 20
競技プログラミング講義
#2' 「続・全探索」
#2 「全探索」
の補足です
●

説明がわかりづらかったと思うので図を交
えて再度説明します
復習ー全探索とは
●

●

「再帰関数」なるものを使って「状態の遷
移」というものを考える方法のこと
例題として「ナップサック問題」というの
があった
ナップサック問題
●

重さ W[i] と価値 V[i] が決まっている N 個の品物
がある。重さの総和が S を超えないように品物
を選んだときの、それらの品物の価値の総和の最
大値を求めよ。
1≦N≦20
1≦W[i],V[i]≦1,000,000
1≦S≦1,000,000

●

重さを値段、価値を美味しさにすると予算内で最
高の食事をする問題などにあたる
状態 (0,300)

  150 円    200 円 
  100P

250P

100 円
50P
状態 (0,300)

  150 円    200 円 
  100P

250P

100 円
50P
1 本目を選んだ→状態 (1,150)

  150 円    200 円 
  100P

250P

100 円
50P
2 本目を選ばなかった→状態 (2,150)

  150 円    200 円 
  100P

250P

100 円
50P
3 本目を選んだ→状態 (3,50)→ 終了

  150 円    200 円 
  100P

250P

100 円
50P
3 本目を選んだ→状態 (3,50)→ 終了

  150 円    200 円 
  100P

250P

100 円
50P
まとめ
●

このような複数ある選択肢を、「これを選
んだらどうなるか」というのを全て調べ上
げるのが全探索

●

あり得るパラレルワールドを全部試してみ
るということ
初期状態 0P

0,300
初期状態 0P

       
0,300
1,150

選んだ
100P

1 本目

選ばなかった

1,300

0P
初期状態 0P

       
0,300
1,150

選んだ
100P

選んだ
選ばなかった
100P
350P

2,-50

2,150

2 本目

選ばなかった

1,300

0P

選んだ
選ばなかった
250P

2,100

1,300

0P
初期状態 0P

       
0,300
1,150

選んだ
100P

選ばなかった

1,300

選んだ
選ばなかった
100P
350P

2,150

2,-50

選んだ 選ばなかった

3,50

150P

0P

選んだ
選ばなかった
250P

0P

1,300

2,100
選んだ 選ばなかった

100P

3,150

3 本目

300P

3,0

選んだ
250P
選ばなかった
3,100
50P

3,50

0P

3,150
初期状態 0P

       
0,300
1,150

選んだ
100P

選ばなかった

1,300

選んだ
選ばなかった
100P
350P

2,150

2,-50

選んだ 選ばなかった

3,50

150P

0P

選んだ
選ばなかった
250P

0P

1,300

2,100
選んだ 選ばなかった

100P

3,150

3 本目

300P

3,0

選んだ
250P
選ばなかった
3,100
50P

3,50

0P

3,150
初期状態 0P

       
0,300
1,150

選んだ
100P

選ばなかった

1,300

選んだ
選ばなかった
100P
350P

2,150

2,-50

選んだ 選ばなかった

3,50

150P

0P

選んだ
選ばなかった
250P

0P

1,300

2,100
選んだ 選ばなかった

100P

3,150

3 本目

300P

3,0

選んだ
250P
選ばなかった
3,100
50P

3,50

0P

3,150
コードについて
●

前回のコードでは
その状態からいける状態で最良のものを返
す
ようにしていた

●

全体のうちで最良のものを保存する変数を
グローバルにとっておくとわかりやすい
int N,S,W[20],V[20];
int ans = 0; // 答えの変数
void nap(int T,int U,int i){ // 使ったお金 T 、選ん
だ品物の価値の合計 U 、見終わった品物の数 i
  if(i==N){ ans = max(ans , U); return 0; }
//N 個見終わった→終了 答えの変数を更新
  if(T+W[i]<=S)nap(T+W[i],U+V[i],i+1); // 選んだ
  nap(T,U,i+1); // 選ばなかった
}
// nap(0,0,0) が答え
全探索の実際
●

DP と呼ばれる手法を理解するための準備
のようなもの

●

実際にはそんなに使わない

●

ので実装できなくてもよい

●

が「状態・状態の遷移」という概念は重要

More Related Content

Featured

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)
 

Featured (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...
 

全探索2