AC日记——双栈排序 洛谷 P1155
思路:
二分图染+模拟;
代码:
#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的更多相关文章
- AC日记——[SDOI2015]星际战争 洛谷 P3324
题目描述 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战. 在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai.当一个巨型机器人的装甲值 ...
- AC日记——联合权值 洛谷 P1351
题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...
- AC日记——I Hate It 洛谷 P1531
题目背景 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 题目描述 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的 ...
- AC日记——神奇的幻方 洛谷 P2615(大模拟)
题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. ...
- AC日记——[CQOI2009]DANCE跳舞 洛谷 P3153
[CQOI2009]DANCE跳舞 思路: 二分+最大流: 代码: #include <cstdio> #include <cstring> #include <iost ...
- AC日记——松江1843路 洛谷七月月赛
松江1843路 思路: 三分: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #define ...
- AC日记——严酷的训练 洛谷 P2430
严酷的训练 思路: 背包: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 5005 int n,m,bi[m ...
- AC日记——[SDOI2010]大陆争霸 洛谷 P3690
[SDOI2010]大陆争霸 思路: dijkstra模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn ...
- AC日记——小魔女帕琪 洛谷 P3802
小魔女帕琪 思路: 概率公式计算: 代码: #include <bits/stdc++.h> using namespace std; ],sig; int main() { ;i< ...
随机推荐
- 背景建模技术(六):帧处理(FrameProcessor)模块
前面几篇文章简单介绍了BgsLibrary的入口函数.视频分析和视频捕获模块,本文将简单介绍帧处理模块,即对每一帧进行处理的函数,也就是真正调用背景建模算法的接口处. 下面贴出源码供大家分析: #in ...
- ACE主动对象模式(2)
转载于:http://www.cnblogs.com/TianFang/archive/2006/12/12/590153.html 我们简单的介绍了一下ACE主动对象实现方式,同时提出了两个问题: ...
- STL之六:map/multimap用法详解
转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...
- POJ3026:Borg Maze (最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18644 Accepted: 5990 题目链接:h ...
- Fixed: The Windows Process Activation Service service terminated with the following error: The system cannot find the file specified
I'm not yet clear what I did, but I'm blogging it so it can be found if someone else has this issue. ...
- js处理时间的那些事
我们在实际需求中一般需要对时间进行相应的出来,比如:对时间串的拆分显示,两个时间差的求值显示等. 时间拆分: 一般对于这种处理我们使用正则表示式: 正则表达式拆分时间: var date = data ...
- windows下codeblocks报错undefined reference to `WSAStartup@8'|
Windows下C++Socket编程,调用WSAStartup函数报错:undefined reference to `WSAStartup@8'| 本人使用的是Codeblocks MinGW M ...
- [洛谷P1707] 刷题比赛
洛谷题目连接:刷题比赛 题目背景 nodgd是一个喜欢写程序的同学,前不久洛谷OJ横空出世,nodgd同学当然第一时间来到洛谷OJ刷题.于是发生了一系列有趣的事情,他就打算用这些事情来出题恶心大家-- ...
- Ubuntu12.04 安装nginx和mongo过程
1.安装php和php-cgi apt-get install php5 php5-cgi 2.安装 nginx apt-get install nginx 3.安装 MongoDB apt-get ...
- jQuery日历签到插件
插件比较简单,先来看DEMO吧,http://codepen.io/jonechen/pen/bZWdXq: CSS部分: *{margin:0;padding:0;font:14px/1.8 &qu ...