結果
問題 | No.281 門松と魔法(1) |
ユーザー |
![]() |
提出日時 | 2016-04-13 13:53:49 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 1,695 bytes |
コンパイル時間 | 867 ms |
コンパイル使用メモリ | 84,444 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-06 20:44:55 |
合計ジャッジ時間 | 2,273 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 57 |
ソースコード
/* -*- coding: utf-8 -*- * * 281.cc: No.281 門松と魔法(1) - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int INF = 1 << 30; /* typedef */ /* global variables */ /* subroutines */ inline bool is_kadomatsu(int h0, int h1, int h2) { return (h0 != h2 && ((h0 < h1 && h1 > h2) || (h0 > h1 && h1 < h2))); } void finish(int ans) { printf("%d\n", ans); exit(0); } /* main */ int main() { int d, h0, h1, h2; cin >> d >> h0 >> h1 >> h2; if (d == 0) finish(is_kadomatsu(h0, h1, h2) ? 0 : -1); int cnt = 0; if (h0 == h2) { h0 = max(0, h0 - d); cnt++; if (h0 == h2) finish(-1); } if (is_kadomatsu(h0, h1, h2)) finish(cnt); // keep h0 < h2 if (h0 > h2) swap(h0, h2); // h0 < h1 > h2 int c0 = INF; if (h1 > 0) { c0 = cnt; int ch0 = h0, ch2 = h2; if (ch0 >= h1) { int e = (ch0 - h1) / d + 1; c0 += e; ch0 = max(ch0 - e * d, 0); } if (ch2 >= h1) { int e = (ch2 - h1) / d + 1; c0 += e; ch2 = max(ch2 - e * d, 0); } if (ch0 == ch2) { if (ch0 == 0) c0 = INF; else c0++; } } // h0 > h1 < h2 int c1 = INF; if (h0 > 0) { c1 = cnt; int ch1 = h1; if (h0 <= ch1) { int e = (ch1 - h0) / d + 1; c1 += e; ch1 = max(ch1 - e * d, 0); } } int c = min(c0, c1); finish((c >= INF) ? -1 : c); return 0; }