poj 4088:集合运算
题目:(至于4089,那个问题做过,使用归并思想,所以没有写)
小张需要从一批数量庞大的正整数中挑选出第k小的数,因为数据量太庞大,挑选起来很费劲,希望你能编程帮他进行挑选。
第二行以后是n个数据T(1<=T<=109),数据之间以空格或者换行符分隔。
10 5 1 3 8 20 2 9 10 12 8 9
8
解题方案
借用于快速排序思想,查找第K小元素
代码
#include <iostream> #include <fstream> using namespace std; void main_solution(); void read_data( int *& data,int &n,int &k ); int min_k(int *data,int s,int e,int k); int main() { main_solution(); system("pause"); return 0; } void read_data( int *& data,int &n,int &k ) { ifstream reader; reader.open( "data.txt" ); reader>>n; reader>>k; data = new int[n]; for(int i=0;i<n;i++) { reader>>data[i]; } reader.close(); } int min_k(int *data,int s,int e,int k) { int t = s; int another = e; while(t != another) { if( t < another ) { if( data[t] <= data[another] ) another --; else { int d = data[another]; data[another] = data[t]; data[t] = d; d = another; another = t; t = d; } } else { if( data[t] >= data[another] ) another ++; else { int d = data[another]; data[another] = data[t]; data[t] = d; d = another; another = t; t = d; } } } if( k == t-s+1 ) return data[t]; else if( k < t-s+1 ) return min_k(data,s,t-1,k); else return min_k(data,t+1,e,k - (t-s+1)); } void main_solution() { int * data; int n; int k; read_data(data,n,k); cout<<min_k(data,0,n-1,k)<<endl; }
时间: 2024-10-06 00:11:03