結果
問題 | No.2675 KUMA |
ユーザー | Xelmeph |
提出日時 | 2024-03-09 11:37:51 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 548 ms / 2,000 ms |
コード長 | 3,796 bytes |
コンパイル時間 | 1,538 ms |
コンパイル使用メモリ | 164,408 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-09-29 21:13:20 |
合計ジャッジ時間 | 6,821 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 47 |
ソースコード
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <array> #include <vector> #include <deque> #include <list> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <stack> #include <queue> #include <bitset> #include <tuple> #include <cmath> #include <complex> #include <algorithm> #include <utility> #include <regex> #include <cstdint> #include <numeric> #include <functional> #include <cassert> using namespace std; namespace utils{ #define ALL(x) begin(x), end(x) #define RALL(x) rbegin(x), rend(x) ///----- aliases using ll = long long int; using ull = unsigned long long; template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>; template<class T> using min_queue = p_queue<T, greater<T>>; template<class T> using max_queue = p_queue<T, less<T>>; template<class T> inline bool CHMIN(T& X, const T& A){ if(X > A) {X = A; return true;} return false; } template<class T> inline bool CHMAX(T& X, const T& A){ if(X < A) {X = A; return true;} return false; } ///----- tuple I/O template <class S, class T> istream& operator>>(istream& is, tuple<S, T>& t){ return is >> get<0>(t) >> get<1>(t); } template<class T> istream& operator>>(istream& is, vector<T>& v){ for (auto &&x : v) { is >> x; } return is; } ///----- constants constexpr ll INFLL = 1'000'000'000'000'000'020ll; constexpr int INF = 1'000'000'009; constexpr double PI = 3.14'159'265'358'979'323'846; constexpr double EPS = 1e-12; } using namespace utils; class solver{ istream& is; ostream& os; public: solver(istream& I, ostream& O) :is(I), os(O) {} int N; vector<tuple<int, int>> xy; bool input() { is >> N; xy.resize(N); is >> xy; return !!is; } void run(); }; using Coord = tuple<int,int>; Coord operator+(const Coord& a, const Coord& b){ return {get<0>(a) + get<0>(b), get<1>(a) + get<1>(b)}; } void solver::run(){ if(!input()) return; set<Coord> V; map<Coord, int> UMA; vector<tuple<Coord, Coord>> kma(4); // EWSN kma[0] = {{2, -1}, {2, 1}}; kma[1] = {{-2, -1}, {-2, 1}}; kma[2] = {{-1, -2}, {1, -2}}; kma[3] = {{-1, 2}, {1, 2}}; for (int i = 0; i < N; ++i) { auto&& xyi = xy[i]; V.emplace(xyi); UMA.emplace(xyi, i); for (auto &&[ka,kb]: kma) { V.insert(xyi + ka); V.insert(xyi + kb); } // end [kx,ky] } // end i vector<int> minK(1<<N, INF); minK[0] = 0; for (auto &&p: V) { if(UMA.count(p)) continue; vector<int> upd = minK; for (int i = 0; i < upd.size(); ++i) { for (auto &&km:kma) { unsigned sp = 0; auto [a,b] = km; if(UMA.count(p + a)){ sp |= 1u << UMA[p + a]; } if(UMA.count(p + b)){ sp |= 1u << UMA[p + b]; } CHMIN(upd[i|sp], minK[i] + 1); } // end km } // end i minK = std::move(upd); } // end p if(minK.back() == INF){ os << "-1\n"; } else os << minK.back() << '\n'; } int main(int argc, char *argv[]) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << setprecision(16) << scientific; #ifdef XELMH string test_cases = "test_e.txt"; cerr << test_cases << " -->" << endl; auto fs = fstream(test_cases, fstream::in); int loop = 0; while(fs) { loop++; cout << '#' << loop << "#------\n"; solver(fs, cout).run(); } if(loop <= 1) { cout << "===" << endl; while(cin) solver(cin, cout).run(); } #else solver(cin, cout).run(); #endif return 0; }