SlideShare a Scribd company logo
1 of 13
アルゴリズムいちねんせい
AGC010 B問題の解き方
@takepan
AGC010 問題B
• 問題を眺めます。
• ピンときた人は、「アルゴリズムいちねんせい」卒業です。このファイルを閉じ
てください。
• ピンと来なかった人は、次のページを見てみましょう。
AGC010 問題B
上記の状態から、箱2を「君に決めた!」と言いつつ、箱3から1個、箱4から2個、箱5から3個、
その次の箱1から4個、箱2から5個、石を取り出します。
• 入力例1の解説
箱1 箱2 箱3 箱4 箱5
4 5 1 2 3
箱1 箱2 箱3 箱4 箱5
4 5 1 2 3
-4 -5 -1 -2 -3
0 0 0 0 0
すべての箱の中の石を取り除けたので、「YES」を出力します。
入力例1では説明文がついていましたが、ここには解説がありません。いきなり大海原に投げ
出された気分ですね。
入力例1では4,5と1,2,3となんとなく数字が単純に増えていましたが、今回は減っている箇
所があります。(赤の矢印のところ)
AGC010 問題B
• 入力例2の解説
箱1 箱2 箱3 箱4 箱5
6 9 12 10 8
箱1 箱2 箱3 箱4 箱5
6 9 12 10 8
円構造であることを分かりやすくするため、箱5のあとに箱1を追加しました。
こう見ると、3箇所で減っているのが分かります。しかも綺麗に2ずつです。
どの箱からかは分かりませんが、そこを起点に1,2,3,4,5個を取り出していきます。
途中で取る個数が減るのは、5→1のところです。そこに当てはめて考えてみます。
AGC010 問題B
• 入力例2の解説
箱1 箱2 箱3 箱4 箱5 箱1
6 9 12 10 8 6
上記の表のように、石を取っていくとすべての箱の中の石を取り除くことができます。
なので、答えは「YES」です。
AGC010 問題B
• 入力例2の解説
箱1 箱2 箱3 箱4 箱5 箱1
6 9 12 10 8 6
-3 -4 -5 -1 -2 -3
-2 -3 -4 -5 -1 -2
-1 -2 -3 -4 -5 -1
0 0 0 0 0 0
箱5個のときは、取り出す石の数は1、2、3、4、5なので、1回に合計15個の石を取り出すことに
なります。
箱の合計の石の数の合計(6+9+12+10+8)は45個なので、取り出す操作は3回行います。
そうなると、箱の中の石の数の並びで、「減る箇所」が「のべ3回」発生することになります。
「のべ」と書いたのは、3回とも同じ箇所で減るケースがあるからです。
AGC010 問題B
• 入力例2の解説
勝手に入力例4を作りました。減る箇所は1箇所です。
入力例2では減る箇所の減る数は2でしたが、今回は12も減っています。なぜでしょうか?
これは、隣り合う箱の数の差は、1回の操作では1箇所以外は「右の箱」引く「左の箱」が1で、
1箇所だけマイナスN-1(今回はNが5なので-4)だからです。
入力例2の3回の操作では、増える回数が2、減る回数が1で「+1」「+1」「-4」で合計-2でした。
これが重なると「-4」「-4」「-4」で合計-12になる訳ですね。
AGC010 問題B
• 入力例4
箱1 箱2 箱3 箱4 箱5 箱1
3 6 9 12 15 3
-1 -2 -3 -4 -5 -1
-1 -2 -3 -4 -5 -1
-1 -2 -3 -4 -5 -1
0 0 0 0 0 0
※「取り出す箇所の切れ目」は、今までのページに出てきた、矢印の部分のことです。
AGC010 問題B
• まとめると
• 1度も減らない箇所は、取り出す操作の回数だけ増える
• 上記に当てはまらない箇所は、「取り出す箇所の切れ目」がある。
「取り出す回数に足りない数」÷(N-1)
で、何回切れ目になっているか分かる
AGC010 問題B
2~4行目はデータの読み込み部分です。
PHPの「おまじない」です。
石の総計を計算し、Nによって定まる「1回の操
作で取り出す石の数」を計算します。
石の総数を「1回の操作で取り出す石の数」で
割り、整数でなければ「NO」を出力して終了し
ます。
• 実装
AGC010 問題B
• 実装
配列の最後に1番めの箱の石の数を追加して
います。
一度も切れ目がない箇所では、$diffは0になり
ます。
あ
今見返してみると、27行目と29行目は1行で済みますね。
($kaisuuの意味がない……)
$diffがマイナスで$cutもマイナスで数えている
ため、 $round==$cut ではなく、
$round==-$cut になっています。
$round+$cut==0 の方が分かりやすい
かもしれませんね。
AGC010 問題B
• おまけ
実は、コンテストのときに提出したプログラムに
はミスがありました。具体的には、27行目の
「floor()」(床関数による整数化)の部分があり
ませんでした。
その場合、例えばN=4で取り出し操作が3回、1
番目と3番目の箱の前で切れ目が「1.5回ずつ」
ある場合に「YES」と出力してしまいます。
TopCoderやcodeForcesならばたぶん撃墜され
ていたはずで、これが正解にしてもらえるのは、
AtCoderならでは、だったかもしれません。
4
6 9 6 9
おしまい

More Related Content

Featured

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 2024Albert 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 InsightsKurio // 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 2024Search 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 summarySpeakerHub
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit 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 managementMindGenius
 
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 WorkGetSmarter
 
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 PresentationErica 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 wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

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
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

アルゴリズムいちねんせい AGC010 B