QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#770485 | #7866. Teleportation | gzy | WA | 3ms | 15444kb | C++14 | 1.8kb | 2024-11-21 22:04:08 | 2024-11-21 22:04:08 |
Judging History
answer
#include<bits/stdc++.h>
#define pb emplace_back
#define pi pair<int,int>
#define fi first
#define se second
#define mp make_pair
using namespace std;
using ll = long long;
using db = double;
constexpr int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc() getchar()//(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? EOF : *p1++)
int rd() {int x = 0, f = 1;char c = gc();while (!isdigit(c)) {if (c == '-') f = -1;c = gc();}while (isdigit(c)) x = x * 10 + (c ^ 48), c = gc();return x * f;}
const int N = 2e5 + 7;
int i, j, k, n, m, a[N];
int l[N], r[N], f[N], v[N], vis[N];
pi c[N];
priority_queue<pi> q;
vector<int> d[N];
int fl(int x) {return x == l[x] ? x : l[x] = fl(l[x]);}
int fr(int x) {return x == r[x] ? x : r[x] = fr(r[x]);}
int dis(int x, int y) {return y - x + (x > y) * n;}
int val(int u, int x) {return dis(v[u], x) + f[x] + 1;}
void add(int x) {
int L = fl((x-1+n)%n), R = fr((x+1)%n);
for(int u : d[L]) if(val(u, x) < f[u]) q.push({-(f[u] = val(u, x)), u});
if(L != R) for(int u : d[R]) if(val(u, x) < f[u]) q.push({-(f[u] = val(u, x)), u});
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
n = rd(), k = rd();
for(i = 0; i < n; i++) a[i] = rd();
for(i = 0; i < n; i++) c[i] = {v[i] = (i + a[i]) % n, i};
sort(c, c + n);
for(i = 0; i < n; i++) d[i].pb(c[i].se);
for(i = 0; i < n; i++) l[i] = d[i].size() ? i : (i-1+n)%n;
for(i = 0; i < n; i++) r[i] = d[i].size() ? i : (i+1)%n;
memset(f, 0x3f, sizeof f);
f[k] = 0, add(k);
while(q.size()) {
pi t = q.top(); q.pop();
int x = t.se;
if(vis[x]) continue;
vis[x] = 1;
if(!x) return printf("%d\n", f[0]),0;
add(x);
}
printf("%d\n", f[0]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15424kb
input:
4 3 0 1 2 3
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15268kb
input:
4 3 0 0 0 0
output:
4
result:
ok 1 number(s): "4"
Test #3:
score: 0
Accepted
time: 3ms
memory: 14108kb
input:
4 3 2 2 2 2
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 3ms
memory: 13800kb
input:
2 1 0 0
output:
2
result:
ok 1 number(s): "2"
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 15444kb
input:
2 1 1 1
output:
1061109567
result:
wrong answer 1st numbers differ - expected: '1', found: '1061109567'