hdu6215 Brute Force Sorting 模拟题+想法题

来源:互联网 发布:ruby python nodejs 编辑:程序博客网 时间:2024/06/05 04:44

Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 887    Accepted Submission(s): 228


Problem Description
Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
1. A[i] is the first element of the array, or it is no smaller than the left one A[i1].
2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
Help Beerus predict the final array.
 

Input
The first line of input contains an integer T (1T10) which is the total number of test cases.
For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
The second line describes the array with N positive integers A[1],A[2],,A[N] where each integer A[i] satisfies 1A[i]100000.
 

Output
For eact test case output two lines.
The first line contains an integer M which is the size of the final array.
The second line contains M integers describing the final array.
If the final array is empty, M should be 0 and the second line should be an empty line.
 

Sample Input
551 2 3 4 555 4 3 2 151 2 3 2 151 3 5 4 252 4 1 3 5
 

Sample Output
51 2 3 4 5 021 2 21 3 32 3 5
 

Source
2017 ACM/ICPC Asia Regional Qingdao Online

题目大意:
给你一个序列,我们需要每次删除这个序列中不满足同左右不是递增关系的数,直到整个序列中的所有数据都满足这个条件为止。

解题思想:这道题目我们如果每次都是从前到后一个个比较删除肯定会超时的,但是如果我们每次都记录一下删除的位置,因为下一次删除的地方肯定是上一次删除的地方的前后,所以我们每次都处理之前处理过的位置就可以了,如果没有删除,那么以后都不会再删除了,直到所有的删除都没有即数据删除完成。

ac代码:
#include<cstdio>#include<cstring>#define N 100003//题目数据范围10^5 int t,n;int a[N];//存储读入的数据数组 int next[N],last[N];//用来实现双向链表 功能数组 int que[N],top;//que数组用以实现队列功能数组 int main(){scanf("%d",&t);while(t--){scanf("%d",&n);int ans=n;a[0]=0;//数组初始值赋为0 next[0]=1;//后继数组初始化 last[n+1]=n;//前驱数组初始化 for(int i=1;i<=n;i++){next[i]=i+1;//i只是表示的在a数组中的位置量,后面一个位置 last[i]=i-1;//前面的一个位置 scanf("%d",&a[i]);que[top++]=i;//将每一个 数据位置都存入队列数组里面 }int flag=1;//设置标记量,方便后面判断处理是否完成 while(flag){int s=0;//初始化的量都为0 flag=0;int now=0;while(now<top)//从头到尾搜一遍保存在队列里的数 {int i=que[now],ff=0;//ff用来存储需要删除的数据的数量 while(next[i]<=n)//用来找到当前删除后的位置 {if(a[i]>a[next[i]])ff++,i=next[i],flag=1;//存在删除标记1 else break;//如果不存在删除则重新来过 }if(ff)ans-=(ff+1);//ans用来存储剩余的数量 if(ff)//如果删除了数据 {next[last[que[now]]]=next[i];//将之前的 双向链表指向改变 last[next[i]]=last[que[now]];//相当于删除中间的数据 que[s++]=last[que[now]];//将新的入队列 }while(que[now]<=i&&now<top)now++;//将now指针后移 }top=s;//top记录的是删除的次数 }printf("%d\n",ans);int now=0;while(now<=n)//将链表里剩余的数据输出即可 {if(now!=0)printf("%d ",a[now]);now=next[now];//通过后继节点的位置来达到链表的功能从而实现输出整个链表的数据 }printf("\n");}return 0;}

题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=6215




原创粉丝点击