QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#394829 | #1375. TripTik | rania__# | WA | 1ms | 5944kb | C++20 | 3.7kb | 2024-04-20 20:12:53 | 2024-04-20 20:12:54 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 5;
int n, k;
pair<int, int> arr[N];
struct node {
vector<int> v;
};
node merge(node x, node y) {
node ret;
ret.v.clear();
while ((x.v.size() or y.v.size()) and ret.v.size() < k) {
if (y.v.empty() or (x.v.size() and x.v.back() > y.v.back())) {
ret.v.push_back(x.v.back());
x.v.pop_back();
} else {
ret.v.push_back(y.v.back());
y.v.pop_back();
}
}
std::reverse(ret.v.begin(), ret.v.end());
return ret;
}
node tree[4 * N];
void build(int idx = 1, int l = 0, int r = n - 1) {
if (l == r) {
tree[idx].v = {arr[l].second};
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 - 1) {
if (wl <= l and r <= wr)
return tree[idx];
int mid = (l + r) / 2;
node ret;
if (wl <= mid)
ret = merge(ret, query(wl, wr, 2 * idx, l, mid));
if (wr > mid)
ret = merge(ret, query(wl, wr, 2 * idx + 1, mid + 1, r));
return ret;
}
int pos[N];
vector<int> fchilds(int node, int scope) {
int l = -1, r = -1;
int s = 0, e = n - 1, mid;
while (s <= e) {
mid = (s + e) / 2;
if (arr[pos[node]].first - (1ll << scope) <= arr[mid].first) {
l = mid;
e = mid - 1;
} else s = mid + 1;
}
s = 0, e = n - 1;
while (s <= e) {
mid = (s + e) / 2;
if (arr[pos[node]].first + (1ll << scope) >= arr[mid].first) {
r = mid;
s = mid + 1;
} else e = mid - 1;
}
if (l == -1 and r == -1)
return vector<int>();
if (r == -1)
r = l;
if (l == -1)
l = r;
auto ret = query(l,r).v;
return ret;
}
void doWork() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> arr[i].first;
arr[i].second = i;
}
arr[n] = {0, n};
sort(arr, arr + n);
for (int i = 0; i <= n; i++) {
pos[arr[i].second] = i;
}
build();
queue<pair<int, int>> q;
vector<vector<int>> cost(n + 1, vector<int>(30, 1e9 + 7));
cost[n][0] = 0;
q.push({n, 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 = 0; i < n; i++) {
int mn = 1e9 + 7;
for (int j = 0; j < 30; j++)
mn = min(mn, cost[i][j]);
if (mn == 1e9 + 7)
mn = -1;
cout << mn << '\n';
}
}
int 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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5944kb
input:
4 2 -2 -1 -3 2
output:
2 1 3 2
result:
wrong answer 1st lines differ - expected: '3', found: '2'