社内se × プログラマ × ビッグデータ

プログラミングなどITに興味があります。

AOJ Maximum Profit

Maximum Profit

c++ がほぼ初見のため、解答例を参照。
1. using namespace stdは、std名前空間を使うという宣言。
通常、cin や coutはstd::coutと表現し、std名前空間のcoutを使うという意味になる。
using namespace stdを宣言しておくことで、stdを明示的に書く必要がなくなる。

2. 以下のように書くことで、1個目の標準入力を n に代入し、以降の標準入力は順次 R[i] 配列に代入される。

cin >> n;
for (int i = 0; i < n; i++) cin >> R[i];

3. 以下のように書くことで、maxv の内容が標準出力される。また、endl を付けることで改行される。

cout << maxv << endl;

解答例

#include<iostream>
#include<algorithm>
using namespace std;
static const int MAX = 200000;

int main() {
	int R[MAX], n;

	cin >> n;
	for (int i = 0; i < n; i++) cin >> R[i];

	int maxv = -2000000000;
	int minv = R[0];

	for (int i = 1; i < n; i++) {
		maxv = max(maxv, R[i] - minv);
		minv = min(minv, R[i]);
	}

	cout << maxv << endl;

	return 0;
}