当前位置:网站首页>Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
2022-07-03 17:09:00 【Elucidation】
Twin City Inn
- First step : Determine whether double stack sorting can be done
To understand perceptually , We want the final output sequence to be Ascending , Obviously, the number in the stack should be guaranteed to be Descending Of ;
Excerpt from zjp_shadow The antithesis of
{1,2,3} This set of data can be put into the same stack , Just put it in and take it out immediately
If the bipartite graph is dyed successfully , Each number will get a color , Numbers of the same color must be in a stack
- The second step : Output the sorting of the minimum dictionary order
A more troublesome simulation , There are many situations that need special judgment
Because the order of different operations will affect the dictionary order , We should give priority to the entry and exit of the first stack , Consider the second stack , namely : Stack 2 Consider the stack before any operation of 1 Can you operate
At the same time, it's very important , The resulting sequence is required to be in ascending order :{1、2、3、4…}, We have to record which number should pop up every time , Don't play in the wrong order .
Code:
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define sca scanf
#define pri printf
#define ul (u << 1)
#define ur (u << 1 | 1)
#define forr(a,b,c) for(int a=b;a<=c;a++)
#define rfor(a,b,c) for(int a=b;a>=c;a--)
//[ Blog address ]:https://blog.csdn.net/weixin_51797626?t=1
using namespace std;
inline int read() {
int x = 0, f = 1, ch = getchar(); while (ch < '0' || ch>'9') {
if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }
inline void write(int x) {
if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int N = 1010, M = 400010, MM = N;
int INF = 0x3f3f3f3f, mod = 998244353;
ll LNF = 0x3f3f3f3f3f3f3f3f;
int n, m, k, T, S, D;
int g[N][N], qmin[N];
int a[N], color[N], top = 1;
stack<int> aa, cc;
bool st[N];
bool col(int x, int c) {
color[x] = c;
for (int j = 1; j <= n; j++)
if (g[x][j]) {
if (!color[j]) {
if (!col(j, 3 - c))return false;
}
else if (c == color[j])return false;
}
return true;
}
inline void popp(stack<int>& aa, int x) {
aa.pop();
if (x == 1)cout << " b";
else cout << " d";
top++;
}
inline bool check(stack<int>& aa) {
return aa.size() && aa.top() == top;// Judge whether the top of the stack is the current number of the bomb
}
void solve() {
cin >> n;
forr(i, 1, n)cin >> a[i];
qmin[n + 1] = n + 1;
rfor(i, n, 1)qmin[i] = min(a[i], qmin[i + 1]);// Preprocess a suffix min
forr(i, 1, n - 2)
forr(j, i + 1, n - 1)
if (a[i] < a[j] && qmin[j + 1] < a[i])// If and only if
g[i][j] = g[j][i] = 1;// We can judge i、j Cannot be the same color
forr(i, 1, n)
if (!color[i]) {
if (!col(i, 1)) {
// Dichotomous coloring
cout << 0;
return;
}
}
top = 1;
bool f = false;
forr(i, 1, n) {
if (color[i] == 1) {
while (aa.size() && aa.top() < a[i]) {
// If there is more than the number to be inserted in the current stack (a[i]) Small number
// Obviously, it will bounce a[i] until
// But not all these numbers are on the stack 1 in , So judge the bomb stack 1 Or stack 2, Bomb stack 1 Just pop the stack 1
if (check(aa))popp(aa, 1);
else popp(cc, 2);
}
aa.push(a[i]);
if (!f)f = true, cout << "a";
else cout << " a";
}
else {
// a key :
// Bomb stack 2 Before , We give priority to stack 1 I can play it all , This will ensure the minimum dictionary order
while (check(aa))popp(aa, 1);
while (cc.size() && cc.top() < a[i]) {
if (check(cc))popp(cc, 2);
else popp(aa, 1);
}
while (check(aa))popp(aa, 1);// And then again
// Anyway, stack 2 Do stack before any operation of 1 The operation of !
cc.push(a[i]);
if (!f)f = true, cout << "c";
else cout << " c";
}
}
// Finally, play all
while (aa.size()) {
if (check(aa))popp(aa, 1);
else popp(cc, 2);
}
while (cc.size())popp(cc, 2);
}
int main() {
cinios;
solve();
return 0;
}
/* */
边栏推荐
- CC2530 common registers for port interrupts
- [combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
- What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
- Kotlin learning quick start (7) -- wonderful use of expansion
- CC2530 common registers for timer 1
- Javescript variable declaration -- VaR, let, const
- 智慧之道(知行合一)
- RF Analyze Demo搭建 Step by Step
- mysql用户管理
- C语言字符串练习
猜你喜欢
图之深度优先搜索
The largest matrix (H) in a brush 143 monotone stack 84 histogram
CC2530 common registers for port initialization
One brush 145 force deduction hot question-2 sum of two numbers (m)
[try to hack] active detection and concealment technology
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
Mysql database DDL and DML
Bcvp developer community 2022 exclusive peripheral first bullet
Design e-commerce spike
One brush 149 force deduction hot question-10 regular expression matching (H)
随机推荐
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
27. Input 3 integers and output them in descending order. Pointer method is required.
Great changes! National housing prices fell below the 10000 yuan mark
Assembly instance analysis -- screen display in real mode
【Try to Hack】主动侦查隐藏技术
SWM32系列教程4-端口映射及串口应用
CC2530 common registers for serial communication
Kotlin学习快速入门(7)——扩展的妙用
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
The largest matrix (H) in a brush 143 monotone stack 84 histogram
网络安全web渗透技术
The way of wisdom (unity of knowledge and action)
CC2530 common registers
Execute script unrecognized \r
vs code 插件 koroFileHeader
CC2530 common registers for crystal oscillator settings
One brush 142 monotone stack next larger element II (m)
How SVN views modified file records
Design e-commerce spike
Rsync remote synchronization