当前位置:网站首页>洛谷P3521 ROT-Tree Rotations
洛谷P3521 ROT-Tree Rotations
2022-08-07 04:53:00 【CLH_W】
题目描述
给定一颗有 nn 个叶节点的二叉树。每个叶节点都有一个权值 p_ip i
(注意,根不是叶节点),所有叶节点的权值构成了一个 1 \sim n1∼n 的排列。
对于这棵二叉树的任何一个结点,保证其要么是叶节点,要么左右两个孩子都存在。
现在你可以任选一些节点,交换这些节点的左右子树。
在最终的树上,按照先序遍历遍历整棵树并依次写下遇到的叶结点的权值构成一个长度为 nn 的排列,你需要最小化这个排列的逆序对数。
输入格式
第一行是一个整数 nn,表示树的叶节点个数。
接下来若干行,使用递归的形式来读入这棵树,读入任意一个子树的信息时,初始时读入其根节点。对于一个结点 uu,首先有一行一个整数 xx。
若 x \neq 0x
=0,则表示 uu 是一个叶节点,其权值为 xx。
若 x = 0x=0,则表示 uu 不是叶节点,则接下来若干行读入其左子树信息,左子树读入结束后接下来若干行读入其右子树信息。
输出格式
输出一行一个整数表示最小的逆序对数。
输入输出样例
输入 #1复制
3
0
0
3
1
2
输出 #1复制
1
说明/提示
样例 1 解释
下图中,左图是初始读入的树,右图是操作后的树。
数据规模与约定
对于 30%30% 的数据,保证 n \leq 5 \times 10^3n≤5×10
3
。
对于 100%100% 的数据,保证 2 \leq n \leq 2 \times 10^52≤n≤2×10
5
, 0 \leq x \leq n0≤x≤n,所有叶节点的权值是一个 1 \sim n1∼n 的排列。
提示
请注意,nn 不是树的结点个数。
上代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<map>
#include<climits>
#include<algorithm>
using namespace std;
typedef int mainint;
typedef double db;
typedef long long ll;
#define il inline
#define pii pair<ll,int>
#define mp make_pair
#define B cout << "breakpoint" << endl;
#define O(x) cout << #x << " " << x << endl;
inline int read()
{
int ans = 0,op = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') op = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
(ans *= 10) += ch - '0';
ch = getchar();
}
return ans * op;
}
const int maxn = 5e5 + 5;
int ls[maxn],rs[maxn],tot;
int sum[maxn];
int n;
ll ans1,ans2,ans;
int st[maxn],top;
il void trash(int i)
{
ls[i] = rs[i] = sum[i] = 0; st[++top] = i;
}
il void up(int i)
{
sum[i] = sum[ls[i]] + sum[rs[i]];
}
void update(int &i,int l,int r,int x,int d)
{
if(!i) i = !top ? ++tot : st[top--];
if(l == r && l == x)
{
sum[i] += d;
return;
}
int mid = l + r >> 1;
if(x <= mid) update(ls[i],l,mid,x,d);
else update(rs[i],mid + 1,r,x,d);
up(i);
}
void merge(int &lx,int rx)
{
if(!lx || !rx)
{
lx = lx + rx;
//if(rx) trash(rx);
return;
}
sum[lx] = sum[lx] + sum[rx];
ans1 += 1ll * sum[rs[lx]] * sum[ls[rx]];
ans2 += 1ll * sum[ls[lx]] * sum[rs[rx]];
merge(ls[lx],ls[rx]);
merge(rs[lx],rs[rx]);
trash(rx);
}
void solve(int &x)
{
int t = read();
int leftson,rightson;
x = 0;
if(t)
update(x,1,n,t,1);
else
{
solve(leftson); solve(rightson);
ans1 = ans2 = 0;
x = leftson;
merge(x,rightson);
ans += min(ans1,ans2);
}
}
int main()
{
n = read();
int x = 0;
solve(x);
cout << ans;
}
边栏推荐
- Find My资讯|AirTag 正在帮更多人找到丢失的行李,Find My用处越来越大
- Seq2Seq superficial understanding
- Find My Information | AirTag is helping more people find their lost luggage, and Find My is becoming more and more useful
- golang server proxy
- [LeetCode Daily Question] - 34. Find the first and last position of an element in a sorted array
- tiup cluster display
- 网线Cable
- typescript88-任务案例
- Golang Break、Continue跳出多层循环
- typescript80-使用配置文件编译文件
猜你喜欢

Backtracking and its Simple Question Example

小 P 周刊 Vol.14
![[LeetCode Daily Question] - 153. Find the minimum value in a rotated sorted array](/img/dd/31fc44ea63b852f2677766bb883522.png)
[LeetCode Daily Question] - 153. Find the minimum value in a rotated sorted array

2022牛客多校六 B-Eezie and Pie (dfs)

线性代数学习笔记4-1:线性方程组的数学和几何意义、零空间/解空间/核

position sticky与overflow冲突失效无作用,解决办法

2022-08-06:给定一个数组arr,长度为N,arr中所有的值都在1~K范围上, 你可以删除数字,目的是让arr的最长递增子序列长度小于K。 返回至少删除几个数字能达到目的。 N <= 10^4

刷题《剑指Offer》day11

量化风控的规则开发,如何更好做策略定规则,抓坏人

MySQL advanced 1 - underlying data structure B+ tree
随机推荐
线性代数学习笔记5-2:(正交的)投影、投影矩阵、A^T A、最小二乘法LS
用C语言实现简单得通讯录
【愚公系列】2022年08月 Go教学课程 031-结构体方法
Automatic water quality monitoring and video monitoring to consolidate and improve the level of drinking water safety
navicat linked server mysql
Reading Notes - RetinaFace: Single-stage Dense Face Localisation in the Wild
golang server proxy
npm install encountered Unexpected end of JSON input while parsing near '...onnect": "^2.0.0", "gru" problem solved
Golang scope pit
《国际学术论文写作与发表》参考答案
Golang Break, Continue out of multi-layer cycle
LeetCode 二叉树的遍历
字符脱敏工具
【Yu Niangniang】1373. Maximum key value and DFS of binary search subtree
2022 Niu Ke Duo School Six B-Eezie and Pie (dfs)
【毕业设计】基于STM32的自动加油站加油系统 -物联网 单片机 嵌入式
[Shader realizes the overall distortion effect of Distortion_Shader Effect Chapter 17]
Find My Information | AirTag is helping more people find their lost luggage, and Find My is becoming more and more useful
4G dtu remote wireless meter reading
从简历被拒到收割8个大厂offer,我用了3个月成功破茧成蝶