网站建设全流程,浏览器网页版在线使用,徐州网站运营,微信网站制作设计方案输入一个长度为 n 的整数数列#xff0c;从小到大输出前 m 小的数。
输入格式
第一行包含整数 n 和 m。
第二行包含 n 个整数#xff0c;表示整数数列。
输出格式
共一行#xff0c;包含 m 个整数#xff0c;表示整数数列中前 m 小的数。
数据范围
1≤m≤n≤#x…输入一个长度为 n 的整数数列从小到大输出前 m 小的数。
输入格式
第一行包含整数 n 和 m。
第二行包含 n 个整数表示整数数列。
输出格式
共一行包含 m 个整数表示整数数列中前 m 小的数。
数据范围
1≤m≤n≤ 1≤数列中元素≤
输入样例
5 3
4 5 1 3 2输出样例
1 2 3
代码
#includeiostream
#includealgorithm
using namespace std;const int N 100010;
int heap[N],l;
int n,m;void down(int now){int target now;if(2*now l heap[2*now] heap[target]){target 2 * now;}if(2*now 1 l heap[2*now 1] heap[target]){target 2 * now 1;}if(target ! now){swap(heap[target],heap[now]);down(target);}
}int main(){cinnm;for(int i 1;in;i){cinheap[i];}l n;for(int i n/2;i0;i--){down(i);}while(m--){coutheap[1] ;heap[1] heap[l];l--;down(1);}return 0;
}