9.1-2 Find the second smallest of n elements.

9.1-1

Show that the second smallest of n elements can be found with n + lg n - 2

comparisons in the worst case. (Hint: Also find the smallest element.)


I thinked about it for some time and can't work out the answer. First I want to borrow the idea from the text, kepp track of both the smallest and second smallest element in the array. But found that in worst case that will degrade to O(2n). I decided to look for an answer from the Internet.


Internet Solution:

1. get the smallest between each adjacent pairs and get n/2 elements.

2. again get the smallest from the n/2 elements using the same method.

3. repeat the method iteratively until we have the smallest element.

4. in the process we track the elements that has been compared the the smallest, and the second smallest must in them.

Get the smallest from these elements, that's the second smallest.


Analysis:

1. This is the method that's used in a match. Get the champion using the groups of two method, and get the second from the ones who's defeated by the champion.

2. Exchange space for time. Remember the intermediate information, and use(cache) that for the final or other intermediate purpose.



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Jonathan is fighting against DIO's Vampire minions. There are n of them with strengths a1,a2,…,an. Denote (l,r) as the group consisting of the vampires with indices from l to r. Jonathan realizes that the strength of any such group is in its weakest link, that is, the bitwise AND. More formally, the strength level of the group (l,r) is defined as f(l,r)=al&al+1&al+2&…&ar. Here, & denotes the bitwise AND operation. Because Jonathan would like to defeat the vampire minions fast, he will divide the vampires into contiguous groups, such that each vampire is in exactly one group, and the sum of strengths of the groups is minimized. Among all ways to divide the vampires, he would like to find the way with the maximum number of groups. Given the strengths of each of the n vampires, find the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths. Input The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of vampires. The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109) — the individual strength of each vampire. The sum of n over all test cases does not exceed 2⋅105. Output For each test case, output a single integer — the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.c++实现
最新发布
07-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值