当前位置:网站首页>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
- 远程办公之如何推进跨部门项目协作 | 社区征文
- Kotlin学习快速入门(7)——扩展的妙用
- function overloading
- Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
- Bcvp developer community 2022 exclusive peripheral first bullet
- How to promote cross department project collaboration | community essay solicitation
- 大消费企业怎样做数字化转型?
- C language string inversion
- Kindeditor editor upload image ultra wide automatic compression -php code
猜你喜欢
建立自己的网站(23)
大变局!全国房价,跌破万元大关
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
Take you to API development by hand
CC2530 common registers for watchdog
人生还在迷茫?也许这些订阅号里有你需要的答案!
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
Life is still confused? Maybe these subscription numbers have the answers you need!
随机推荐
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
PHP online confusion encryption tutorial sharing + basically no solution
聊聊接口优化的几个方法
Informatics Olympiad all in one YBT 1175: divide by 13 | openjudge noi 1.13 27: divide by 13
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
Vs code plug-in korofileheader
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
kubernetes资源对象介绍及常用命令(三)
MySQL user management
[2. Basics of Delphi grammar] 2 Object Pascal data type
Atom QT 16_ audiorecorder
Kotlin学习快速入门(7)——扩展的妙用
ANOVA example
Execute script unrecognized \r
Unity notes unityxr simple to use
LeetCode 1656. Design ordered flow
[combinatorics] recursive equation (example 1 of recursive equation | list recursive equation)
Kotlin learning quick start (7) -- wonderful use of expansion