#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
long long st123(vector<int> p, int s) {
vector<int> ori = p;
int n = (int)(p.size());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (p[i] == -1 || p[i] == i) continue;
int cur = i;
vector<int> seq = {cur};
while (p[cur] != i) {
cur = p[cur];
seq.pb(cur);
}
// cerr << i << ": ";
// for (int x : seq) cerr << x << ' ';
// cerr << endl;
for (int i = 0; i < (int)(seq.size()); i++) {
ans += abs(seq[(i + 1) % (int)(seq.size())] - seq[i]);
p[seq[i]] = -1;
}
}
vector<int> bruh;
int maxi = 0;
for (int i = 0; i < n; i++) {
maxi = max(maxi, ori[i]);
if (maxi == i) bruh.pb(i);
}
// for (int x : bruh) cerr << x << ' ';
// cerr << endl;
while (bruh.size() >= 2) {
int x = bruh.size();
if (bruh[x - 1] - bruh[x - 2] == 1) {
bruh.pop_back();
} else break;
}
ans += ((int)(bruh.size()) - 1) * 2;
return ans;
}
long long minimum_walk(vector<int> p, int s) {
// if (s == 0) return st123(p, s);
vector<int> ori = p;
int n = (int)(p.size());
ll ans = 0;
vector<pii> range(n);
for (int i = 0; i < n; i++) {
if (p[i] == -1) continue;
int cur = i;
vector<int> seq = {cur};
while (p[cur] != i) {
cur = p[cur];
seq.pb(cur);
}
pii get = {i, i};
for (int x : seq) {
get.ff = min(get.ff, x);
get.ss = max(get.ss, x);
}
for (int j = 0; j < (int)(seq.size()); j++) {
ans += abs(seq[(j + 1) % (int)(seq.size())] - seq[j]);
range[seq[j]] = get;
p[seq[j]] = -1;
}
}
vector<vector<int>> dp(n);
for (int i = 0; i < n; i++) dp[i].resize(n, 1e9);
dp[range[s].ff][range[s].ss] = 0;
queue<pii> q;
q.push(range[s]);
while (!q.empty()) {
pii cur = q.front(); q.pop();
if (cur.ff > 0) {
pii nrange = {range[cur.ff - 1].ff, max(cur.ss, range[cur.ff - 1].ss)};
if (dp[nrange.ff][nrange.ss] > dp[cur.ff][cur.ss] + 1) {
dp[nrange.ff][nrange.ss] = dp[cur.ff][cur.ss] + 1;
q.push(nrange);
}
}
if (cur.ss < n - 1) {
pii nrange = {min(cur.ff, range[cur.ss + 1].ff), range[cur.ss + 1].ss};
if (dp[nrange.ff][nrange.ss] > dp[cur.ff][cur.ss] + 1) {
dp[nrange.ff][nrange.ss] = dp[cur.ff][cur.ss] + 1;
q.push(nrange);
}
}
}
ans += dp[0][n - 1] * 2;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4188 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3314' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |