You are given n numbers a1, a2, . . . , an. It takes constant time to check whether two numbers ai and aj are of the same value. The goal is to check whether more than half of the numbers have the same value. Design an O(n log n) time algorithm to solve the problem.详细的分析运行时间
时间: 2023-03-13 11:13:03 浏览: 169
答:我们可以使用快速排序算法来解决这个问题。先对这n个数字进行排序,时间复杂度为O(n log n) 。排序完成后,可以在常数时间内判断相邻的数字是否相等。如果有大于一半的数字相等,那么排序后它们会聚集在一起。因此,只需要在排序结果中检查相邻的数字即可,从而完成对问题的求解。
相关问题
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions. Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user. Input Specification: Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10 4 ) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space. Output Specification: For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
这道题的任务很简单:将一串不同的正整数插入哈希表中,并输出输入数字的位置。哈希函数的定义为 H(key)=key%TSize,其中 TSize 是哈希表的最大大小。使用二次探测(只增加正数)来解决碰撞。注意,表格大小最好为质数。如果用户给出的最大大小不是质数,则必须重新定义表格大小为大于用户给出大小的最小质数。
输入格式:每个输入文件包含一个测试用例。对于每种情况,第一行包含两个正数:MSize(≤10 4)和N(≤MSize),它们是用户定义的表大小和输入数字的数量。然后,在下一行中给出N个不同的正整数。一行中的所有数字由空格分隔。
输出格式:对于每个测试用例,在一行中打印输入数字的相应位置(索引从0开始)。一行中的所有数字由空格分隔,行末不能有多余的空格。如果无法插入数字,则打印“-”。
用c++解决Time limit: 3.0 second Memory limit: 64 MB. Input The first line contains the number n of fighters in the division (2 ≤ n ≤ 50000). The second line contains ten integers in the range from 1 to 10000 separated with a space written in the nonascending order. These are the times of sending a message from one telegraph to another if the length of their common prefix is zero, one, two, …, nine. The next n lines contain the numbers of telegraphs given to the fighters of the division. The number of Anka's telegraph is described first, and the number of Chapaev's telegraph is described last. All the numbers of telegraphs are different. Output Output the only line “-1” if it is impossible to deliver the message to Chapaev. Otherwise, in the first line output the minimal time required to deliver the message. In the second line output the number of fighters in the delivery path, and in the third line output their numbers separated with a space in the order from Anka to Chapaev. The fighters of the 25th Division are numbered from 1 to n in the order in which their mobile telegraphs are described in the input. If there are several ways to deliver the message in minimal time, output any of them.
这是一道基于图论的问题,我们可以将每个电报作为一个节点,然后根据题目给出的条件,连接边,并给每条边赋上对应的权值。然后,我们可以使用 Dijkstra 算法求出从 Anka 的节点到 Chapaev 的节点的最短路径。如果不存在这样的路径,则输出 -1。
具体实现步骤如下:
1.读入数据,构建图。
2.使用 Dijkstra 算法求出从 Anka 的节点到 Chapaev 的节点的最短路径。
3.输出结果。
C++ 代码实现如下:
阅读全文