数列有序!



Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
 

 

Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
 

 

Output
对于每个测试实例,输出插入新的元素后的数列。
 

 

Sample Input
3 3 1 2 4 0 0
 
      一开始用set,wa,可能是set维护成本吧。
 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 int main()
 5 {
 6     int ans[110];
 7     int n,m;
 8     while(cin>>n>>m&&n+m)
 9     {
10         for(int i=1;i<=n;++i)
11             cin>>ans[i];
12         int l=lower_bound(ans+1,ans+n+1,m)-ans;
13         for(int i=n;i>=l;i--)
14         {
15             ans[i+1]=ans[i];
16         }
17         ans[l]=m;
18         cout<<ans[1];
19         for(int i=2;i<=n+1;++i)
20             cout<<" "<<ans[i];
21         cout<<endl;
22 
23     }
24 }
View Code

 

posted @ 2019-04-30 22:31  Auroras  阅读(135)  评论(0编辑  收藏  举报