Chmin Chmax
(utilities/chmin_chmax.hpp)
- View this file on GitHub
- Last update: 2026-06-16 01:13:59+09:00
- Include:
#include "utilities/chmin_chmax.hpp"
Overview
Small update helpers for dynamic programming and relaxation code. Each function updates the first argument only when the new value improves it, and returns whether the update happened.
Functions
| Function | Description | Complexity |
|---|---|---|
bool chmin(T& a, const U& b) |
Sets a = b if b < a. |
$O(1)$ |
bool chmax(T& a, const U& b) |
Sets a = b if a < b. |
$O(1)$ |
Example
#include "utilities/chmin_chmax.hpp"
#include <iostream>
int main() {
int best = 100;
bool updated = m1une::utilities::chmin(best, 42);
std::cout << updated << " " << best << "\n";
}
Verified with
Code
#ifndef M1UNE_CHMIN_CHMAX_HPP
#define M1UNE_CHMIN_CHMAX_HPP 1
namespace m1une {
namespace utilities {
template <typename T, typename U>
bool chmin(T& a, const U& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
bool chmax(T& a, const U& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
} // namespace utilities
} // namespace m1une
#endif // M1UNE_CHMIN_CHMAX_HPP#line 1 "utilities/chmin_chmax.hpp"
namespace m1une {
namespace utilities {
template <typename T, typename U>
bool chmin(T& a, const U& b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
bool chmax(T& a, const U& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
} // namespace utilities
} // namespace m1une