// ~ Be Name Khoda ~ //
#include "books.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 1e6 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
ll ans = 0;
int per[maxn5], req[maxn5], cmp[maxn5], ps[maxn5];
int num = 0, av[maxn5], cnt = 0;
bool mark[maxn5];
void dfs(int v){
mark[v] = true;
req[min(v, per[v])]++;
req[max(v, per[v])]--;
ans += abs(v - per[v]);
cnt++;
cmp[v] = num;
if(!mark[per[v]])
dfs(per[v]);
}
int get(int l, int r){
if(l > r)
return 0;
return ps[r] - (l ? ps[l - 1] : 0);
}
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
int mx = 0, mn = n;
for(int i = 0; i < n; i++){
per[i] = p[i];
if(i != per[i]){
mn = min(mn, i);
mx = max(mx, i);
}
}
if(mn == n)
return 0;
int good = 0;
fill(av, av + n + 5, n + 10);
for(int i = 0; i < n; i++) if(!mark[i]){
cnt = 0;
dfs(i);
if(cnt > 1)
good++;
num++;
}
int emp = 0, cur = 0;
for(int i = 0; i < n; i++){
cur += req[i];
if(i)
ps[i] = ps[i - 1];
if(!cur)
ps[i]++;
}
int add = 2 * n;
//for(int i = 0; i < n; i++) if(i != p[i])
// add = min(add, abs(i - s) * 2);
if(mn > s || mx < s){
assert(false);
add = min(abs(mx - s), abs(mn - s)) * 2;
}
//cout << s << ' ' << mx << endl;
return ans + 2 * get(mn, mx - 1) + add;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:71:6: warning: unused variable 'emp' [-Wunused-variable]
71 | int emp = 0, cur = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |