Official
A - バス停の時刻表 / Bus Stop Timetable Editorial
by
A - バス停の時刻表 / Bus Stop Timetable Editorial
by
kyopro_friends
Editorial for printing
初心者の方へ
- AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
- また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
問題文の指示通り、全ての \(i\) についての \(T_{i-1}-T_i\) を計算し、その最小値と最大値を求めればよいです。
多くの言語では、配列の中から最大値・最小値を探すメソッドが提供されており、自分で for 文を書く必要はありません。
実装例 (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> t(n);
for(int i=0; i<n; i++) cin >> t[i];
vector<int> d(n-1);
for(int i=0; i<n-1; i++) d[i] = t[i+1] - t[i];
int m = *min_element(d.begin(), d.end());
int M = *max_element(d.begin(), d.end());
cout << m << ' ' << M << endl;
}
実装例 (Python)
N = int(input())
T = list(map(int, input().split()))
d = [T[i+1] - T[i] for i in range(N-1)]
print(min(d), max(d))
posted:
last update:
