SGU 507 Treediff
这个题目 其实可以暴力 用两个 set 合并; 每次放进去一个元素只要找到这个元素第一个比他大的元素和最后一个比他小的元素;然后更新最优值;
证明为什么不会超时; 假如最后集合的小的为 S1,大的集合为S2; 这样将 S1合并到S2; 这样S2 》= S1×2;合并 t 次后 大小就成了 S1×(2^t); 但是集合大小只有N 个元素 也就是说 N 》= S1×(2^t ); 但S1等于1时 也就是说一个元素最多合并t 《= log(N) 次;
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
using namespace std; struct date{
int v,next;
}edge[]; int N,M,total,head[];
void add_edge( int u,int v ){
edge[total].v = v;
edge[total].next = head[u];
head[u] = total++;
}
inline int min(int a,int b){return a<b?a:b;}
inline int max(int a,int b){return a>b?a:b;}
set<int>s[]; int res[]; int tab[];
int dfs( int sta )
{
bool fell = false; bool fall = false; int ans = ;
for( int i = head[sta]; i != -; i = edge[i].next )
{
int v = edge[i].v; ans = min( ans,dfs( v ) ); set<int>::iterator it,now;
if( !fell ){ tab[sta] = tab[v]; res[sta] = ; fell = true; continue; }
int len1 = s[tab[sta]].size(); int len2 = s[tab[v]].size();
if( len1 > len2 ){
for( it = s[tab[v]].begin(); it != s[tab[v]].end(); it++ )
{
now = s[tab[sta]].lower_bound(*it);
if( now != s[tab[sta]].end() ){ ans = min( ans,*now-*it ); }
if( now != s[tab[sta]].begin() ){ now--;ans = min( ans,*it-*now ); }
if( s[tab[sta]].find(*it) == s[tab[sta]].end() ) s[tab[sta]].insert(*it);
}
}else {
for( it = s[tab[sta]].begin(); it != s[tab[sta]].end(); it++ )
{
now = s[tab[v]].lower_bound(*it);
if( now != s[tab[v]].end() ){ ans = min( ans,*now-*it ); }
if( now != s[tab[v]].begin() ){ ans = min( ans,*it-*(--now) ); }
if( s[tab[v]].find(*it) == s[tab[v]].end() )s[tab[v]].insert(*it);
}
tab[sta] = tab[v];
}
}
res[sta] = ans; return ans;
}
int main( )
{
while( scanf("%d%d",&N,&M) != EOF )
{
for( int i = ; i <= N; i++ )s[i].clear();
for( int i = ; i <= N; i++ )tab[i] = i;
total = ; int k = N-M+; memset( head,-,sizeof(head) );
for( int i = ; i <= N ;i++ ){
int v; scanf("%d",&v);
add_edge( v,i );
}
for( int i = ; i <= M; i++ ){
int num; scanf("%d",&num); s[k++].insert(num);
}
dfs( );
printf("%d",res[]);
for( int i = ; i <= N-M; i++ )
printf(" %d",res[i]);
puts("");
}
return ;
}
SGU 507 Treediff的更多相关文章
- SGU - 507 启发式合并维护平衡树信息
题意:给定一颗树,每个叶子节点\(u\)都有权值\(val[u]\),求每个非叶子节点子树的最小叶子距离,若该子树只有一个叶子节点,输出INF 貌似本来是一道树分治(并不会)的题目,然而可以利用平衡树 ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- 树形DP求树的重心 --SGU 134
令一个点的属性值为:去除这个点以及与这个点相连的所有边后得到的连通分量的节点数的最大值. 则树的重心定义为:一个点,这个点的属性值在所有点中是最小的. SGU 134 即要找出所有的重心,并且找出重心 ...
- SGU 170 Particles(规律题)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=170 解题报告:输入两个由'+'和'-'组成的字符串,让你判断第二个串能不能由第一个 ...
随机推荐
- 亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]
亿能测试大讲堂 - YY在线课程[ 测试人员需要掌握的Shell脚本编程 ]http://automationqa.com/forum.php?mod=viewthread&tid=2453& ...
- CWinApp类CMultiDocTemplate类CDocument类CView类的关系
转自:http://blog.csdn.net/bboot/article/details/26884011 不得不转,瞬间搞清了很多问题,短小精悍 1.CWinApp类 它包含并管理着应用程序的 ...
- LeetCode: Next Greater Element I
stack和map用好就行 public class Solution { public int[] nextGreaterElement(int[] findNums, int[] nums) { ...
- Boot-Repair&usb_repair
https://help.ubuntu.com/community/Boot-Repair https://askubuntu.com/questions/500647/unable-to-mount ...
- docker 在windows上的使用
Docker ToolBox 安装 1.首先,安装Docker ToolBox,其中包含了Docker三剑客: docker , docker-machine 和 docker-compose . 安 ...
- js 的编译
js和其他语言一样,也是需要编译的,代码编译发生在代码执行之前,js代码的编译会经历如下几个阶段 1.分词,词法分析 这个过程会将由字符串组成的字符分解成有意义的代码块,这些代码块称为词法单元,例如 ...
- mysql一次性删除所有表而不删除数据库
1.执行如下语句获取删除语句 SELECT CONCAT( 'drop table ', table_name, ';' ) from information_schema.tables where ...
- 5分钟理解Centos7防火墙firewalld
版权声明:本内容为原创内容,转载请声明出处. 原文地址:http://www.excelib.com/article/287/show firewalld简介 Centos7中默认将原来的防火墙ipt ...
- 使用Xcode IDE写node.js
最近在玩node.js 但是发现很多IDE就是用不顺手 后来发现Xcode可以剖析java script 于是试着使用Xcode来当做node.js的编辑器 首先,在Mac上必须先安装node.js的 ...
- SSH2 增删查改实例
(一)引入包 (共73个,不一定都需要,但是我的项目是这么多,经过调试,没有包冲突) (二)创建数据库表 建立数据库octtest,并创建user表,表里面一共4个字段:id,姓,名,年龄. 语句如下 ...