题目:https://www.luogu.org/problemnew/show/P1155

思路...

看博客:https://www.cnblogs.com/Narh/p/9213825.html

二分图什么的,字典序什么的,考场上怎么想出来...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const maxn=;
int n,a[maxn],f[maxn],col[maxn],head[maxn],ct,pos[maxn];
bool flag;
struct N{
int to,next;
N(int t=,int n=):to(t),next(n) {}
}edge[maxn*maxn];
void add(int x,int y){edge[++ct]=N(y,head[x]); head[x]=ct;}
bool init()
{
f[n]=n+;//!
for(int i=n-;i;i--)
{
f[i]=min(a[i+],f[i+]);
for(int j=i+;j<=n;j++)
if(a[j]>a[i]&&f[j]<a[i])add(i,j),add(j,i);
}
return ;
}
void dfs(int x)
{
for(int i=head[x];i;i=edge[i].next)
{
int u=edge[i].to;
if(col[u]==col[x]){flag=; return;}
else if(!col[u])col[u]=(col[x]^),dfs(u);
if(flag)return;
}
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
init();
for(int i=;i<=n;i++)
if(!col[i])
{
col[i]=;
dfs(i);
if(flag){printf("0\n"); return ;}
}
int nw=;
for(int i=;i<=n;i++)
{
pos[a[i]]=col[i];
if(col[i]==)printf("a ");
else printf("c ");
while(pos[nw])
{
if(pos[nw]==)printf("b ");
else printf("d ");
nw++;
}
}
return ;
}

洛谷P1155 双栈排序——思路题的更多相关文章

  1. 洛谷P1155 双栈排序题解(图论模型转换+二分图染色+栈)

    洛谷P1155 双栈排序题解(图论模型转换+二分图染色+栈) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1311990 原题地址:洛谷P1155 双栈排序 ...

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

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

  3. 洛谷——P1155 双栈排序

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

  4. 洛谷P1155 双栈排序

    这题什么毒瘤......之前看一直没思路,然后心说写个暴搜看能有多少分,然后就A了??! 题意:给你一个n排列,求它们能不能通过双栈来完成排序.如果能输出最小字典序方案. [update]这里面加了一 ...

  5. 洛谷 P1155 双栈排序

    题面 解题思路 这道题乍一看还以为是个模拟..怒写一发30分(noip提高组t4有模拟吗?). 其实很好hack,如 10 10 2 8 1 7 9 3 4 5 6 按模拟的思路,应该是10入第一个栈 ...

  6. 洛谷$P1155$ 双栈排序 贪心+二分图匹配

    正解:贪心+二分图匹配 解题报告: 传送门$QwQ$ 跪了,,,我本来以为我$NOIp$做得差不多了,,,然后康了一眼发现没做多少啊其实$QAQ$ 然后来康题趴$QwQ$ 首先考虑如果只有一个栈的情况 ...

  7. 洛谷P1155 双栈排序(贪心)

    题意 题目链接 Sol 首先不难想到一种贪心策略:能弹则弹,优先放A 然后xjb写了写发现只有\(40\),原因是存在需要决策的情况 比如 \(A = {10}\) \(B = {8}\) 现在进来一 ...

  8. P1155 双栈排序(二分图染色)

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

  9. P1155 双栈排序

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

随机推荐

  1. 杭电 5053 the Sum of Cube(求区间内的立方和)打表法

    Description A range is given, the begin and the end are both integers. You should sum the cube of al ...

  2. URAL 1297 求最长回文字符串

    有种简单的方法,数组从左到右扫一遍,每次以当前的点为中心,只要左右相等就往左右走,这算出来的回文字符串是奇数长度的 还有偶数长度的回文字符串就是以当前扫到的点和它左边的点作为中心,然后往左右扫 这是O ...

  3. 592. Fraction Addition and Subtraction

    Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...

  4. zoj 2676 dinic模板求实型最小割(可做dinic模板)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #inc ...

  5. [bzoj3238][Ahoi2013]差异_后缀数组_单调栈

    差异 bzoj-3238 Ahoi-2013 题目大意:求任意两个后缀之间的$LCP$的和. 注释:$1\le length \le 5\cdot 10^5$. 想法: 两个后缀之间的$LCP$和显然 ...

  6. Subsets and Subsets II (回溯,DFS,组合问题)

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  7. MongoDB学习day01--非关系型数据库

    1.数据库和文件的主要区别: 1.1数据库有数据库表/行和列的概念,让我们存储操作数据方便 1.2数据库提供了方便的接口,让java.php..net.nodejs很方便的实现增删改查 2.NoSQL ...

  8. Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    在操作hibernate数据库时,调用saveOrUpdate方法进行更新保存对象时, (1)ID为null时执行SAVE,但是前端jsp通过<input type="hidden&q ...

  9. iText输出中文的三种字体选择方式

    1.使用iTextAsian.jar中的字体    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",Bas ...

  10. hdu5371 Hotaru&#39;s problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...