[是男人就过8题——Pony.ai]Perfect N-P Arrays
[是男人就过8题——Pony.ai]Perfect N-P Arrays
题目大意:
一棵\(n(\sum n\le5\times10^6)\)个结点的树,每个结点都有一个括号。求树上一个合法的括号序列使得若将'('当成\(1\),')'当成\(-1\)。该序列最大前缀和最大,输出最大前缀和。
思路:
由于括号序列前缀和是连续的,所以我们可以把括号序列任意时刻前缀和\(\ge 0\)的限制给去掉。两遍树形DP求出从一个点出发最大/最小前缀和。绝对值取\(\min\)后即为经过这个点的最大答案。注意到最大/最小前缀和可能来自于同一棵子树,因此我们还需记录次大/次小值。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
const int N=1e6+1;
std::vector<int> e[N];
int w[N],ans,max[N][2],min[N][2];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
inline void upd1(const int &x,const int &y) {
int tmp1=min[y][0]+w[x];
int tmp2=max[y][0]+w[x];
for(register int i=0;i<2;i++) {
if(tmp1<min[x][i]) {
std::swap(tmp1,min[x][i]);
}
if(tmp2>max[x][i]) {
std::swap(tmp2,max[x][i]);
}
}
}
inline void upd2(const int &x,const int &y) {
int tmp1=min[y][min[y][0]==min[x][0]+w[y]];
int tmp2=max[y][max[y][0]==max[x][0]+w[y]];
if(tmp1!=INT_MAX) tmp1+=w[x];
if(tmp2!=INT_MIN) tmp2+=w[x];
for(register int i=0;i<2;i++) {
if(tmp1!=INT_MAX&&tmp1<min[x][i]) {
std::swap(tmp1,min[x][i]);
}
if(tmp2!=INT_MIN&&tmp2>max[x][i]) {
std::swap(tmp2,max[x][i]);
}
}
}
void dfs1(const int &x,const int &par) {
max[x][0]=min[x][0]=w[x];
max[x][1]=INT_MIN;
min[x][1]=INT_MAX;
for(auto &y:e[x]) {
if(y==par) continue;
dfs1(y,x);
upd1(x,y);
}
}
void dfs2(const int &x,const int &par) {
for(auto &y:e[x]) {
if(y==par) continue;
ans=std::max(ans,std::min(std::abs(max[y][0]),std::abs(min[x][min[x][0]==min[y][0]+w[x]])));
ans=std::max(ans,std::min(std::abs(min[y][0]),std::abs(max[x][max[x][0]==max[y][0]+w[x]])));
upd2(y,x);
dfs2(y,x);
}
}
int main() {
int n;
while(~scanf("%d",&n)) {
for(register int i=1;i<=n;i++) {
const int p=getint();
if(p) add_edge(p,i);
w[i]=getint();
}
dfs1(1,0);
dfs2(1,0);
printf("%d\n",ans);
for(register int i=1;i<=n;i++) {
e[i].clear();
}
ans=0;
}
return 0;
}
[是男人就过8题——Pony.ai]Perfect N-P Arrays的更多相关文章
- 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数
[题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...
- 是男人就过 8 题--Pony.AI A AStringGame
链接:https://www.nowcoder.com/acm/contest/92/A来源:牛客网 AStringGame 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 26214 ...
- 是男人就过八题A_A String Game题解
题意 给一个字符串\(s\),和\(n\)个子串\(t[i]\),两个人博弈,每次取出一个串\(t[i]\),在后面加入一个字符,保证新字符串仍然是\(s\)的子串,无法操作的人输. 分析 n个子串, ...
- leetcode第四题:Median of Two Sorted Arrays (java)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)
题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...
- 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the tw ...
- 算法题之Median of Two Sorted Arrays
这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1 ...
- 刷题4. Median of Two Sorted Arrays
一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...
随机推荐
- JS学习笔记Day12
一.浏览器的默认行为以及阻止行为 (一)右键菜单事件:oncontextmenu: 阻止:return false: (二)超链接的默认行为:跳转: 阻止:标准浏览器:event.preventDef ...
- IaaS,PaaS,SaaS 的区别(转)
越来越多的软件,开始采用云服务. 云服务只是一个统称,可以分成三大类. IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a- ...
- LFYZ-OJ ID: 1019 位数问题
位数问题 问题描述 在所有的N位数中,有多少个数中有偶数个数字3?由于结果可能很大,你只需要输出这个答案对12345取余的值. INPUT 输入一个数N(1<=N<=1000),输入以0结 ...
- 第三节:总结.Net下后端的几种请求方式(WebClient、WebRequest、HttpClient)
一. 前言 前端调用有Form表单提交,ajax提交,ajax一般是用Jquery的简化写法,在这里不再过多介绍: 后端调用大约有这些:WebCient.WebRequest.Httpclient.W ...
- MySQL学习8 - 数据的增删改
一.介绍 二.插入数据 INSERT 三.更新数据UPDATE 四.删除数据DELETE 一.介绍 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现 ...
- webpack 代码优化压缩方法
在配置基于webpack的react单页面脚手架时,公共依赖库代码打包至vender.js中,页面逻辑代码打包至app.js中,使用webpack-bundle-analyzer分析发现,两个js中包 ...
- tangent space与object space
3d渲染每个网格(Mesh)的面都可配一个材质(Material),要想在一个面上显示出更多的细节,除了模型做的更精致,还可以使用法线贴图(Normal Texture). 法线向量(Normal V ...
- SQL server 存储过程中 列传行
select @exchange=exchange,@coupons_type=coupons_type - FLOOR(exchange))) from points_exchange_svc wh ...
- 【转】Python3 (入门6) 库的打包与安装
Python3 (入门6) 库的打包与安装 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog.csdn.net/Rozol/article/details/6940288 ...
- Lua中的一些库(1)
[数学库] 数学库(math)由一组标准的数学函数构成.这里主要介绍几个常用的函数,其它的大家可以自行百度解决. 三角函数(sin,cos,tan……)所有的三角函数都使用弧度单位,可以用函数deg( ...