【AtCoder】ABC 448 A - chmin
AtCoder/ABC/A問題AtCoder/ABC/100点問題AtCoder/灰DiffAtCoder/NoviSteps/7QAtCoder/アルゴリズムの基礎/条件分岐AtCoder/アルゴリズムの基礎/forループ競技プログラミング

A - chmin
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
AtCoder
実行時間制限: 2 sec / メモリ制限: 1024 MiB / Difficulty: 13 / NoviSteps: 7Q / 配点: 100 点
問題概要
長さ の整数列 と整数 が与えられる。 の順に以下の処理を行え。
- もし なら、 に更新した上で
1を出力する。 - そうでないなら
0を出力する。
制約
- 入力は全て整数
考察
問題のタイトルにあるように、これはいわゆるchmin関数の処理である。
私はchminを以下のようにbool型が返るように実装しているので、chmin(X, A_i)がtrueならば1を出力し、そうでないなら0を出力すればよい。
CPP
1.template <typename T>2.inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }実装例
CPP
1.#include <bits/stdc++.h>2.using namespace std;3. 4.#define rep(i, start, end) for (auto i = (start); (i) < (end); (i)++)5. 6.template <typename T>7.inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }8. 9.// ======================================== //10. 11.int main()12.{13. int N, X;14. cin >> N >> X;15. vector<int> A(N);16. rep(i, 0, N) cin >> A[i];17. 18. rep(i, 0, N)19. {20. if (chmin(X, A[i]))21. {22. cout << 1 << endl;23. }24. else25. {26. cout << 0 << endl;27. }28. }29. 30. return 0;31.}
Submission #73885158 - AtCoder Beginner Contest 448
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
AtCoder
実装時間: 5 分以内
コメント
テンプレートがある人は貼るだけになりそう。





