#include <bits/stdc++.h>
#include "books.h"
#warning That's not FB, that's my FB
using namespace std;
using ll = long long;
int inf = 1e9;
int n, s;
int p[1000005], pos[1000005];
vector<vector<int>> cyc;
vector<int> cur;
bool viz[1000005];
void dfs(int nod)
{
viz[nod] = true;
cur.push_back(nod);
if (!viz[p[nod]])
dfs(p[nod]);
}
int vmin[1000005], vmax[1000005];
int rmq_min[21][1000005], rmq_max[21][1000005];
int lg[1000005];
void build()
{
for (int i = 2; i <= n; i++)
lg[i] = 1 + lg[i / 2];
for (int i = 1; i <= n; i++)
{
rmq_min[0][i] = vmin[i];
rmq_max[0][i] = vmax[i];
}
for (int j = 1; j <= lg[n]; j++)
{
for (int i = 1; i <= n - (1 << j) + 1; i++)
{
rmq_min[j][i] = min(rmq_min[j - 1][i], rmq_min[j - 1][i + (1 << (j - 1))]);
rmq_max[j][i] = max(rmq_max[j - 1][i], rmq_max[j - 1][i + (1 << (j - 1))]);
}
}
}
int qmin(int l, int r)
{
int lgg = lg[r - l + 1];
return min(rmq_min[lgg][l], rmq_min[lgg][r - (1 << lgg) + 1]);
}
int qmax(int l, int r)
{
int lgg = lg[r - l + 1];
return max(rmq_max[lgg][l], rmq_max[lgg][r - (1 << lgg) + 1]);
}
pair<int,int> query(int l, int r)
{
pair<int,int> uf = {qmin(l,r),qmax(l,r)};
return uf;
}
pair<int,int> f(pair<int,int> pos)
{
while (true)
{
pair<int,int> repos = query(pos.first,pos.second);
if (repos.first == pos.first and repos.second == pos.second)
break;
pos = repos;
}
return pos;
}
int get_cl(pair<int,int> pos)
{
int cst = 0;
int inip = pos.second;
while (pos.second == inip and pos.first != 1)
{
cst++;
pos.first--;
pos = f(pos);
}
if (pos.second == inip)
return inf;
return cst;
}
int get_cr(pair<int,int> pos)
{
int cst = 0;
int inip = pos.first;
while (pos.first == inip and pos.second != n)
{
cst++;
pos.second++;
pos = f(pos);
}
if (pos.first == inip)
return inf;
return cst;
}
ll minimum_walk(vector<int> PERM, int STR)
{
cyc.clear();
memset(viz,0,sizeof(viz));
n = PERM.size();
for (int i = 1; i <= n; i++)
p[i] = PERM[i - 1] + 1, pos[p[i]] = i;
s = STR + 1;
for (int i = 1; i <= n; i++)
{
if (!viz[i])
{
cur.clear();
dfs(i);
cyc.push_back(cur);
}
}
for (auto it : cyc)
{
sort(it.begin(), it.end());
for (auto itt : it)
vmin[itt] = it[0], vmax[itt] = it.back();
}
build();
ll ans = 0;
for (int i = 1; i <= n; i++)
ans += abs(i - p[i]);
int pzz = 1;
while (pzz < s and p[pzz] == pzz)
ans -= 2, pzz++;
pzz = n;
while (pzz > s and p[pzz] == pzz)
ans -= 2, pzz--;
pair<int,int> pos = f({s,s});
while (pos.first != 1 or pos.second != n)
{
int cl = get_cl(pos), cr = get_cr(pos);
if (cl == inf)
{
while (pos.first != 1)
{
ans += 2;
pos.first--;
pos = f(pos);
}
while (pos.second != n)
{
ans += 2;
pos.second++;
pos = f(pos);
}
}
else
{
if (cl <= cr)
{
for (int i = 1; i <= cl; i++)
{
ans += 2;
pos.first--;
pos = f(pos);
}
}
else
{
for (int i = 1; i <= cr; i++)
{
ans += 2;
pos.second++;
pos = f(pos);
}
}
}
}
return ans;
}
/**
doamne cate try-uri pana sa ajung la greedy-ul corect
daca faceam asta la cf era atat de over
In fine, eu mereu domin un interval [L R] cu prop ca f(L, R) = (L, R)
f(L, R) = (L, R) daca nu exista vreun i in (L R) care sa faca parte dintr-un ciclu care contine un j inn afara (L R)
Altfel f(L, R) = f(intv reunit cu intervalul ciclului aluia kind of stuff), idk doar un aint de minime si maxime
Acum, din (L, R) tu poti plati 2 bani ca sa faci artificial L-- sau R++
Fie cL = nr minim de mutari pe stanga astfel incat daca le faci, te extinzi pana la urma si in dreapta
cR = nr minim de mutari in dreapta astfel incat pana la urma te extinzi in stanga
Proof penal => e optim sa faci mutari in directia cu costul minim (adica faci min(cL, cR) mutari intr-o directie si vezi unde dai)
Ma rog, daca cL = cR = inf (nu poate fi doar una inf si una nu, ofc), spamezi mutari in ambele parti pana ajungi la capat
In fine, la final scazi 2 * (marimea sufixului de p[i] = i + marimea prefixului de p[i] = i)
Kinda se reduce la a gasi cL si cR eficient... chestia funny e ca nu trebuie vreo structura complicata
Gen, daca doar simulezi, oricum mereu te extinzi cu 1 intr-o parte, deci daca faci O(k) query-uri de aint, se va extinde (L, R) la final O(k)
Deci tot O(NlogN) iese sau cv de genul
Ce boti oamenii ca au bagat asa putini 50 -> 100
**/
Compilation message
books.cpp:3:2: warning: #warning That's not FB, that's my FB [-Wcpp]
3 | #warning That's not FB, that's my FB
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1624 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1624 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
1624 KB |
Output is correct |
26 |
Correct |
1 ms |
1628 KB |
Output is correct |
27 |
Correct |
1 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1624 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1624 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
1624 KB |
Output is correct |
26 |
Correct |
1 ms |
1628 KB |
Output is correct |
27 |
Correct |
1 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
277 ms |
216040 KB |
Output is correct |
31 |
Correct |
254 ms |
202488 KB |
Output is correct |
32 |
Correct |
250 ms |
238692 KB |
Output is correct |
33 |
Correct |
230 ms |
221896 KB |
Output is correct |
34 |
Correct |
240 ms |
221900 KB |
Output is correct |
35 |
Correct |
221 ms |
214220 KB |
Output is correct |
36 |
Correct |
195 ms |
199452 KB |
Output is correct |
37 |
Correct |
196 ms |
189188 KB |
Output is correct |
38 |
Correct |
237 ms |
188272 KB |
Output is correct |
39 |
Correct |
197 ms |
188240 KB |
Output is correct |
40 |
Correct |
269 ms |
189236 KB |
Output is correct |
41 |
Correct |
248 ms |
200604 KB |
Output is correct |
42 |
Correct |
269 ms |
193804 KB |
Output is correct |
43 |
Correct |
228 ms |
224508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1732 KB |
Output is correct |
3 |
Correct |
1 ms |
1624 KB |
Output is correct |
4 |
Correct |
1 ms |
1724 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
1 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
1 ms |
1628 KB |
Output is correct |
11 |
Correct |
1 ms |
1628 KB |
Output is correct |
12 |
Correct |
1 ms |
1624 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1668 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
1 ms |
1628 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1476 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1368 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1624 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1624 KB |
Output is correct |
23 |
Correct |
1 ms |
1628 KB |
Output is correct |
24 |
Correct |
1 ms |
1628 KB |
Output is correct |
25 |
Correct |
1 ms |
1624 KB |
Output is correct |
26 |
Correct |
1 ms |
1628 KB |
Output is correct |
27 |
Correct |
1 ms |
1628 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
2 ms |
1628 KB |
Output is correct |
30 |
Correct |
277 ms |
216040 KB |
Output is correct |
31 |
Correct |
254 ms |
202488 KB |
Output is correct |
32 |
Correct |
250 ms |
238692 KB |
Output is correct |
33 |
Correct |
230 ms |
221896 KB |
Output is correct |
34 |
Correct |
240 ms |
221900 KB |
Output is correct |
35 |
Correct |
221 ms |
214220 KB |
Output is correct |
36 |
Correct |
195 ms |
199452 KB |
Output is correct |
37 |
Correct |
196 ms |
189188 KB |
Output is correct |
38 |
Correct |
237 ms |
188272 KB |
Output is correct |
39 |
Correct |
197 ms |
188240 KB |
Output is correct |
40 |
Correct |
269 ms |
189236 KB |
Output is correct |
41 |
Correct |
248 ms |
200604 KB |
Output is correct |
42 |
Correct |
269 ms |
193804 KB |
Output is correct |
43 |
Correct |
228 ms |
224508 KB |
Output is correct |
44 |
Correct |
1 ms |
1628 KB |
Output is correct |
45 |
Correct |
1 ms |
1732 KB |
Output is correct |
46 |
Correct |
1 ms |
1624 KB |
Output is correct |
47 |
Correct |
1 ms |
1724 KB |
Output is correct |
48 |
Correct |
1 ms |
1628 KB |
Output is correct |
49 |
Correct |
1 ms |
1628 KB |
Output is correct |
50 |
Correct |
1 ms |
1628 KB |
Output is correct |
51 |
Correct |
1 ms |
1628 KB |
Output is correct |
52 |
Correct |
1 ms |
1628 KB |
Output is correct |
53 |
Correct |
1 ms |
1628 KB |
Output is correct |
54 |
Correct |
1 ms |
1628 KB |
Output is correct |
55 |
Correct |
1 ms |
1624 KB |
Output is correct |
56 |
Correct |
2 ms |
1628 KB |
Output is correct |
57 |
Correct |
1 ms |
1668 KB |
Output is correct |
58 |
Correct |
1 ms |
1628 KB |
Output is correct |
59 |
Correct |
1 ms |
1628 KB |
Output is correct |
60 |
Correct |
1 ms |
1628 KB |
Output is correct |
61 |
Correct |
1 ms |
1628 KB |
Output is correct |
62 |
Correct |
1 ms |
1628 KB |
Output is correct |
63 |
Correct |
1 ms |
1628 KB |
Output is correct |
64 |
Correct |
207 ms |
224048 KB |
Output is correct |
65 |
Correct |
215 ms |
228096 KB |
Output is correct |
66 |
Correct |
231 ms |
189192 KB |
Output is correct |
67 |
Correct |
211 ms |
188508 KB |
Output is correct |
68 |
Correct |
19 ms |
18964 KB |
Output is correct |
69 |
Correct |
17 ms |
18288 KB |
Output is correct |
70 |
Correct |
19 ms |
19216 KB |
Output is correct |
71 |
Correct |
20 ms |
21000 KB |
Output is correct |
72 |
Correct |
22 ms |
22016 KB |
Output is correct |
73 |
Correct |
24 ms |
18072 KB |
Output is correct |
74 |
Correct |
219 ms |
221748 KB |
Output is correct |
75 |
Correct |
223 ms |
221812 KB |
Output is correct |
76 |
Correct |
258 ms |
223944 KB |
Output is correct |
77 |
Correct |
212 ms |
224372 KB |
Output is correct |
78 |
Correct |
206 ms |
218568 KB |
Output is correct |
79 |
Correct |
240 ms |
218596 KB |
Output is correct |
80 |
Correct |
243 ms |
238796 KB |
Output is correct |
81 |
Correct |
207 ms |
211224 KB |
Output is correct |
82 |
Correct |
206 ms |
211172 KB |
Output is correct |
83 |
Correct |
216 ms |
205036 KB |
Output is correct |
84 |
Correct |
230 ms |
201248 KB |
Output is correct |
85 |
Correct |
184 ms |
193700 KB |
Output is correct |
86 |
Correct |
210 ms |
189960 KB |
Output is correct |
87 |
Correct |
238 ms |
230132 KB |
Output is correct |
88 |
Correct |
232 ms |
222412 KB |
Output is correct |
89 |
Correct |
206 ms |
209044 KB |
Output is correct |
90 |
Correct |
235 ms |
188684 KB |
Output is correct |
91 |
Correct |
189 ms |
188240 KB |
Output is correct |
92 |
Correct |
212 ms |
188240 KB |
Output is correct |