码迷,mamicode.com
首页 > 其他好文 > 详细

数列有序!

时间:2019-04-30 23:38:57      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:tput   otto   def   its   none   content   opened   lower   output   



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

 

数列有序!

标签:tput   otto   def   its   none   content   opened   lower   output   

原文地址:https://www.cnblogs.com/Auroras/p/10798378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!