HDU 6215 Brute Force Sorting 模拟双端链表
一层一层删
链表模拟
最开始写的是一个一个删的 WA
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double EPS = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const int maxm = ;
int a[maxn];
int pre[maxn], last[maxn];
int visit[maxn];
queue<int> que;
int main()
{
int t ;
cin >> t;
while (t--)
{
mem(visit, );
pre[] = -, last[] = ;
int n;
cin >> n;
a[] = -;
a[n + ] = INT_MAX;
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
pre[i] = i - , last[i] = i + ;
}
int flag = ;
for (int i = ; i < n; i++)
if (a[i] <= a[i + ])
{
flag = ;
}
if (!flag)
{
cout << << endl;
continue;
}
for (int i = ; i <= n; i++)
{
if (a[pre[i]] > a[i] || a[last[i]] < a[i])
{
que.push(pre[i]);
visit[i] = ;
pre[last[i]] = pre[i];
last[pre[i]] = last[i];
}
}
while (!que.empty())
{
int now = que.front();
que.pop();
if (!visit[now])
{
continue;
}
if (a[pre[now]] > a[now] || a[last[now]] < a[now])
{
que.push(pre[now]);
visit[now] = ;
pre[last[now]] = pre[now];
last[pre[now]] = last[now];
}
}
int sum = ;
for (int i = ; i <= n; i++)
if (visit[i])
{
sum++;
}
cout << sum << endl;
if (sum)
{
for (int i = ; i <= n; i++)
if (visit[i])
{
printf("%d ", a[i]);
}
cout << endl;
}
}
}
正解:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e5 + ;
int que[maxn], a[maxn], pre[maxn], suf[maxn], n, top;
int main()
{
int _;
cin >> _;
while(_--)
{
top = ;
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
pre[i] = i-;
suf[i] = i+;
scanf("%d", &a[i]);
que[top++] = i;
}
suf[] = ;
int ans = n, flag = ;
while() //一轮一轮的删, 如果没有改动过,说明都是有序的了
{
int cnt = , cur = , flag = ;
while(cur < top) //当前这一轮
{
int p = que[cur], num = ;
while(suf[p] <= n && a[p] > a[suf[p]]) //把一个连续的删掉
{
flag = ;
num++;
p = suf[p];
}
if(num)
{
ans -= num+;
suf[pre[que[cur]]] = suf[p]; //维护链表,使其联通
pre[suf[p]] = pre[que[cur]];
que[cnt++] = pre[que[cur]];
}
while(que[cur] <= p && cur < top) //修改当前指针
cur++;
}
top = cnt;
if(!flag) break; //没有就跳出
}
printf("%d\n", ans);
int cur = ;
while(cur <= n)
{
if(cur)
printf("%d ",a[cur]);
cur = suf[cur];
}
puts("");
}
return ;
}
HDU 6215 Brute Force Sorting 模拟双端链表的更多相关文章
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- hdu 6215 Brute Force Sorting(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...
- HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...
- hdu 6215 -- Brute Force Sorting(双向链表+队列)
题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...
- HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting
Brute Force Sorting Time Limit: 1 Sec Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
- Java数据结构——用双端链表实现队列
//================================================= // File Name : LinkQueue_demo //---------------- ...
- Java单链表、双端链表、有序链表实现
单链表: insertFirst:在表头插入一个新的链接点,时间复杂度为O(1) deleteFirst:删除表头的链接点,时间复杂度为O(1) 有了这两个方法,就可以用单链表来实现一个栈了,见htt ...
随机推荐
- IDEA里面maven菜单解读
- 有关二次离线和 Yuno loves sqrt technology II
二次离线 前置技能 莫队 修改查询 \(O(\sqrt n )-O(1)\) 平衡 概念 考虑朴素莫队离线询问,过程中维护信息从 \([l,r]\) 扩展为 \([l\pm 1,r\pm 1]\) , ...
- 浏览器端-W3School-HTML:HTML DOM Anchor 对象
ylbtech-浏览器端-W3School-HTML:HTML DOM Anchor 对象 1.返回顶部 1. HTML DOM Anchor 对象 Anchor 对象 Anchor 对象表示 HTM ...
- 浏览器端-W3School-JavaScript:JavaScript 事件参考手册
ylbtech-浏览器端-W3School-JavaScript:JavaScript 事件参考手册 1.返回顶部 1. JavaScript 事件参考手册 事件通常与函数配合使用,这样就可以通过发生 ...
- FragmentFactory
import android.support.v4.app.Fragment; import java.util.HashMap; public class FragmentFactory { pri ...
- Java String.Format() 方法及参数说明
转自:https://blueram.iteye.com/blog/441683 JDK1.5中,String类新增了一个很有用的静态方法String.format():format(Locale l ...
- 启用 SR-IOV 解决 Neutron 网络 I/O 性能瓶颈
目录 文章目录 目录 前言 Neutron 的网络实现模型 基于虚拟网络设备的虚拟机流量走向 Neutron 网络实现模型的性能瓶颈 SR-IOV 技术简介 在 Neutron 中引入 SR-IOV ...
- 分期花呗 账户交易通知:尾号6932客户,您的申请已通过,账户余额38139元,无手续费,点t.cn/Aijsx9vq取款,回T退订。
10692285499 分期花呗 账户变动通知:尾号6932客户,您的申请已通过,账户余额5000元,请及时点击t.cn/AiOMsNAm取款,回T退订. 106935276259002分期花呗 账户 ...
- 如图 honehoneclock
如图人体时钟 honehoneclock 页面演示来自http://chabudai.org/blog/?p=59 <embed name="honehoneclock" ...
- 修改Windows 2008以后系统的NTP服务设置
@echo off echo autor OAK @echo off echo -------------------------------- @echo off echo setup time r ...