Problem Description
现有 n 个从小到大排列的数组成的序列。需要对这个序列进行 c 次操作。
每次操作有两种类型:
- 操作 1:插入一个数 v 到序列中,并保持有序。
- 操作 2:输出当前的序列。
bLue 并不太擅长序列操作,所以他想来请求你的帮助,你能帮助他完成这个任务吗?
Input
输入数据有多组(数据组数不超过 30),到 EOF 结束。
对于每组数据:
- 第 1 行输入一个整数 n (1 <= n <= 10^5),表示初始的有序序列中数字的个数。
- 第 2 行输入 n 个用空格隔开的整数 ai (0 <= ai <= 10^6),表示初始序列。
- 第 3 行输入一个整数 c (1 <= c <= 1000),表示有 c 次操作。
- 接下来有 c 行,每行表示一次操作:
- 如果操作类型为 1,则输入格式为 "1 v",其中 v (0 <= v <= 1000) 表示要插入到序列的数。
- 如果操作类型为 2,则输入格式为 "2"。
Output
对于每组数据中的每次类型为 2 的操作,输出一行,表示当前的序列,每个数之间用空格隔开。
Example Input
5 1 2 2 3 5 5 1 0 2 1 3 1 7 2
Example Output
0 1 2 2 3 5 0 1 2 2 3 3 5 7
Hint
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void add(int n)
{
struct node *q,*p,*t;
q=(struct node *)malloc (sizeof(struct node));
q->data=n;
p=head;
t=p->next;
int flag=1;
while(t)
{
if(t->data>q->data)
{
flag=0;
p->next=q;
q->next=t;
break;
}
else
{
p=p->next;
t=t->next;
}
}
if(flag)
{
p->next=q;
q->next=NULL;
}
}
void get(int n)
{
struct node *q,*p;
head->next=NULL;
q=head;
while(n--)
{
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;
}
}
void put()
{
struct node *p;
p=head->next;
while(p!=NULL)
{
if(p->next==NULL)
{
printf("%d\n",p->data);
}
else
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
int b,n,c,i,j;
while(scanf("%d",&n)!=EOF)
{
head=(struct node*)malloc(sizeof(struct node));
struct node *p,*s;
get(n);
scanf("%d",&c);
for(i=1;i<=c;i++)
{
scanf("%d",&j);
if(j==1)
{
scanf("%d",&b);
add(b);
}
if(j==2)
{put();}
}
p=head;
while(p!=NULL)
{
s=p->next;
free(p);
p=s;
}
}
return 0;
}