結果

問題 No.281 門松と魔法(1)
ユーザー not_522
提出日時 2020-01-02 23:52:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 4,999 bytes
コンパイル時間 1,058 ms
コンパイル使用メモリ 98,996 KB
最終ジャッジ日時 2025-01-08 15:55:15
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

// This is free and unencumbered software released into the public domain.
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// For more information, please refer to <http://unlicense.org>
/****************/
/* template.hpp */
/****************/
#include <algorithm>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
using std::cerr;
using std::cout;
using std::endl;
using std::max;
using std::min;
using std::swap;
struct BoolName : std::numpunct<char> {
std::string t, f;
BoolName(std::string t, std::string f) : t(t), f(f) {}
std::string do_truename() const { return t; }
std::string do_falsename() const { return f; }
};
void setBoolName(std::string t, std::string f) {
cout.imbue(std::locale(cout.getloc(), new BoolName(t, f)));
}
struct Initializer {
Initializer() {
cout << std::fixed << std::setprecision(15) << std::boolalpha;
setBoolName("Yes", "No");
}
} initializer;
struct Input {
bool eof;
Input() : eof(false) {}
operator char() {
char v;
this->eof = (std::scanf("%c", &v) != 1);
return v;
}
operator int() {
int v;
this->eof = (std::scanf("%d", &v) != 1);
return v;
}
operator long() {
long v;
this->eof = (std::scanf("%ld", &v) != 1);
return v;
}
operator long long() {
long long v;
this->eof = (std::scanf("%lld", &v) != 1);
return v;
}
operator unsigned int() {
unsigned int v;
this->eof = (std::scanf("%u", &v) != 1);
return v;
}
operator unsigned long() {
unsigned long v;
this->eof = (std::scanf("%lu", &v) != 1);
return v;
}
operator unsigned long long() {
unsigned long long v;
this->eof = (std::scanf("%llu", &v) != 1);
return v;
}
operator double() {
double v;
this->eof = (std::scanf("%lf", &v) != 1);
return v;
}
operator long double() {
long double v;
this->eof = (std::scanf("%Lf", &v) != 1);
return v;
}
void ignore() const { getchar(); }
} in;
template <typename T> T abs(T a) { return a >= 0 ? a : -a; }
template <typename T, typename S> bool chmin(T &a, const S &b) {
return a > b ? a = b, true : false;
}
template <typename T, typename S> bool chmax(T &a, const S &b) {
return a < b ? a = b, true : false;
}
template <typename T, typename S> std::function<S(T)> cast() {
return [](const T &t) { return static_cast<S>(t); };
}
template <typename T> T copy(const T &a) { return T(a); }
class ZeroPadding {
public:
ZeroPadding(int n) : n(n) {}
int n;
};
std::ostream &operator<<(std::ostream &os, const ZeroPadding &z) {
os << std::setw(z.n) << std::setfill('0');
return os;
}
template <typename T> constexpr T inf() {
return std::numeric_limits<T>::max() / 2 - 1;
}
/************/
/* main.cpp */
/************/
int d;
void magic(int &n, int &res, int t = 1) {
n -= d * t;
if (n < 0) {
n = 0;
}
res += t;
}
bool isKadomatsu(int h1, int h2, int h3) {
return h1 != h2 && h1 != h3 && h2 != h3 &&
((h2 > h1 && h2 > h3) || (h2 < h1 && h2 < h3));
}
int solve1(int h1, int h2, int h3) {
if (h2 == 0) {
return inf<int>();
}
int res = 0;
if (h1 >= h2) {
magic(h1, res, (h1 - h2) / d + 1);
}
if (h3 >= h2) {
magic(h3, res, (h3 - h2) / d + 1);
}
if (h1 == h3) {
magic(h1, res);
}
if (h1 == h3) {
return inf<int>();
}
return res;
}
int solve2(int h1, int h2, int h3) {
int res = 0;
if (h1 == h3) {
magic(h1, res);
}
if (h1 == 0 || h3 == 0) {
return inf<int>();
}
if (min(h1, h3) <= h2) {
magic(h2, res, (h2 - min(h1, h3)) / d + 1);
}
return res;
}
int main() {
d = in;
int h1(in), h2(in), h3(in);
if (isKadomatsu(h1, h2, h3)) {
cout << 0 << endl;
} else if (d == 0) {
cout << -1 << endl;
} else {
int res = min(solve1(h1, h2, h3), solve2(h1, h2, h3));
cout << (res == inf<int>() ? -1 : res) << endl;
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0