flash可以做網(wǎng)站seo優(yōu)化運(yùn)營(yíng)
輸入一個(gè)長(zhǎng)度為?n?的整數(shù)數(shù)列,從小到大輸出前?m?小的數(shù)。
輸入格式
第一行包含整數(shù)?n?和?m。
第二行包含?n?個(gè)整數(shù),表示整數(shù)數(shù)列。
輸出格式
共一行,包含?m?個(gè)整數(shù),表示整數(shù)數(shù)列中前?m?小的數(shù)。
數(shù)據(jù)范圍
1≤m≤n≤,
1≤數(shù)列中元素≤
輸入樣例:
5 3
4 5 1 3 2
輸出樣例:
1 2 3
代碼:
#include<iostream>
#include<algorithm>
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(){cin>>n>>m;for(int i = 1;i<=n;i++){cin>>heap[i];}l = n;for(int i = n/2;i>0;i--){down(i);}while(m--){cout<<heap[1]<<" ";heap[1] = heap[l];l--;down(1);}return 0;
}
?