Luo Gu $ P1155 $ sort greedy + dual-stack bipartite graph matching

Positive Solutions: + bipartite graph matching piggy

Report problem solving:

Portal $ QwQ $

Knelt ,,, I thought I did almost $ NOIp $ Kang ,,, then find out how much of a ah do not really $ QAQ $

Kang then to lie $ QwQ $ title

First, consider the case if only one stack? If you say there is $ i <j <k $ and $ p_k <p_i <p_j $ on the $ GG $

Now we found two stacks? Becomes divided into two sequences so that the situation does not exist in this sequence? So obviously consider the bipartite graph coloring Aung $ QwQ $ staining failed Explanation if no solution chant.

Then sentenced finish no solution to consider the output of the program? This is clearly just greedy chant.

First, this stack allocation, is clearly the first priority to the front of the stack, so you can do together in the previous sentence no solution when you sweep from front to back, can be put into the first stack on the first stack .

Then the output of the program? On the order $ push $ chant, but because there is $ pop (1) $ superior to the $ push (2) $, so every $ push (2) $ before they have to $ pop (1 ) $ duck

Then gone? $ Over $

Kang specific codes lie $ QwQ $

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define t(i) edge[i].to
#define w(i) edge[i].wei
#define ri register int
#define rc register char
#define rb register bool
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
#define e(i,x) for(ri i=head[x];i;i=edge[i].nxt)

const int N=1000+10;
int n,p[N],mn[N],head[N],ed_cnt,col[N],pos=1;
struct ed{int to,nxt;}edge[N*N<<1];
stack<int>stck[2];

il int read()
{
    rc ch=gc;ri x=0;rb y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
il void ad(ri x,ri y){edge[++ed_cnt]=(ed){x,head[y]};head[y]=ed_cnt;edge[++ed_cnt]=(ed){y,head[x]};head[x]=ed_cnt;}
il void print(ri x)
{
    switch(x)
    {
        case 1:{printf("a ");break;}
        case 2:{printf("b ");break;}
        case 3:{printf("c ");break;}
        case 4:{printf("d ");break;}
    }
}
il bool pop(ri opt)
{
    if(!stck[opt].empty() && stck[opt].top()==pos)return print((opt+1)<<1),stck[opt].pop(),++pos,1;
    return 0;
}
il void push(ri x,ri opt)
{
    if(opt)while(pop(0));//printf("x=%d opt=%d\n",x,opt);
    while(!stck[opt].empty() && stck[opt].top()<x)if(!pop(opt))pop(!opt);
    if(opt)while(pop(0));stck[opt].push(x);print(opt<<1|1);
}

int main()
{
    n=read();rp(i,1,n)p[i]=read();mn[n+1]=n;my(i,n,1)mn[i]=min(mn[i+1],p[i]);rp(i,1,n)rp(j,i+1,n)if(mn[j+1]<p[i] && p[i]<p[j])ad(i,j),col[i]=col[j]=-1;
    rp(i,1,n)
        if(!(~col[i]))
        {
            queue<int>Q;Q.push(i);col[i]=0;
            while(!Q.empty())
            {
                ri nw=Q.front();Q.pop();
                e(i,nw){if(col[t(i)]==col[nw])return printf("0\n"),0;if(!(~col[t(i)]))Q.push(t(i)),col[t(i)]=!col[nw];}
            }
        }
    rp(i,1,n)push(p[i],col[i]);rb flg=1;while(flg){flg=0;while(pop(0))flg=1;while(pop(1))flg=1;}
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/lqsukida/p/11410011.html