結果
問題 |
No.531 エヌスクミ島の平和協定
|
ユーザー |
![]() |
提出日時 | 2025-05-14 12:58:10 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 4,458 bytes |
コンパイル時間 | 403 ms |
コンパイル使用メモリ | 68,236 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-05-14 12:59:26 |
合計ジャッジ時間 | 1,531 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
#include <iostream> int main() { // Optimize standard I/O operations for performance. // This helps especially with large inputs, though for this problem N <= 100000 isn't huge. // It ensures that cin/cout operations are faster. std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); // Declare variables to store the number of animals (n) and boat capacity (m). // Use long long to accommodate n up to 100000. long long n; long long m; // Read the input values for n and m from standard input. std::cin >> n >> m; // The core logic depends on comparing boat capacity 'm' with the number of animals 'n'. // Case 1: Boat capacity is sufficient to carry all animals at once. if (m >= n) { // If m >= n, we can load all n animals onto a single boat and move them // from Enuskumi Island to New Enuskumi Island in one trip. // This trip takes exactly 1 day. std::cout << 1 << std::endl; } else { // Case 2: Boat capacity is less than the number of animals (m < n). // In this case, we cannot move all animals in a single trip. We need to consider // if intermediate steps are possible while respecting the rule. // The rule "every animal must be both a predator and a prey" effectively means that // on any island, if there are any animals present, the set of animals must satisfy // certain properties related to the cyclic predation structure. // Analysis shows that the only "valid" sets of animals on an island are: // 1. The empty set (no animals). // 2. The set of all N animals. // 3. If N is even, the set of all odd-indexed animals (S_odd) and the set of all // even-indexed animals (S_even) are also valid. Both S_odd and S_even have size N/2. // Any state transition (move) must result in a state where the animal sets on both islands are valid. // An intermediate state typically involves splitting the N animals into two non-empty sets: // S animals on New Enuskumi and A\S animals remaining on Enuskumi. // Check if n is odd. if (n % 2 != 0) { // If n is odd, the only valid sets are the empty set and the set of all N animals. // It's impossible to partition the N animals into two non-empty valid sets S and A\S. // Since m < n, we cannot move all animals at once. Any partial move (moving a proper subset S) // would result in an invalid state because neither S nor A\S would be A or empty. // Therefore, the move is impossible if n is odd and m < n. std::cout << -1 << std::endl; } else { // If n is even: // The sets S_odd and S_even (each size N/2) provide a valid partition of A. // This means an intermediate state where S_odd is on one island and S_even on the other is potentially valid. // To reach this state or move from it, we need to transport groups of size N/2. // Check if the boat capacity m is sufficient to carry a group of size N/2. if (m >= n / 2) { // If m >= n/2, we have enough capacity to move S_odd or S_even. // The move can be done in two steps: // Day 1: Move S_odd (for example) from Enuskumi to New Enuskumi. Requires capacity N/2. // After Day 1, state is (S_even on Enuskumi, S_odd on New Enuskumi). This is a valid state. // Day 2: Move S_even from Enuskumi to New Enuskumi. Requires capacity N/2. // After Day 2, state is (Empty on Enuskumi, A on New Enuskumi). Goal reached. // Total time required is 2 days. std::cout << 2 << std::endl; } else { // If m < n/2: // The boat capacity is insufficient to move S_odd or S_even (the smallest non-empty valid proper subsets). // Since moving any smaller group S would result in an invalid state (as S and A\S wouldn't both be valid), // no valid move is possible from the initial state if m < n/2. // Therefore, the move is impossible. std::cout << -1 << std::endl; } } } // Return 0 to indicate successful program execution. return 0; }