Contest6 The K-th largest number

本文介绍了一种在大规模数据集中查找第K大的数的方法,并提供了一个时间复杂度为O(NlogK)的算法实现示例。该算法适用于数据集非常大(如100万到1000万之间)的情况。

Problem B: The K-th largest number

Time Limit: 2 Sec   Memory Limit: 40 MB
Submit: 856   Solved: 233
[ Submit][ Status][ Web Board]

Description

Given N integer numbers, find the K-th largest number. 
Requirement: design an algorithm with the time complexity O(NlogK).

Input

The first line contains N, K. In the following N lines, each line contains an integer value. 
Note that N could be very large ( 1000000 <= N <= 10000000). You should allocate the memory dynamically and design the algorithm with the time complexity O(NlogK).

Output

Sample Input

12 4
7
2
1
5
6
6
4
3
8
8
10
9

Sample Output

8

HINT

If the memory is not enough, you may apply a sliding window to split the batchs of input and sort in each batch.



#include <stdio.h>
#include <time.h>
#include <stdlib.h>


void qsort2(int* a, int l, int r, int rank) {
	int m;
	if (l < r) {
		m = partition2(a, l, r);
	}
	if (r - m + 1 == rank) return;
	else if (r - m + 1 > rank) qsort2(a, m + 1, r, rank); 
	else qsort2(a, l, m - 1, rank - (r - m + 1));
}

int partition2(int* a, int l, int r) {
	int i = l;
	int j = r;
	int pivot = a[l];
	while (i < j) {
		while ((a[j] >= pivot) & (i < j)) --j;
		a[i] = a[j];
		while ((a[i] <= pivot) & (i < j)) ++i;
		a[j] = a[i];
	}
	return j;
}

int main() {
	srand(time(NULL));
	int n, rank;
	scanf("%d %d", &n, &rank);
	int* a = (int*) malloc(n * sizeof(int));
	for (int i = 0; i != n; ++i) {
		scanf("%d", a + i);
	}
	qsort2(a, 0, n - 1, rank);
	printf("%d\n", a[n - rank]);
	return 0;
}


# T671331 [ZJCPC 2017] Problem Preparation ## 题目描述 It's time to prepare the problems for the $14$-th Zhejiang Provincial Collegiate Programming Contest! Almost all members of SUA programming contest problem setter team brainstorm and code day and night to catch the deadline, and empty bottles of $\textit{Marjar Cola}$ litter the floor almost everywhere! To make matters worse, one of the team member fell ill just before the deadline. So you, a brilliant student, are found by the team leader Dai to help the team check the problems' arrangement. Now you are given the difficulty score of all problems. Dai introduces you the rules of the arrangement: - The number of problems should lie between $10$ and $13$ (both inclusive). - The difficulty scores of the easiest problems (that is to say, the problems with the smallest difficulty scores) should be equal to $1$. - At least two problems should have their difficulty scores equal to $1$. - After sorting the problems by their difficulty scores in ascending order, the absolute value of the difference of the difficulty scores between two neighboring problems should be no larger than $2$. BUT, if one of the two neighboring problems is the hardest problem, there is no limitation about the difference of the difficulty scores between them. The hardest problem is the problem with the largest difficulty score. It's guaranteed that there is exactly one hardest problem. The team members have given you lots of possible arrangements. Please check whether these arrangements obey the rules or not. ## 输入格式 There are multiple test cases. The first line of the input is an integer $T$ ($1 \le T \le 10^4$), indicating the number of test cases. Then $T$ test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 100$), indicating the number of problems. The next line contains $n$ integers $s_1, s_2, \dots, s_n$ ($-1000 \le s_i \le 1000$), indicating the difficulty score of each problem. We kindly remind you that this problem contains large I/O file, so it's recommended to use a faster I/O method. For example, you can use scanf/printf instead of cin/cout in C++. ## 输出格式 For each test case, output "Yes" (without quotes) if the arrangement follows the rules, otherwise output "No" (without quotes). ## 输入输出样例 #1 ### 输入 #1 ``` 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 999 1 1 2 3 4 5 6 7 8 9 11 999 1 3 5 7 9 11 13 17 19 21 10 15 1 13 17 1 7 9 5 3 11 13 1 1 1 1 1 1 1 1 1 1 1 1 2 10 2 3 4 5 6 7 8 9 10 11 10 15 1 13 3 6 5 4 7 1 14 ``` ### 输出 #1 ``` No No Yes No Yes Yes No No ``` ## 说明/提示 The first arrangement has $9$ problems only, which violates the first rule. Only one problem in the second and the fourth arrangement has a difficulty score of $1$, which violates the third rule. The easiest problem in the seventh arrangement is a problem with a difficulty score of $2$, which violates the second rule. After sorting the problems of the eighth arrangement by their difficulty scores in ascending order, we can get the sequence $\{1, 1, 3, 4, 5, 6, 7, 13, 14, 15\}$. We can easily discover that $|13-7| = 6 > 2$. As the problem with a difficulty score of $13$ is not the hardest problem (the hardest problem in this arrangement is the problem with a difficulty score of $15$), it violates the fourth rule.
最新发布
09-30
The following is the current ranking rules for the ICPC Asia EC Online Qualifiers, and there will be two online contests.\n\nIn each contest, only the rank of the top-ranked team from each university will be taken as the score of that university;\nIn each contest, participating universities will be ranked according to their scores;\nThe two rankings of universities are combined using the merge sorting method. For any two universities that obtain the same ranking in different contests, the university that received this ranking in the first contest will be ranked first.\nDelete duplicate universities and obtain the final ranking of all participating universities (only the highest rankings for each university are retained).\nNow assuming that there are n teams in the first contest and m teams in the second contest.\nFor each contest, given the ranking of each team and the university to which it belongs, please output the final ranking of all participating universities according to the above rules.\nYou can better understand this process through the sample.\nInput\nThe first line contains two integers n,m (1≤n,m≤104) , representing the number of teams participating in the first contest and the second contest.\nThen following n lines, the i-th line contains a string si (1≤∣si∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the first contest belongs.\nThen following m lines, the i-th line contains a string ti (1≤∣ti∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the second contest belongs.\nIt’s guaranteed that each university has only one abbreviation.\nOutput\nOutput several lines, the i-th line contains a string, representing the abbreviation of the i-th ranked university in the final ranking.\nYou should ensure that the abbreviation of any participating universities appears exactly once. 翻译一下
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值