双栈排序

思路:

  二分图染+模拟;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 1005
#define maxm 2000005
int n,head[maxn],E[maxm],V[maxm],cnt,col[maxn];
int minn[maxn],ai[maxn],sta1[maxn],sta2[maxn],top1,top2;
bool if_[maxn][maxn];
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
void edge_add(int u,int v)
{
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
bool dfs(int now,int dis,int fa)
{
if(col[now]) return true;
col[now]=dis+;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
if(dfs(V[i],dis^,now)) return true;
}
return false;
}
int main()
{
in(n);
for(int i=;i<=n;i++) in(ai[i]);
minn[n]=ai[n];
for(int i=n-;i>=;i--) minn[i]=min(minn[i+],ai[i]);
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
{
if(minn[j+]<ai[i]&&ai[i]<ai[j])
{
if(!if_[i][j])
{
if_[i][j]=true;
if_[j][i]=true;
edge_add(i,j);
}
}
}
}
for(int i=;i<=n;i++)
{
if(!col[i])
{
if(dfs(i,,))
{
printf("");
return ;
}
}
}
int now=;
for(int i=;i<=n;i++)
{
if(ai[i]>now)
{
if(col[i]==) putchar('a'),sta1[++top1]=ai[i];
else putchar('c'),sta2[++top2]=ai[i];
}
else
{
now++;
if(col[i]==) printf("a b");
else printf("c d");
while()
{
if(sta1[top1]==now)
{
printf(" b"),top1--;
now++;continue;
}
if(sta2[top2]==now)
{
printf(" d"),top2--;
now++;continue;
}
break;
}
}
if(i!=n) putchar(' ');
}
while(top1--) printf(" b");
while(top2--) printf(" d");
return ;
}

AC日记——双栈排序 洛谷 P1155的更多相关文章

  1. AC日记——[SDOI2015]星际战争 洛谷 P3324

    题目描述 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战. 在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai.当一个巨型机器人的装甲值 ...

  2. AC日记——联合权值 洛谷 P1351

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  3. AC日记——I Hate It 洛谷 P1531

    题目背景 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 题目描述 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的 ...

  4. AC日记——神奇的幻方 洛谷 P2615(大模拟)

    题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. ...

  5. AC日记——[CQOI2009]DANCE跳舞 洛谷 P3153

    [CQOI2009]DANCE跳舞 思路: 二分+最大流: 代码: #include <cstdio> #include <cstring> #include <iost ...

  6. AC日记——松江1843路 洛谷七月月赛

    松江1843路 思路: 三分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ...

  7. AC日记——严酷的训练 洛谷 P2430

    严酷的训练 思路: 背包: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 5005 int n,m,bi[m ...

  8. AC日记——[SDOI2010]大陆争霸 洛谷 P3690

    [SDOI2010]大陆争霸 思路: dijkstra模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn ...

  9. AC日记——小魔女帕琪 洛谷 P3802

    小魔女帕琪 思路: 概率公式计算: 代码: #include <bits/stdc++.h> using namespace std; ],sig; int main() { ;i< ...

随机推荐

  1. ACE_Message_Block功能简介

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/30/607960.html ACE_Message_Block在Ace中用来表示消息的存放空间 ...

  2. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集

    A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. G - YYS FZU - 2278 数学期望 (大数)

    Yinyangshi is a famous RPG game on mobile phones. Kim enjoys collecting cards in this game. Suppose ...

  4. sshSSH Secure Shell Client root用户无法登录解决办法

    最近使用这个工具,普通用户可以登录root用户不可以登录.将vi /etc/ssh/sshd_config按照下述配置解决问题 修改sshd配置文件:vi /etc/ssh/sshd_config P ...

  5. 最新eclipse安装SVN插件

    转载自:http://welcome66.iteye.com/blog/1845176 eclipse里安装SVN插件,一般来说,有两种方式: 直接下载SVN插件,将其解压到eclipse的对应目录里 ...

  6. 杭电多校第八场-A-Character Encoding

    题目描述 In computer science, a character is a letter, a digit, a punctuation mark or some other similar ...

  7. NOI2001 方程的解数

    1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛  时间限制: 5 s  空间限制: 64000 KB     题目描述 Descripti ...

  8. 好用的python库(转)

    这个专区就是用来分享你在使用 Python 过程中发现的或者自己写的 Python 库.有时候一个好用的库将大大节省一个开发者的时间,也会让开发者多这个库的作者感激涕零的.例如我在开发 Pythonz ...

  9. Grunt构建工具

    Grunt是javascript的构建工具,对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等,自动化工具可以简化工作.Grunt生态系统非常庞大.你可以利 ...

  10. js_读【javascript面向对象编程指南】笔记

    写在前面: 工欲善其事,必先利其器.编程的器,是前人总结的经验,常言道站在巨人的肩膀上开发,往往比自己另辟蹊径容易的多.经验藏于书,故有书中自有颜如玉,书中自有黄金屋,我也一度认为读书要花费很多时间, ...