https://www.acwing.com/problem/content/155/

#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack> using namespace std; const int N = 1010; int n;
int a[N], minv[N];
bool g[N][N];
int color[N]; bool dfs(int u, int c)
{
color[u] = c;
for (int v = 1; v <= n; v++)
if (g[u][v])
{
if (color[v] == c) return false;
if (color[v] == -1 && !dfs(v, !c)) return false;
} return true;
} bool isBipartite() {
for (int i = 1; i <= n; i ++ )
if (color[i] == -1)
if (!dfs(i, 0)) {
return false;
}
return true;
} void printResult() {
stack<int> stk1, stk2;
int cur = 1;
for (int i = 1; i <= n; i ++ )
{
if (color[i] == 0)
{
stk1.push(a[i]);
cout << "a ";
}
else
{
stk2.push(a[i]);
cout << "c ";
} while (true)
{
if (stk1.size() && stk1.top() == cur)
{
stk1.pop();
cout << "b ";
cur++ ;
}
else if (stk2.size() && stk2.top() == cur)
{
stk2.pop();
cout << "d ";
cur++ ;
}
else break;
}
}
cout << endl;
} void sort(int a[]) {
minv[n + 1] = n + 1;
for (int i = n; i >= 0; i--) {
minv[i] = min(minv[i + 1], a[i]);
} memset(g, false, sizeof g);
for (int i = 1; i <= n; i ++ )
for (int j = i + 1; j <= n; j ++ )
if (a[i] < a[j] && minv[j + 1] < a[i])
g[i][j] = g[j][i] = true; memset(color, -1, sizeof color); if (!isBipartite()) {
cout << 0 << endl;
return;
} printResult();
} int main()
{
int T;
cin >> T;
while (T -- )
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
sort(a); } return 0;
}

  

AcWing 153. 双栈排序的更多相关文章

  1. NOIP2008双栈排序[二分图染色|栈|DP]

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  2. noip2008 双栈排序

    题目描述 Description \(Tom\)最近在研究一个有趣的排序问题.如图所示,通过\(2\)个栈\(S_1\)和\(S_2\),\(Tom\)希望借助以下\(4\)种操作实现将输入序列升序排 ...

  3. BZOJ 2080: [Poi2010]Railway 双栈排序

    2080: [Poi2010]Railway Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 140  Solved: 35[Submit][Statu ...

  4. 双栈排序(codevs 1170)

    题目描述 Description Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈 ...

  5. #include <NOIP2008 Junior> 双栈排序 ——using namespace wxl;

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  6. [NOIP2008] 提高组 洛谷P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  7. 【NOIP2008】双栈排序

    感觉看了题解还是挺简单的,不知道当年chty同学为什么被卡了呢么久--所以说我还是看题解了 原题: Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将 ...

  8. 双栈排序(codevs 1170)题解

    [问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈 ...

  9. Noip2008双栈排序

    [问题描述] 用两个栈使一个1...n的排列变得有序.一共有四个操作: A.stack1.push() 读入一个放入栈一 B.stack1.pop() 弹出栈一放入输出序列 C.stack2.push ...

随机推荐

  1. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 12—Support Vector Machines 支持向量机

    Lecture 12 支持向量机 Support Vector Machines 12.1 优化目标 Optimization Objective 支持向量机(Support Vector Machi ...

  2. 删除链表之two star programming

    最近偶尔看到一篇关于Linus Torvalds的访问,大神说大部分人都不理解指针.假设被要求写一段链表删除节点的程序,大多数的做法都是跟踪两个指针,当前指针cur和其父节点pre,这种实现很容易理解 ...

  3. MySQL数据库篇之存储引擎

    主要内容: 一.数据引擎 二.MySQL支持的存储引擎 三.使用存储引擎 1️⃣ 什么是存储引擎? MySQL中建立的库----> 文件夹,库中建立的表----->文件. 现实生活中我们用 ...

  4. centos7 安装wxPython

    *** exact error that occured. This usually means GTK+ is incorrectly installed.    configure: error: ...

  5. Slim安装以及使用【转】

    最近在用backbone.js 做东西,因为牵扯到REST services 所以需要后台支持,此处选择了php.Slim 是php的一个框架. 貌似国内文章对此的介绍比较少,在安装Slim的过程中出 ...

  6. cs api测试

    更新磁盘方案 http://192.168.150.16:8900/client/api?command=updateDiskOffering&id=199e3be4-2af1-47a3-9f ...

  7. Windows ---- mysql 5.7 配置安装

    去官网下载mysql 下载地址    https://dev.mysql.com/downloads/mysql/    根据自己操作系统位数选择相对应的版本 点击Download下载 下载下来后是一 ...

  8. CentOS搭建VSFTP服务器

    一.安装vsftpd 1.查看是否已经安装vsftpd 2.如果没有,就安装 3.测试是否安装成功 4.安装成功设置开机启动 二.配置vsftpd 1.修改配置文件/etc/vsftpd/vsftpd ...

  9. vmware workstation环境下虚拟机固定ip的设置

    一.准备 (1)vmware workstation (2)centOS 6.5 二.配置 采用nat模式对虚拟机进行固定ip配置,nat模式相当于在windows的操作系统下构建了一个独立的内部局域 ...

  10. xgboost 里边的gain freq, cover

    assuming that you're using xgboost to fit boosted trees for binary classification. The importance ma ...