HDU 6215 Brute Force Sorting [链表]
来源:互联网 发布:unity3d手机游戏大全 编辑:程序博客网 时间:2024/06/05 11:03
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215
题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1]。我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成。
题解:双向链表模拟过程,设置一个队列,用于存可能产生非排序过的串的头结点,每次从队列中拿出一个头结点判断后面的情况,若还能继续删除,则将头结点的上一个结点放入队列继续判,知道没有别的结点为止。
AC代码:
#include<cstdio>#include<cstring>#define N 100005int t,n;int a[N],next[N],mark[N],last[N];int que[N],top;int main(){scanf("%d",&t);while(t--){memset(mark,0,sizeof(mark));scanf("%d",&n);int ans=n;a[0]=0;next[0]=1;last[n+1]=n;for(int i=1;i<=n;i++){next[i]=i+1;last[i]=i-1;scanf("%d",&a[i]);que[top++]=i;}int flag=1;while(flag){int s=0;flag=0;int now=0;while(now<top){int i=que[now],ff=0;while(next[i]<=n){if(a[i]>a[next[i]])ff++,i=next[i],flag=1;else break;}if(ff)ans-=(ff+1);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++;}top=s;}printf("%d\n",ans);int now=0;while(now<=n){if(now!=0)printf("%d ",a[now]);now=next[now];}printf("\n");}return 0;}
阅读全文
0 0
- HDU 6215 Brute Force Sorting [链表]
- HDU 6215 Brute Force Sorting [链表]
- Hdu 6215 Brute Force Sorting【链表】
- hdu 6215 Brute Force Sorting 【链表+队列】
- HDU 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- HDU 6215 Brute Force Sorting 链表 + 队列(模拟)
- HDU 6215 Brute Force Sorting 模拟
- HDU 6215 Brute Force Sorting【双端链表】
- hdoj 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting 模拟链表
- HDU 6215 Brute Force Sorting (链表维护数组)
- HDU 6215 Brute Force Sorting(伪双向链表删边)
- HDU-6215 Brute Force Sorting(思维、模拟链表)
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
- 【2017青岛网络赛】1010 hdu 6215 Brute Force Sorting 双向链表+队列
- HDU6215 Brute Force Sorting 【模拟】
- Bitwise Operation
- HDU-6214 Smallest Minimum Cut(最大流)
- Tensorflow-图像标签问题
- Qt之打包发布(NSIS详解)
- SpringCloud(第 008 篇)电影微服务,使用 application.yml 配置文件配置 Ribbon 在客户端进行负载均衡调度算法
- HDU 6215 Brute Force Sorting [链表]
- 索引的实现原理
- BZOJ 3262 陌上花开
- C#中控件Control的Paint事件和OnPaint虚函数的区别
- springMVC中简单的web.xml配置信息
- 1134. Vertex Cover (25)
- Web框架面试题
- 实验一报告
- LeetCode 55. Jump Game