QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#41350 | #1375. TripTik | 2873531385 | WA | 4ms | 23568kb | C++ | 2.3kb | 2022-07-29 20:11:07 | 2022-07-29 20:11:09 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <queue>
#define ls (node << 1)
#define rs (node << 1 | 1)
using ll = long long;
#define int ll
const int N = 1e5+7, inf = 1e18+7;
using pir = std::pair<int, int> ;
int pos[N];
pir point[N];
int n, k;
int tmp[9], cnt;
struct Node {
int l, r, mx[4];
Node (int x = -1) {
for (int i = 0; i<4; ++i) mx[i] = x;
}
Node operator+(const Node& n) const {
Node res;
res.l = l, res.r = n.r;
int i = 0, j = 0;
while (i+j<k) {
if (mx[i]>n.mx[j]) {
res.mx[i+j] = mx[i];
++i;
} else {
res.mx[i+j] = n.mx[j];
++j;
}
}
return res;
}
}tree[N<<2];
void push_up(int node) {
tree[node] = tree[ls]+tree[rs];
}
void build(int node, int L, int R) {
tree[node].l = L, tree[node].r = R;
if (L==R) {
tree[node].mx[0] = point[L].second;
return ;
}
int mid = L+R>>1;
build(ls, L, mid);
build(rs, mid+1, R);
push_up(node);
}
Node query(int node, int L, int R) {
if (point[tree[node].l].first>=L && point[tree[node].r].first<=R) return tree[node];
if (point[tree[node].l].first>R || point[tree[node].r].first<L) return Node(-1);
return query(ls, L, R)+query(rs, L, R);
}
int dp[N][31];
bool vis[N][31];
signed main() {
std::cin >> n >> k;
for (int i = 1; i<=n; ++i) {
std::cin >> pos[i];
// pos[i]*=2;
point[i].first = pos[i];
point[i].second = i;
}
std::sort(point+1, point+1+n);
build(1, 1, n);
for (int i = 0; i<=n; ++i) {
for (int j = 0; j<31; ++j) dp[i][j] = -1;
}
dp[0][0] = 0;
std::queue<pir> Q;
Q.push({0, 0});
vis[0][0] = 1;
while (!Q.empty()) {
auto [i, j] = Q.front();
Q.pop();
if (j<=29 && !vis[i][j+1]) {
dp[i][j+1] = dp[i][j]+1;
vis[i][j+1] = 1;
Q.push({i, j+1});
}
if (j>0 && !vis[i][j-1]) {
dp[i][j-1] = dp[i][j]+1;
vis[i][j-1] = 1;
Q.push({i, j-1});
}
Node p = query(1, pos[i]-(1<<j), pos[i]+(1<<j));
for (int t = 0; t<k; ++t) {
if (p.mx[t]==-1) break;
if (!vis[p.mx[t]][j]) {
vis[p.mx[t]][j] = 1;
dp[p.mx[t]][j]=dp[i][j]+1;
Q.push({p.mx[t], j});
}
}
}
for (int i = 1; i<=n; ++i) {
int ans = inf;
for (int j = 0; j<=30; ++j) {
if (dp[i][j]==-1) continue;
ans = std::min(ans, dp[i][j]);
}
std::cout << (ans==inf? -1:ans) << "\n";
}
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 23568kb
input:
4 2 -2 -1 -3 2
output:
2 1 3 2
result:
wrong answer 1st lines differ - expected: '3', found: '2'