1. 程式人生 > >堆的應用!--求第k大數

堆的應用!--求第k大數

求一個數列中的第k大的數,將前k個數建最小堆,後面若比堆頂元素還小,則捨去,否則將堆頂置換成該元素,然後維護堆,最後輸出堆頂元素~~

若求第k小的數,則只需建最大堆即可。

public class heap_test {//求第k大的數,維護一個大小為k的最小堆
	private static int k,n;
	private static int []a=new int [101];
	public static void down(int i,int n){
		int t,temp;
		while(i*2<=n){
			if (a[i]>a[i*2]){
				t=i*2;
			}else{
				t=i;
			}
			if (a[t]>a[i*2+1]){
				t=i*2+1;
			}
			if (t!=i){
				temp=a[t];
				a[t]=a[i];
				a[i]=temp;
				i=t;
			}else break;
		}
	}
	public static void main(String[] args) {
		Scanner cin=new Scanner(System.in);
		k=cin.nextInt();//讀取k
		n=0;
		while(cin.hasNextInt()){
			n++;
			a[n]=cin.nextInt();
		}
		cin.close();
		if (k<=n){
		for (int i=k/2;i>=1;i--){
			down(i,k);
		}
		int num=n;
		for (int i=k+1;i<=num;i++){
			if (a[i]>a[1]){//如果更大
				a[1]=a[i];
				down(1,i);
			}
		}
		System.out.println(a[1]);
  }
	

   }
}


相關推薦

應用--k大數

求一個數列中的第k大的數,將前k個數建最小堆,後面若比堆頂元素還小,則捨去,否則將堆頂置換成該元素,然後維護堆,最後輸出堆頂元素~~ 若求第k小的數,則只需建最大堆即可。 public class h

ACM_k大數(三分)

求第k大 Time Limit: 6000/3000ms (Java/Others) Problem Description: 給定兩個陣列A和B,大小為N,M,每次從兩個陣列各取一個數相乘放入陣列C,最終得到一個N*M的陣列C。求C中第K大的數。 Input: 輸入包含

HDOJ4006 The kth great number --優先佇列k大數

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 7004    Acc

k大數

#include<bits/stdc++.h> using namespace std; int n,k,a[10000]; int qsort(int l,int r,int k) { int key=a[r]; int i=l,j=r; while(i!=j)

線性時間複雜度陣列中K大數

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

快速排除(最優法,不是快排)隨機陣列中k大數(c++)(讓數隨機沒寫)

#include <iostream> using namespace std; int get_kth(int arr[], int n, int k) {     int left = 0;     int right = n - 1;     while

應用之——K最大值和中值

今天和大家一塊學習下面試中常見的兩個關於堆的面試題,第一:求K個最大值;第二:求中值元素。演算法和資料結構算是筆者的死穴了。 一、求前K個最大的值 首先能想到的是使用Arrays.sort()進行排序後取前K個值即可,效率是O(N*log2^N)。

陣列中第二大數(後續K大)

package cn.edu.nwsuaf.cie.qhs; import java.util.Scanner; public class GetGreater {     private int initArray[];     public int[] getInitArray() {      

POJ 2104 K-th Number(區間k大數)(平方切割,歸並樹,劃分樹)

ac代碼 deb rank turn tracking line 查看 div 能夠 題目鏈接: http://poj.org/problem?id=2104 解題思路: 由於查詢的個數m非常大。樸素的求法無法在規定時間內求解。因此應該選用合理的方式維護數據來做到高效

The kth great number multiset應用(找k大值)

for begin urn mes etc when ++ multi size The kth great number Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao

spoj 7258 SUBLEX(k大字串

top stdin add algo can find size esp con   其實對sam的拓撲排序我似懂非懂但是會用一點了。    /** @xigua */ #include <stdio.h> #include <cmath> #in

A*k短路

display ++ truct OS print LG algo IV AD A*算法 理論:https://www.cnblogs.com/n-u-l-l/archive/2012/07/29/2614194.html 例題1: UESTC A*+最短路 1

poj1442(treapk大)

syn div gcd += ace str while pre clu ac代碼: #include<bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i <=

A*算法的認識與K短路模板

最短 啟發式 單源最短路 進行 鄰接 target 技術分享 urn 它的 現在來了解A*算法是什麽 現在來解決A*求K短路問題  在一個有權圖中,從起點到終點最短的路徑成為最短路,第2短的路成為次短路,第3短的路成為第3短路,依此類推,第k短的路成為第k短路。那麽,第

hdu3949(線性基,k小的異或和

鏈接 sel time cau int another logs 題目 there 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 XOR Time Limit: 2000/1000 MS (Java/Others)

尋找k大數

#include "stdafx.h" #include<cstdio> #include<iostream> #include<algorithm> using namespace std; const int maxn=11000000; typedef lo

無序數組K大/K小的數

std name 根據 ide ace mat algorithm cout ios 方法一:quicksort 根據快排思想,從後往前找比基準數小的,交換位置。 從前往後找比基準數大的,交換位置。 最後安放基準數。 保證 l到p 是大數,若 p-l+1==k 那麽p就是第

k短路板子 ~~~A*+Dijkstra

#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <

LeetCode703 Kth Largest Element in a Stream(K大元素)

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth dis

HDU2852 KiKi's K-Number (權值線段樹k大)

題意:三種操作,0 e 表示插入一個數字e,1 e 表示刪除一個數字e,2 e k 表示查詢比e大的第k個數,刪除和查詢均可能沒有 目標。 思路:建一棵權值線段樹,維護每個數字區間中數字的數量。 查詢時,先查出1到e的數字數量n,然後查詢第k+n大。 #include<cs