Link
BZOJ - https://www.lydsy.com/JudgeOnline/problem.php?id=3244
Luogu - https://www.luogu.org/problemnew/show/P1232
在树上标是不好的
最好在bfs序上搞 而且bfs序上点是按深度从小到大+编号从小到大排序好的
就很舒服
我说完了 开始你的表演.jpg
萨比水题 毁我人参
(总觉得是我萨比啊
Luogu
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
#define R register
const int MAXN = 2e5 + 5;
double Ans = 1;
int n, Dfs[MAXN], Bfs[MAXN], Gets[MAXN];
double Mar[MAXN], Sum[MAXN];
int Dep[MAXN];
int main()
{
scanf("%d", &n);
for (R int i = 1; i <= n; ++i)
{
scanf("%d", &Dfs[i]);
}
for (R int t, i = 1; i <= n; ++i)
{
scanf("%d", &t);
Bfs[t] = i;
}
for (R int i = 1; i <= n; ++i)
{
Dfs[i] = Bfs[Dfs[i]];
Gets[Dfs[i]] = i;
}
Sum[1] = Mar[1] = 1;
for (R int i = 2; i < n; ++i)
{
if (Gets[i] > Gets[i + 1])
{
Mar[i] = 1;
}
Sum[i] = Sum[i-1] + Mar[i];
}
for (R int x, y, i = 2; i <= n; ++i)
{
x = Dfs[i-1], y = Dfs[i];
if (x < y) if (Sum[x-1] < Sum[y-1]) ++Dep[x], --Dep[y];
}
for (R int Cur = 0, i = 1; i < n; ++i)
{
Cur += Dep[i];
if (!Cur && !Mar[i]) Mar[i] = 0.5;
Ans += Mar[i];
}
printf("%.3f", Ans + Mar[n]);
return 0;
}
BZOJ
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
#define R register
const int MAXN = 2e5 + 5;
double Ans = 1;
int n, Dfs[MAXN], Bfs[MAXN], Gets[MAXN];
double Mar[MAXN], Sum[MAXN];
int Dep[MAXN];
int main()
{
scanf("%d", &n);
for (R int i = 1; i <= n; ++i)
{
scanf("%d", &Dfs[i]);
}
for (R int t, i = 1; i <= n; ++i)
{
scanf("%d", &t);
Bfs[t] = i;
}
for (R int i = 1; i <= n; ++i)
{
Dfs[i] = Bfs[Dfs[i]];
Gets[Dfs[i]] = i;
}
Sum[1] = Mar[1] = 1;
for (R int i = 2; i < n; ++i)
{
if (Gets[i] > Gets[i + 1])
{
Mar[i] = 1;
}
Sum[i] = Sum[i-1] + Mar[i];
}
for (R int x, y, i = 2; i <= n; ++i)
{
x = Dfs[i-1], y = Dfs[i];
if (x < y) if (Sum[x-1] < Sum[y-1]) ++Dep[x], --Dep[y];
}
for (R int Cur = 0, i = 1; i < n; ++i)
{
Cur += Dep[i];
if (!Cur && !Mar[i]) Mar[i] = 0.5;
Ans += Mar[i];
}
Ans += Mar[n];
printf("%.3f %.3f %.3f", Ans-0.001, Ans, Ans+0.001);
return 0;
}