P1155 dual-stack Sort

Title Description

TomRecent research in an interesting sort of problem. As shown, the two stacks S1 and S2, Tomhoping to achieve the following four operations in ascending order of the input sequence.

Operating a

If the input sequence is not empty, the first element pushed onto the stack S1

Operation b

If the stack is not empty S1, S1 is the output sequence to pop the top element

Operation c

If the input sequence is not empty, the first element pushed onto the stack S2

Operation d

If the stack is not empty S2, the S2 output sequence to pop the top element

If a 1-n P are arranged such that a series of operations by the output sequence 1,2, ..., (n-1 ), Tomit is called a P "sorting arrangement may be a dual-stack." Such as (1,3,2,4) is a "collating sequence may be dual-stack", and (2,3,4,1) are not. The following diagram describes the operation of a sequence (1,3,2,4) sorting: <a, c, c, b, a, d, d, b>

Of course, such an operation may be several sequences, for the cases (1,3,2,4), <a, c , c, b, a, d, d, b> is another possible sequence of operations. TomI want to know what is the smallest among the lexicographical sequence of operations yes.

Input Format

The first row is an integer n.

The second row has a space-separated positive integer of n to form a 1-n are arranged.

Output Format

A total line, if the input arrangement is not "sorting arrangement may be a dual-stack", the output digital 0 ; otherwise, the output lexicographically smallest operation sequence, each separated by a space between the two operations, the end of the line with no spaces.

Sample input and output

Input # 1
4
1 3 2 4
Output # 1
a b a a b b a b
Input # 2
4
2 3 4 1
Output # 2
0
Input # 3
3
2 3 1
Output # 3
a c a b b d

Description / Tips

30% of the data satisfies: n≤10

50% of the data meet: n≤50

100% data satisfies: n≤1000

Thinking

A simple recursive algorithm.

In fact, this topic does not need a bipartite graph, do not need to search.

As long as thinking if we can sort the elements required to meet the nature, then you can add greed to A stack.

Barley

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=1010;

char ans[2*N];
int in[N],a[N],b[N];
int ad,bd,k,n,cnt,asd;

bool check(int k) {
	if(!bd)
		return 1;
	int i,j;
	for(i=k+1; i<=n; i++)
		if(in[i]>in[k]&&in[i]>b[bd])
			break;
	for(j=i+1; j<=n; j++)
		if(in[j]<in[k])
			return false;
	return true;
}

int main () {
	bool able=1;
	int at=1;
	scanf("%d",&n);
	a[0]=b[0]=1e4,asd=1;
	for(int i=1; i<=n; i++)
		scanf("%d",&in[i]);
	for(int i=1; i<=(n<<1); i++) {
		if(a[ad]==asd) {
			ad--;
			asd++;
			ans[++cnt]='b';
			continue;
		}
		if(b[bd]==asd) {
			bd--;
			asd++;
			ans[++cnt]='d';
			continue;
		}
		if(at<=n&&in[at]<a[ad]&&check(at)) {
			a[++ad]=in[at];
			ans[++cnt]='a';
			at++;
			continue;
		}
		if(at<=n&&in[at]<b[bd]) {
			b[++bd]=in[at];
			ans[++cnt]='c';
			at++;
			continue;
		}
		able=0;
		break;
	}
	if(able)
		for(int i=1; i<=cnt; i++)
			putchar(ans[i]),putchar(' ');
	else
		printf("0");
	printf("\n");
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11449468.html