QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#798434#8790. First Billionq1w2e3r4#ML 0ms3720kbC++142.1kb2024-12-04 13:45:492024-12-04 13:45:49

Judging History

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

  • [2024-12-04 13:45:49]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:3720kb
  • [2024-12-04 13:45:49]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#include<random>

mt19937_64 rng(time(0));
int n;
int a[105];
vector<int> v[405];
const int SZ = 4000;

int findkth(int k, vector<int>& v){
    int len = v.size();
    int idx = rng() % len;
    int pivot = v[idx];

    vector<int> small, big;
    for(int x: v){
        if(x <= pivot) small.push_back(x);
        else big.push_back(x);
    }

    if(small.size() <= k) return findkth(k, small);
    else return findkth(k-small.size() , big);
}

void build(int id, int l, int r){
    if(l == r){
        v[id].push_back(a[l]);
        v[id].push_back(0);
        return;
    }
    int mid = l + r >> 1;
    build(id*2, l, mid);
    build(id*2+1, mid+1, r);
    
    vector<int> tmp;
    for(int x: v[id*2]){
        for(int y: v[id*2+1]){
            tmp.push_back(x+y);
        }
    }
    
    if(tmp.size() >= SZ){
        int n = tmp.size();
        int al = findkth(n/2 - SZ/2, tmp), ar = findkth(n/2 + SZ/2 - 1, tmp);
        
        for(int x: tmp){
            if(x >= al && x <= ar){
                v[id].push_back(x);
            }
        }
    }
    else{
        v[id] = tmp;
    }
}

void query(int id, int l, int r, int target, vector<int>& ans){
    // cout << "query " << l << " " << r << " " << target << endl;
    if(l == r){
        if(target == a[l]) ans.push_back(l);
        assert(target == 0 || target == a[l]);
        return;
    }

    int mid = l + r >> 1;
    for(auto x: v[id*2]){
        for(int y: v[id*2+1]){
            if(x + y == target){
                query(id*2, l, mid, x, ans);
                query(id*2+1, mid+1, r, y, ans);
                return;
            }
        }
    }
}

signed main(){
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];
    build(1,1,n);
    bool ok = false;
    for(auto x: v[1]){
        // cout << x << " ";
        if(x == 1e9) ok = true;
    }

    // assert(ok);
    vector<int> ans;
    query(1,1,n,(int)1e9,ans);
    cout << ans.size();
    for(int x: ans){
        cout << " " << x;
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3696kb

input:

10
386413329 88494216 245947398 316438989 192751270 204627269 65749456 3938400 150458676 345180997

output:

5 1 5 6 7 9

result:

ok OK (n = 10)

Test #2:

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

input:

10
119486233 299942886 169540407 349937991 597883752 32230162 140514533 57341098 12602102 220520836

output:

5 1 3 4 7 10

result:

ok OK (n = 10)

Test #3:

score: -100
Memory Limit Exceeded

input:

14
384615281 84612238 83310504 54746763 142296081 56775470 128760350 343006424 177232390 214368720 67220468 21895072 16352717 224807522

output:


result: