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 ...
随机推荐
- 使用Android自带的资源
Android自带的资源文件有 :https://developer.android.google.cn/reference/android/R.html 代码中使用如下: 1.查看源代码的资源文件 ...
- Thymeleaf 页面表达式基础
转自:http://www.cnblogs.com/vinphy/p/4674247.html#undefined (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个 ...
- P2814 家谱
我真没什么创意了woc.. so,为什么一道水题是蓝色的???哦哦哦,水好像就是蓝色的,emmm那就不是恶意评分了嘤嘤嘤 ... 好吧实际上可能是非c党对于字符串的处理需要进行编号和结构体,会麻烦一点 ...
- ssh端口转发功能
一.SSH 端口转发能够提供两大功能: 1.加密SSH Client 端至SSH Server 端之间的通讯数据 2.突破防火墙的限制完成一些之前无法建立的TCP 连接 (隧道功能) 二:SSH端口 ...
- redis学习(三)
如何保障reids的数据安全和性能? 一.持久化选项 1.快照snapshotting 它可以将存在于某一时刻的所有数据都写入硬盘里面. 配置选项示例: save 60 1000 注:从最近一次创 ...
- 交换机安全学习笔记 第四章 VLAN
Trunk 口 思科称为:native VLAN 华为称为:PVID 说白了就是Trunk端口本身所属的VLAN,因为,Trunk端口要"透传"多个VLAN的流量,其本 ...
- POJ 3585 Accumulation Degree 题解
题面 一句话题意:找一个点使得,使得从这个点出发作为源点,发出的流量最大,输出这个最大的流量 这道题是换根法+二次扫描的模板: 首先若确定1为原点,那么可以写出dp方程:当v的度是1时, g[u]+= ...
- ps -ef 和ps -aux的区别
在 linux 显示进程的命令是ps ,常用的是 ps -ef,今天看到了还有一个ps -aux,查询了资料,这里总结一下 那么ps -ef 和ps -aux 有什么区别呢? 其实区别不是很大,这就要 ...
- PythonDay09
第九章函数 今日内容 函数定义 函数调用 函数返回值 函数的参数 函数定义 # 通过定义一个计算数据长度的函数,def为关键字,count_len是函数名def count_len(): lst = ...
- Vue进行路由跳转的几种方式
1.<router-link to="需要跳转到页面的路径"> 2.this.$router.push()跳转到指定的url,并在history中添加记录,点击回退返回 ...