//Power Of Ninja Go
#include <bits/stdc++.h>
#ifdef atom
#include "grader.cpp"
#else
#include "books.h"
#endif
using namespace std;
typedef long long ll; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector< ii > vii;
#define X first
#define Y second
#define pb push_back
int n;
const int maxn = 1e6+5;
vector<int> p;
int a[maxn], b[maxn];
bool vis[maxn];
int cnt = 0;
void dfs(int u)
{
if(vis[u]) return;
vis[u] = true;
a[cnt] = min(u, a[cnt]);
b[cnt] = max(u, b[cnt]);
dfs(p[u]);
}
long long minimum_walk(std::vector<int> P, int s)
{
p = P;
n = P.size();
for(int i = 0; i< n; i++) a[i] = 1e9;
for(int i = 0; i< n; i++)
{
if(vis[i]) continue;
dfs(i); cnt++;
}
int ed = 0;
ll res = 0;
for(int i = 0; i< cnt; i++)
{
if(a[i] == b[i]) continue;
//cout << a[i] << " " << b[i] << endl;
if(ed< a[i])
{
res += 2*(a[i]-ed);
ed = b[i];
}
res += 2*(b[i]-a[i]);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
428 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
428 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
428 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
540 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4078' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Incorrect |
2 ms |
428 KB |
3rd lines differ - on the 1st token, expected: '8', found: '6' |
4 |
Halted |
0 ms |
0 KB |
- |