由于数的范围是限定的,考虑用哈希。
注意:此题不能用cin cout,会超时
#include<iostream> #include<cstdio> #include<cstring> #define maxn 1000005 using namespace std; bool ifhas[maxn]; int main(void){ int n,m; while(scanf("%d %d",&n,&m)!=EOF){ memset(ifhas,false,sizeof(ifhas)); int a; int max = -100000000; int min = 100000000; for(int i = 1;i <= n;i ++){ scanf("%d",&a); max = max > a?max:a; min = min < a?min:a; ifhas[a + 500000] = true; } int start = max + 500000; int end = min + 500000; for(int i = 1;i < m;i ++) { for(int j = start;j >= end;j --){ if(ifhas[j] == true){ printf("%d ",j - 500000); start = j - 1; break; } } } for(int j = start;j >= end;j --){ if(ifhas[j]){ printf("%d ",j - 500000); break; } } } }