QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#801199#9806. Growing Treeucup-team135#WA 50ms36696kbC++233.5kb2024-12-06 19:23:512024-12-06 19:23:51

Judging History

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

  • [2024-12-06 19:23:51]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:36696kb
  • [2024-12-06 19:23:51]
  • 提交

answer

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int p=998244353;
int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}}
int inv(int x) {return po(x,p-2);}
mt19937 rnd;
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#define debugv(v) do {cout<< #v <<" : {"; for(int izxc=0;izxc<v.size();++izxc) {cout << v[izxc];if(izxc+1!=v.size()) cout << ","; }cout <<"}"<< endl;} while(0)
#else
#define debug(...)
#define debugv(v)
#endif
#define lob(a,x) lower_bound(all(a),x)
#define upb(a,x) upper_bound(all(a),x)
template<typename T>
using pqg = priority_queue<T,vector<T>, greater<>>;
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int N = 10;
    vector<vector<int>> lca(1 << (N + 1), vector<int>(1 << (N + 1)));
    for (int x = 1; x < lca.size(); ++x) {
        for (int y = 1; y < lca.size(); ++y) {
            int a = x, b = y;
            while (a != b) {
                if (a > b) swap(a, b); // a < b
                b /= 2;
            }
            lca[x][y] = a;
        }
    }
    int t;cin>>t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(1 << (n + 1));
        for (int i = 2; i < a.size(); ++i) cin >> a[i];
        vector<int> sum(1 << (n + 1));
        for (int i = 1; i < (1 << n); ++i) {
            sum[2 * i] = sum[i] + a[2 * i];
            sum[2 * i + 1] = sum[i] + a[2 * i + 1];
        }
        vector<int> sums = sum;
        sort(all(sums), [](int x, int y){ return x < y; });
        sums.erase(unique(all(sums)), sums.end());
        for (auto &s : sum) s = lower_bound(all(sums), s) - sums.begin();
        vector<vector<int>> below(1 << (n + 1));
        for (int i = 1 << n; i < (1 << (n + 1)); ++i) {
            below[i] = {sum[i]};
        }
        int ans = n + 1, cur = 0;
        auto first_unset = [&](int bit) {
            while (cur & (1 << bit)) {
                bit++;
            }
            return bit;
        };
        auto go = [&](auto go, int i) -> void {
            if (i == 0) {
                ans = min(ans, (int)__builtin_popcount(cur));
                return;
            }
            auto &v1 = below[2 * i];
            auto &v2 = below[2 * i + 1];
            auto &v = below[i];
            v.reserve(v1.size() + v2.size());
            int i1 = 0, i2 = 0;
            bool ok = 1;
            while (i1 < v1.size() && i2 < v2.size()) {
                if (v1[i1] == v2[i2]) {ok = 0; break;}
                if (v1[i1] < v2[i2]) { v.push_back(v1[i1++]); }
                else v.push_back(v2[i2++]);
            }
            if (ok) {
                //debug("ok");
                while (i1 < v1.size()) v.push_back(v1[i1++]);
                while (i2 < v2.size()) v.push_back(v2[i2++]);
                return go(go, i - 1);
            }
            int first = first_unset(__lg(i));
            if (first == n) return;//fail
            cur |= (1 << first);

            below[i] = below[2 * i];
            go(go, i - 1);
            below[i] = below[2 * i + 1];
            go(go, i - 1);

            cur &= ~(1 << first);
        };
        go(go, (1 << n) - 1);
        cout << (ans == n + 1 ? -1 : ans) << '\n';
    }
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 50ms
memory: 35932kb

input:

3
2
1 2 4 3 2 1
2
1 2 3 3 2 1
2
1 2 3 3 1 1

output:

1
2
-1

result:

ok 3 number(s): "1 2 -1"

Test #2:

score: -100
Wrong Answer
time: 49ms
memory: 36696kb

input:

94
5
44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2
3
83 3 74 14 45 17 22 41 62 74 25 1 56 22
7
21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...

output:

2
0
-1
2
0
1
-1
0
0
3
0
0
0
1
2
1
0
2
0
1
0
-1
0
-1
0
0
-1
-1
-1
-1
-1
3
-1
0
3
2
7
-1
-1
-1
1
2
4
0
0
2
7
1
6
0
-1
2
-1
0
0
0
-1
1
-1
-1
0
0
1
1
-1
0
1
2
0
-1
0
0
1
1
-1
0
-1
0
0
0
-1
3
-1
1
7
0
0
0
0
1
0
-1
3
3

result:

wrong answer 32nd numbers differ - expected: '4', found: '3'