QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#394901#1375. TripTikrania__#TL 4ms36656kbC++203.3kb2024-04-20 21:31:112024-04-20 21:31:11

Judging History

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

  • [2024-04-20 21:31:11]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:36656kb
  • [2024-04-20 21:31:11]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10;
int n, k;
pair<int, int> arr[N];

struct node {
    vector<int> v;
    int l, r;
};

node merge(node x, node y) {
    node ret;
    for (auto i: x.v)
        ret.v.push_back(i);
    for (auto i: y.v)
        ret.v.push_back(i);
    std::sort(ret.v.rbegin(), ret.v.rend());
    if (ret.v.size() > k)
        ret.v.resize(k);
    ret.l = x.l;
    ret.r = y.r;
    return ret;
}

node tree[4 * N];

void build(int idx = 1, int l = 0, int r = n) {
    if (l == r) {
        tree[idx].v = {arr[l].second};
        tree[idx].l = arr[l].first;
        tree[idx].r = arr[l].first;
        return;
    }
    int mid = (l + r) / 2;
    build(2 * idx, l, mid);
    build(2 * idx + 1, mid + 1, r);
    tree[idx] = merge(tree[2 * idx], tree[2 * idx + 1]);
}

node query(int wl, int wr, int idx = 1, int l = 0, int r = n) {
    if (wl <= tree[idx].l and tree[idx].r <= wr)
        return tree[idx];
    int mid = (l + r) / 2;
    node ret;
    ret.v.clear();
    if (wl <= tree[2 * idx].r)
        ret = merge(ret, query(wl, wr, 2 * idx, l, mid));
    if (wr >= tree[2 * idx + 1].l)
        ret = merge(ret, query(wl, wr, 2 * idx + 1, mid + 1, r));
    return ret;
}

int pos[N];

vector<int> fchilds(int node, int scope) {
    return query(arr[pos[node]].first - (1ll << scope), arr[pos[node]].first + (1ll << scope)).v;
}

void doWork() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i].first;
        arr[i].second = i;
    }

    arr[0] = {0, 0};
    sort(arr, arr + n + 1);
    for (int i = 0; i <= n; i++) {
        pos[arr[i].second] = i;
    }

    build();
    queue<pair<int, int>> q;
    vector<vector<int>> cost(n + 5, vector<int>(35, 1e9 + 7));
    cost[0][0] = 0;
    q.push({0, 0});
    while (q.size()) {
        int node = q.front().first;
        int scope = q.front().second;
        q.pop();
        if (scope != 29 and cost[node][scope] + 1 < cost[node][scope + 1]) {
            cost[node][scope + 1] = cost[node][scope] + 1;
            q.push({node, scope + 1});
        }
        if (scope != 0 and cost[node][scope] + 1 < cost[node][scope - 1]) {
            cost[node][scope - 1] = cost[node][scope] + 1;
            q.push({node, scope - 1});
        }

        vector<int> childs = fchilds(node, scope);
        for (auto to: childs) {
            if (cost[node][scope] + 1 < cost[to][scope]) {
                cost[to][scope] = cost[node][scope] + 1;
                q.push({to, scope});
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        int mn = cost[i][0] + 1;
        for (int j = 0; j < 30; j++) {
            auto temp = fchilds(i, j);
            for (auto to: temp)
                if (to == i)
                    mn = min(mn, cost[i][j]);
        }
        if (mn >= 1e9 + 7)
            mn = -1;
        cout << mn << '\n';
    }
}


signed main() {
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
//    freopen("bisector.in","r",stdin);
//    freopen("bisector.out","w",stdout);
    int t = 1;
    // cout << primes.size() << endl;
//    cin >> t;
    while (t--) {
        doWork();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 36656kb

input:

4 2
-2
-1
-3
2

output:

3
1
3
2

result:

ok 4 lines

Test #2:

score: -100
Time Limit Exceeded

input:

100000 4
-81032899
-96819000
35569221
67767248
74154777
75473369
3463319
18339682
-77145048
-26587182
-22902096
-55717109
-47237134
-44620229
-45753774
33183854
-15569807
72648777
-33842373
44545672
-48017305
71506102
33448860
-20688896
6205313
21527590
24985350
-14688810
29596074
-47051603
-9258675...

output:

56
49
48
47
47
50
38
42
-1
-1
51
55
51
52
51
46
41
48
46
44
47
47
49
44
37
-1
44
44
45
51
49
49
45
49
44
43
51
54
46
43
-1
44
49
57
46
45
43
47
46
53
48
44
47
-1
49
55
53
47
52
51
49
47
53
56
43
50
43
29
43
50
52
52
47
45
47
50
52
47
49
47
50
49
48
47
49
45
62
37
47
48
51
39
47
46
48
50
47
53
44
52
...

result: