双栈排序

思路:

  二分图染+模拟;

代码:

#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. SELECT LOCK IN SHARE MODE and FOR UPDATE

    Baronwrote nice article comparing locking hints in MySQL and SQL Server. In MySQL/Innodb LOCK IN SHA ...

  2. oracle 国外网站【转载】

    [转自]:http://www.2cto.com/database/201406/306615.html 1. http://www.oratechinfo.co.uk/ http://www.ora ...

  3. python---aiohttp的使用

    1.aiohttp的简单使用(配合asyncio模块) import asyncio,aiohttp async def fetch_async(url): print(url) async with ...

  4. Spring 学习笔记 整合 Struts2

           Struts2与Spring整合后,可以使用Spring的配置文件applicationContext.xml来描述依赖关系,在Struts2的配置文件struts.xml来使用Spri ...

  5. 解决gitlab关闭登录选项问题

    1.连接资料库      mysql -uroot -p   2.use gitlabhq_production; 3.进入后,输入下面语句UPDATE application_settings se ...

  6. java与C#的基础语法区别--持续更新

    1.判断字符串是否相等 java : equals()比较的是对象的内容(区分字母的大小写格式),但是如果使用“==”比较两个对象时,比较的是两个对象的内存地址,所以不相等.即使它们内容相等,但是不同 ...

  7. bzoj 2705: [SDOI2012]Longge的问题——欧拉定理

    Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...

  8. bzoj 1594: [Usaco2008 Jan]猜数游戏——二分+线段树

    Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在牛棚后面摆N(1 <= N<= 1,000,00 ...

  9. Achain 钱包部署

    官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ...

  10. dot.js使用心得

    一.dot.js介绍 最近用到的数据模板引擎有很多,今天讲的doT.js也是其中一种. doT.js的特点是体积小,速度快,并且不依赖其他插件. 官网下载:http://olado.github.io ...