結果
問題 |
No.3047 Verification of Sorting Network
|
ユーザー |
👑 |
提出日時 | 2025-02-01 19:05:10 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 4,717 bytes |
コンパイル時間 | 4,139 ms |
コンパイル使用メモリ | 291,556 KB |
実行使用メモリ | 8,612 KB |
最終ジャッジ日時 | 2025-03-05 20:34:59 |
合計ジャッジ時間 | 94,465 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 58 WA * 3 |
ソースコード
// 64bit高速化+乱択解 (おそらくWA) #include <bits/stdc++.h> // all using namespace std; #define MAX_T 10000 #define MAX_N 27 #define MAX_COST 2e8 class XorShift64 { public: using result_type = uint64_t; XorShift64(uint64_t seed) : state(seed) {} uint64_t operator()() { state ^= state << 13; state ^= state >> 7; state ^= state << 17; return state; } static constexpr uint64_t min() { return 0; } static constexpr uint64_t max() { return UINT64_MAX; } private: uint64_t state; }; expected<vector<bool>, vector<bool>> is_sorting_network_j64(const int n, const vector<pair<int, int>> cmps) { assert(2 <= n && n <= MAX_N && MAX_N <= UINT64_WIDTH); for (auto [a, b] : cmps) { // 0-indexed assert(0 <= a && a < b && b < n); } int m = cmps.size(); vector<bool> unused(m, true), unsorted(n - 1, false); array<array<uint64_t, 4>, MAX_N> state; random_device seed_gen; mt19937_64 rng(seed_gen() ^ chrono::steady_clock::now().time_since_epoch().count()); //XorShift64 rng(seed_gen() ^ chrono::steady_clock::now().time_since_epoch().count()); const double phi = sqrt(1.25) + 0.5; uint64_t limit = pow(phi, n) * 100; // 試行回数の上限、でもよく見落とす for (uint64_t i = 0; i < limit; i += 64) { for (int j = 0; j < n; ++j) { state[j][0] = rng(); state[j][1] = rng(); state[j][2] = rng(); state[j][3] = rng(); } for (int j = 0; auto [a, b] : cmps) { auto va0 = state[a][0], va1 = state[a][1], va2 = state[a][2], va3 = state[a][3]; auto vb0 = state[b][0], vb1 = state[b][1], vb2 = state[b][2], vb3 = state[b][3]; auto na0 = va0 & vb0, na1 = va1 & vb1, na2 = va2 & vb2, na3 = va3 & vb3; auto nb0 = va0 | vb0, nb1 = va1 | vb1, nb2 = va2 | vb2, nb3 = va3 | vb3; state[a][0] = na0; state[a][1] = na1; state[a][2] = na2; state[a][3] = na3; state[b][0] = nb0; state[b][1] = nb1; state[b][2] = nb2; state[b][3] = nb3; if (va0 != na0 || va1 != na1 || va2 != na2 || va3 != na3) { unused[j] = false; } ++j; } for (int j = 1; j < n; ++j) { if ((state[j - 1][0] & ~state[j][0]) != 0 || (state[j - 1][1] & ~state[j][1]) != 0 || (state[j - 1][2] & ~state[j][2]) != 0 || (state[j - 1][3] & ~state[j][3]) != 0) { unsorted[j - 1] = true; } } } if (any_of(unsorted.begin(), unsorted.end(), [](bool x) { return x; })) { return unexpected(unsorted); } return unused; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; assert(1 <= t && t <= MAX_T); // phi = (1 + sqrt(5)) / 2 : golden ratio double phi = sqrt(1.25) + 0.5; // testcases cost <= 1e8 double cost = 0.0; for (int i = 0; i < t; ++i) { int n, m; cin >> n >> m; vector<pair<int, int>> cmps; assert(2 <= n && n <= MAX_N && 1 <= m && m <= n * (n - 1) / 2); cost += m * pow(phi, n); assert(cost <= MAX_COST); vector<int> vec_a, vec_b; for (int j = 0; j < m; ++j) { int a; cin >> a; vec_a.push_back(a); } for (int j = 0; j < m; ++j) { int b; cin >> b; vec_b.push_back(b); } for (int j = 0; j < m; ++j) { int a = vec_a[j], b = vec_b[j]; assert(1 <= a && a < b && b <= n); // 1-indexed to 0-indexed cmps.emplace_back(a - 1, b - 1); } auto is_sorting = is_sorting_network_j64(n, cmps); if (is_sorting.has_value()) { auto unused = is_sorting.value(); assert(unused.size() == m); cout << "Yes\n"; cout << count(unused.begin(), unused.end(), true) << '\n'; // 1-indexed for (int j = 1; const auto x : unused) { if (x) { cout << j << ' '; } j++; } cout << '\n'; } else { auto unsorted = is_sorting.error(); assert(unsorted.size() == n - 1); cout << "No\n"; cout << count(unsorted.begin(), unsorted.end(), true) << '\n'; // 1-indexed for (int j = 1; const auto x : unsorted) { if (x) { cout << j << ' '; } j++; } cout << '\n'; } } return 0; }