📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 2907| 回复: 8
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : 构建特殊堆

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
A rooted binary tree with keys in its nodes has the binary search tree
property (BST property) if, for every node, the keys in its left
subtree are smaller than its own key, and the keys in its right
subtree are larger than its own key. It has the heap property if, for
every node, the keys of its children are all smaller than its own key.
You are given a set of n binary tree nodes that each contain an
integer i and an integer j. No two i values are equal and no two j
values are equal. We must assemble the nodes into a single binary tree
where the i values obey the BST property and the j values obey the
heap property. If you pay attention only to the second key in each
node, the tree looks like a heap, and if you pay attention only to the
first key in each node, it looks like a binary search tree.Describe a
recursive algorithm for assembling such a tree

上一篇:Microsoft : find anagram pairs
下一篇:Google : 就地寻找两数组中第k大的数
🔗
Narashy 2011-6-18 07:44:37 | 只看该作者
全局:
标准的treap
测试题: http://poj.org/problem?id=2201
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-20 19:55:03 | 只看该作者
全局:
标准的treap
测试题:
Narashy 发表于 2011-6-18 07:44


Are you ACMer??
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-20 19:56:16 | 只看该作者
全局:
和以前的一个根据中序和前续遍历构建二叉树的题目一样的解法:

struct TREE_NODE
{
        int nLVal;
        int nRVal;
        TREE_NODE* pLft;
        TREE_NODE* pRgt;
};

bool SortLftVal(const TREE_NODE& node1, const TREE_NODE& node2)
{
        return node1.nLVal < node2.nLVal;
}

//Already sorted by the first value
TREE_NODE* _inner_construct(TREE_NODE nodes[], int n)
{
        assert(nodes);

        if (0 == n) return NULL;

        //find the corresponding index of node with second value the biggest
        int nIndex = 0;
        for (int i = 0; i < n; i++)
        {
                if (nodes[i].nRVal > nodes[nIndex].nRVal)
                        nIndex = i;
        }

        nodes[nIndex].pLft = _inner_construct(nodes, nIndex);
        nodes[nIndex].pRgt = _inner_construct(nodes+nIndex+1, n-nIndex-1);

        return &nodes[nIndex];
}

TREE_NODE* ConstructBSTHeapTree(TREE_NODE nodes[], int n)
{
        assert(nodes && n>0);

        sort(nodes, nodes+n, SortLftVal);

        return _inner_construct(nodes, n);
}
回复

使用道具 举报

🔗
Narashy 2011-6-23 02:32:28 | 只看该作者
全局:
回复 4# wwwyhx

这个做法最坏情况下是o(n^2)的,poj上那题很可能过不了吧
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-6-24 18:07:44 | 只看该作者
全局:
回复  wwwyhx

这个做法最坏情况下是o(n^2)的,poj上那题很可能过不了吧
Narashy 发表于 2011-6-23 02:32



    啊???????????????????????????????????
回复

使用道具 举报

🔗
Narashy 2011-6-26 12:11:24 | 只看该作者
全局:
啊???????????????????????????????????
wwwyhx 发表于 2011-6-24 18:07



    你可以试着提交下看看啊,可能会得到 time limit exceeded 的结果
回复

使用道具 举报

🔗
lambda2fei 2012-9-4 15:43:24 | 只看该作者
全局:
wwwyhx 发表于 2011-6-24 18:07
啊???????????????????????????????????

想到个O(nlogn)的解法,不过要排序两次,分别对两个关键字,描述起来比较复杂
回复

使用道具 举报

🔗
CStick75 2012-9-6 20:00:20 | 只看该作者
全局:
实际上可以RMQ吧……对value排序,然后不停地RMQ得到根节点,最多查询N次……
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表