Codevs1170 双栈排序
2015-10-29 15:05
435 查看
思路:看题解看得目瞪口呆,真的不想说什么了。
代码如下:
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<stack> #include<algorithm> #include<vector> using namespace std; const int maxn=1005; int n,col[maxn]; int a[maxn],b[maxn]; vector<int> g[maxn]; void init() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&a[i]); b[n+1]=1000000000; for (int i=n;i>=1;--i) b[i]=min(b[i+1],a[i]); } bool paint(int u) { for (int i=0;i<g[u].size();++i) { if (!col[g[u][i]]) { col[g[u][i]]=3-col[u]; paint(g[u][i]); } else if (col[g[u][i]]==col[u]) return false; } return true; } stack<int> s[3]; int main() { freopen("stack.in","r",stdin); freopen("stack.out","w",stdout); init(); for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j) { if (a[i]<a[j] && a[i]>b[j+1]) { g[i].push_back(j); g[j].push_back(i); } } memset(col,0,sizeof(col)); for (int i=1;i<=n;++i) if (!col[i]) { col[i]=1; if (!paint(i)) { puts("0"); return 0; } } int now=1; for (int i=1;i<=n;++i) { if (col[i]==1) printf("a "); else printf("c "); s[col[i]].push(a[i]); while ( (!s[1].empty() && s[1].top()==now) || (!s[2].empty() && s[2].top()==now)) { if (!s[1].empty() && s[1].top()==now) { printf("b "); s[1].pop(); } if (!s[2].empty() && s[2].top()==now) { printf("d "); s[2].pop(); } now++; } } return 0; }
相关文章推荐
- 例题5.5 圆桌骑士 LA3523
- 编程算法 - 二分图判定 代码(C)
- poj 2942 Knights of the Round Table 补图+点双连通分量+判定二分图
- 二分图判定
- 二分图判定
- 【NWERC2015】【BZOJ4427】Cleaning Pipes清理管道
- UVAlive 3523 Knights of the Round Table [点双连通分量] [Tarjan]
- NOIP2010 Codevs 1069 关押罪犯 [并查集] [二分图判定]
- HDU 2444 二分图判定+最大匹配
- Linux Swap的基本原理
- simrank
- Eclipse去掉jquery的红叉
- iOS 网络判断
- Spring/Hibernate 应用性能优化的7种方法
- [傅里叶变换及其应用学习笔记] 三. 复习,将一般周期函数表示成简单周期函数和
- 深刻理解立即执行函数
- JavaScript操作Cookie笔记
- Android基础之Activity、Fragment、View的生命周期
- Go语言基本的语法和内置数据类型初探
- Redis和Memcached的比较