#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nxt, dist[maxn];
vector<vector<int> > edge;
vector<int>p;
vector<pii>adj[maxn];
ll ans;
void clr()
{
for(int i = 1; i <= nxt; i++)
adj[i].clear();
ans = 0;
nxt = 0;
memset(val, 0, sizeof val);
}
ll minimum_walk(vector<int>perm, int S)
{
clr();
s = S;
p = perm;
n = sz(p);
set<int>one;
for(int i = 0; i < n; i++)
{
if(!val[i])
{
int col = ++nxt, cur = i, cnt = 0, chk = 0;
while(!val[cur])
{
if(cur == s)
chk = 1;
cnt++;
val[cur] = col;
ans += abs(p[cur] - cur);
cur = p[cur];
}
if(cnt == 1 && !chk)
one.insert(col);
}
}
vector<int>belong[nxt + 1];
for(int i = 0; i < n; i++)
belong[val[i]].pb(i);
edge = vector<vector<int> > (nxt + 1, vector<int>(nxt + 1, mod));
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(val[i] != val[j])
edge[val[i]][val[j]] = min(edge[val[i]][val[j]], j - i);
}
}
for(int i = 1; i <= nxt; i++)
{
for(int j = 1; j <= nxt; j++)
{
if(i == j)
continue;
for(int a : belong[i])
{
for(int b : belong[j])
{
if((a < b && p[a] > b) || (a > b && p[a] < b))
edge[i][j] = 0;
}
}
}
}
for(int i = 1; i <= nxt; i++)
for(int j = i + 1; j <= nxt; j++)
{
if((one.count(i) && i != val[s]) || (one.count(j) && j != val[s]))
{
continue;
}
adj[i].pb({j, edge[i][j]});
adj[j].pb({i, edge[j][i]});
}
for(int i = 1; i <= nxt; i++)
{
for(int j = 1; j <= nxt; j++)
{
if(i == j)
continue;
/*for(pii to : adj[i])
{
cout << i << " " << to.x << " " << to.y << endl;
}*/
}
}
set<pii>se;
for(int i = 1; i <= nxt; i++)
dist[i] = mod;
se.insert({dist[val[s]] = 0, val[s]});
while(sz(se))
{
int v = se.begin()->second;
if(dist[v] != mod)
ans += dist[v] * 2;
//cout << v << " " << dist[v] << endl;
se.erase(se.begin());
for(pii to : adj[v])
{
if(dist[to.x] > to.y)
{
//cout << v << " " << to.x << " " << to.y << endl;
if(dist[to.x] != mod)
se.erase({dist[to.x], to.x});
dist[to.x] = to.y;
se.insert({dist[to.x], to.x});
}
}
}
//int add = mod;
//for(int i = 1; i <= nxt; i++)
// if(i != startcol) add = min(add, edge[i][startcol]);
//ans += add;
//cout << "Startcol: " << val[s] << endl;
return ans;
}
/*int main()
{
ios_base::sync_with_stdio(false), cin.tie(0);
vector<int>perm;
ifstream fin("in.txt");
int n, s;
fin >> n >> s;
perm.resize(n);
for(int i = 0; i < n; i++)
fin >> perm[i];
cout << minimum_walk(perm, s) << endl;
//cout << minimum_walk({3, 2, 1, 0}, 0);
return 0;
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
27776 KB |
Output is correct |
2 |
Correct |
32 ms |
27776 KB |
Output is correct |
3 |
Correct |
30 ms |
27768 KB |
Output is correct |
4 |
Correct |
28 ms |
27676 KB |
Output is correct |
5 |
Correct |
32 ms |
27768 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
28 ms |
27776 KB |
Output is correct |
8 |
Correct |
31 ms |
27776 KB |
Output is correct |
9 |
Correct |
29 ms |
27776 KB |
Output is correct |
10 |
Correct |
31 ms |
27736 KB |
Output is correct |
11 |
Correct |
30 ms |
27668 KB |
Output is correct |
12 |
Correct |
27 ms |
27648 KB |
Output is correct |
13 |
Correct |
31 ms |
27768 KB |
Output is correct |
14 |
Correct |
27 ms |
27776 KB |
Output is correct |
15 |
Correct |
30 ms |
27768 KB |
Output is correct |
16 |
Correct |
28 ms |
27776 KB |
Output is correct |
17 |
Correct |
27 ms |
27772 KB |
Output is correct |
18 |
Correct |
33 ms |
27768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
27776 KB |
Output is correct |
2 |
Correct |
32 ms |
27776 KB |
Output is correct |
3 |
Correct |
30 ms |
27768 KB |
Output is correct |
4 |
Correct |
28 ms |
27676 KB |
Output is correct |
5 |
Correct |
32 ms |
27768 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
28 ms |
27776 KB |
Output is correct |
8 |
Correct |
31 ms |
27776 KB |
Output is correct |
9 |
Correct |
29 ms |
27776 KB |
Output is correct |
10 |
Correct |
31 ms |
27736 KB |
Output is correct |
11 |
Correct |
30 ms |
27668 KB |
Output is correct |
12 |
Correct |
27 ms |
27648 KB |
Output is correct |
13 |
Correct |
31 ms |
27768 KB |
Output is correct |
14 |
Correct |
27 ms |
27776 KB |
Output is correct |
15 |
Correct |
30 ms |
27768 KB |
Output is correct |
16 |
Correct |
28 ms |
27776 KB |
Output is correct |
17 |
Correct |
27 ms |
27772 KB |
Output is correct |
18 |
Correct |
33 ms |
27768 KB |
Output is correct |
19 |
Correct |
32 ms |
27776 KB |
Output is correct |
20 |
Correct |
36 ms |
27836 KB |
Output is correct |
21 |
Correct |
46 ms |
31736 KB |
Output is correct |
22 |
Incorrect |
43 ms |
30200 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2096' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
27776 KB |
Output is correct |
2 |
Correct |
32 ms |
27776 KB |
Output is correct |
3 |
Correct |
30 ms |
27768 KB |
Output is correct |
4 |
Correct |
28 ms |
27676 KB |
Output is correct |
5 |
Correct |
32 ms |
27768 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
28 ms |
27776 KB |
Output is correct |
8 |
Correct |
31 ms |
27776 KB |
Output is correct |
9 |
Correct |
29 ms |
27776 KB |
Output is correct |
10 |
Correct |
31 ms |
27736 KB |
Output is correct |
11 |
Correct |
30 ms |
27668 KB |
Output is correct |
12 |
Correct |
27 ms |
27648 KB |
Output is correct |
13 |
Correct |
31 ms |
27768 KB |
Output is correct |
14 |
Correct |
27 ms |
27776 KB |
Output is correct |
15 |
Correct |
30 ms |
27768 KB |
Output is correct |
16 |
Correct |
28 ms |
27776 KB |
Output is correct |
17 |
Correct |
27 ms |
27772 KB |
Output is correct |
18 |
Correct |
33 ms |
27768 KB |
Output is correct |
19 |
Correct |
32 ms |
27776 KB |
Output is correct |
20 |
Correct |
36 ms |
27836 KB |
Output is correct |
21 |
Correct |
46 ms |
31736 KB |
Output is correct |
22 |
Incorrect |
43 ms |
30200 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2096' |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
30300 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3512' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
27776 KB |
Output is correct |
2 |
Correct |
32 ms |
27776 KB |
Output is correct |
3 |
Correct |
30 ms |
27768 KB |
Output is correct |
4 |
Correct |
28 ms |
27676 KB |
Output is correct |
5 |
Correct |
32 ms |
27768 KB |
Output is correct |
6 |
Correct |
27 ms |
27776 KB |
Output is correct |
7 |
Correct |
28 ms |
27776 KB |
Output is correct |
8 |
Correct |
31 ms |
27776 KB |
Output is correct |
9 |
Correct |
29 ms |
27776 KB |
Output is correct |
10 |
Correct |
31 ms |
27736 KB |
Output is correct |
11 |
Correct |
30 ms |
27668 KB |
Output is correct |
12 |
Correct |
27 ms |
27648 KB |
Output is correct |
13 |
Correct |
31 ms |
27768 KB |
Output is correct |
14 |
Correct |
27 ms |
27776 KB |
Output is correct |
15 |
Correct |
30 ms |
27768 KB |
Output is correct |
16 |
Correct |
28 ms |
27776 KB |
Output is correct |
17 |
Correct |
27 ms |
27772 KB |
Output is correct |
18 |
Correct |
33 ms |
27768 KB |
Output is correct |
19 |
Correct |
32 ms |
27776 KB |
Output is correct |
20 |
Correct |
36 ms |
27836 KB |
Output is correct |
21 |
Correct |
46 ms |
31736 KB |
Output is correct |
22 |
Incorrect |
43 ms |
30200 KB |
3rd lines differ - on the 1st token, expected: '2082', found: '2096' |
23 |
Halted |
0 ms |
0 KB |
- |