QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#304260#8004. Bit Componentucup-team133#WA 1ms3560kbC++234.3kb2024-01-13 17:05:292024-01-13 17:05:29

Judging History

你现在查看的是最新测评结果

  • [2024-01-13 17:05:29]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3560kb
  • [2024-01-13 17:05:29]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

vector<string> perm_to_field(int n, const vector<int>& p) {
    int w = topbit(n) + 1;
    vector<string> v;
    for (int i = 0; i < n; i++) {
        string s = "";
        for (int j = 0; j < w; j++) s += char('0' + (p[i] >> j & 1));
        ranges::reverse(s);
        v.emplace_back(s);
    }
    return v;
}

int component(int n, const vector<int>& p) {
    int w = topbit(n) + 1;
    auto v = perm_to_field(n, p);
    vector seen(n, vector<bool>(w, false));
    int res = 0;
    auto dfs = [&](auto self, int x, int y) -> void {
        seen[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (clamp(nx, 0, n - 1) != nx or clamp(ny, 0, w - 1) != ny) continue;
            if (v[nx][ny] != '1') continue;
            if (seen[nx][ny]) continue;
            self(self, nx, ny);
        }
    };

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < w; j++) {
            if (v[i][j] != '1') continue;
            if (seen[i][j]) continue;
            dfs(dfs, i, j);
            res++;
        }
    }

    return res;
}

vector<int> gray_code(int n) {
    vector<int> res;
    for (int i = 0; i < n; i++) res.emplace_back(i ^ (i >> 1));
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;

    int top = topbit(n);
    if (n == 1) {
        cout << "YES\n";
        cout << "1\n";
        return 0;
    }
    if (n == 2) {
        cout << "NO\n";
        return 0;
    }
    if (n == 3) {
        cout << "YES\n";
        cout << "2 3 1\n";
        return 0;
    }

    if (n < (1 << top) + (1 << (top - 1)) + 1) {
        cout << "NO\n";
        return 0;
    }

    if (n == (1 << (top + 1)) - 1) {
        cout << "YES\n";
        auto ord = gray_code(1 << (top + 1));
        for (int i = 1; i <= n; i++) cout << ord[i] << (i == n ? "\n" : " ");
        return 0;
    }

    vector<int> ans;
    auto org = gray_code(1 << top);
    debug(org);
    for (int i = 1; i < int(org.size()); i++) {
        ans.emplace_back(org[i]);
        if ((org[i] >> (top - 1) & 1) and org[i] + (1 << top) <= n) ans.emplace_back(org[i] + (1 << top));
        if (org[i] == (1 << (top - 1)) + (1 << (top - 2))) {
            assert(org[i + 1] == org[i] + 1);
            ans.emplace_back((1 << top) + (1 << (top - 1)));
            auto org2 = gray_code(1 << (top - 1));
            ans.emplace_back(1 << top);
            ranges::reverse(org2);
            org2.pop_back();
            for (int& val : org2) ans.emplace_back(val + (1 << top));
            ans.emplace_back((1 << top) + (1 << (top - 1)) + 1);
        }
    }
    cout << "YES\n";
    for (int i = 0; i < n; i++) cout << ans[i] << (i + 1 == n ? "\n" : " ");
#ifdef LOCAL
    assert(component(n, ans) == 1);
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3516kb

input:

1

output:

YES
1

result:

ok answer is 1

Test #2:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

2

output:

NO

result:

ok answer is 0

Test #3:

score: 0
Accepted
time: 1ms
memory: 3560kb

input:

3

output:

YES
2 3 1

result:

ok answer is 1

Test #4:

score: 0
Accepted
time: 0ms
memory: 3444kb

input:

4

output:

NO

result:

ok answer is 0

Test #5:

score: 0
Accepted
time: 1ms
memory: 3444kb

input:

5

output:

NO

result:

ok answer is 0

Test #6:

score: 0
Accepted
time: 0ms
memory: 3452kb

input:

6

output:

NO

result:

ok answer is 0

Test #7:

score: 0
Accepted
time: 0ms
memory: 3444kb

input:

7

output:

YES
1 3 2 6 7 5 4

result:

ok answer is 1

Test #8:

score: 0
Accepted
time: 0ms
memory: 3492kb

input:

8

output:

NO

result:

ok answer is 0

Test #9:

score: 0
Accepted
time: 0ms
memory: 3488kb

input:

9

output:

NO

result:

ok answer is 0

Test #10:

score: 0
Accepted
time: 0ms
memory: 3440kb

input:

10

output:

NO

result:

ok answer is 0

Test #11:

score: 0
Accepted
time: 0ms
memory: 3444kb

input:

11

output:

NO

result:

ok answer is 0

Test #12:

score: 0
Accepted
time: 0ms
memory: 3444kb

input:

12

output:

NO

result:

ok answer is 0

Test #13:

score: -100
Wrong Answer
time: 0ms
memory: 3484kb

input:

13

output:

YES
1 3 2 6 12 8 10 11 9 13 7 5 13

result:

wrong answer Every number must appear exactly once, but 4 appears 0 times