HDU2523 SORT AGAIN HASH

SORT AGAIN

                                                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                                                              Total Submission(s): 1395    Accepted Submission(s): 448


Problem Description

给你N个整数,x1,x2...xn,任取两个整数组合得到|xi-xj|,(0<i,j<=N,i!=j)。
现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它)。
 

Input

输入数据首先包含一个正整数C,表示包含C组测试用例.
每组测试数据的第一行包含两个整数N,K。(1<N<=1000,0<K<=2000)
接下去一行包含N个整数,代表x1,x2..xn。(0<=xi<=2000)
 

Output

对于每组测试数据,请输出第K大的组合数,每个输出实例占一行。
 

Sample Input
3
3 2
4 0 7
4 2
1 2 3 4
2 1
2 9
 

Sample Output
4
2
7
   小白叫咱们学STL,没想到这题竟然写成了这个样子,无语啊!!!
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

vector< int > s, r;

int hash[2005];

int main(  )
{
	int T;
	cin>> T;
	while( T-- )
	{
		int N, K;
		cin>> N>> K;
		for( int h= 1; h<= N; ++h )
		{
			int c;
			cin>> c;
			s. push_back( c );
		}
		for( vector< int >:: iterator i= s. begin(  ); i!= s. end(  ); ++i )
		{
			vector< int >:: iterator t= ++i;
			--i;
			for( vector< int >:: iterator j= t; j!= s. end(  ); ++j )
			{
//				cout<< abs( *i- *j )<< endl;
//				r. push_back( abs( *i- *j ) )
//				cout<< *i<< *j<< endl;
				hash[ abs( *i- *j ) ]= 1;
			}   
		}
//		sort( r. begin(  ), r. end(  ) );
//		cout<< r. size(  )<< endl;
//		for( vector< int >:: iterator i= r. begin(  ); i!= r. end(  ); ++i )
//		{
//			cout<< *i<< " ";
//		}		
//		cout<< "\n";
//		unique( r. begin(  ), r. end(  ) );
//		cout<< r[K- 1]<< endl;
//		s. clear(  ), r. clear(  );
		int cnt= 0;
		for( int i= 0; i<= 20000; ++i )
		{
			if( hash[i] )
			{
				++cnt;
				if( cnt== K )
				{
					cout<< i<< endl;
					break;
				}
			}
		}
		s. clear(  );
		memset( hash, 0, sizeof( hash ) );
	}
	return 0;
}

  各位知道我在干什么吗?

原文地址:https://www.cnblogs.com/Lyush/p/2119646.html