1 条题解

  • 0
    @ 2025-4-7 21:38:05

    C++ :

    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <string>
    using namespace std;
    
    const int N = 1007;
    int a[N], c[N], Afterwards[N], n;
    stack<int> s1, s2;
    bool G[N][N];
    
    bool Color(int x, int Colour)
    {
    	c[x] = Colour;
    	for (int i = 1; i <= n; ++i)
    		if (G[x][i])
    			if (c[x] == c[i]) return false;
    			else if (!c[i] && !Color(i, -Colour)) return false;
    	return true;
    }
    
    int main()
    {
    	int i, j, k, Top = 1;
        string s = "";
    	
    	cin >> n;
    	for (i = 1; i <= n; ++i) cin >> a[i];
    	copy(a + 1, a + n + 1, Afterwards + 1);
    	sort(Afterwards + 1, Afterwards + n + 1);
    	
    	for (i = 1; i <= n; ++i)
    		for (j = i + 1; j <= n; ++j)
    			if (a[i] < a[j])
    				for (k = j + 1; k <= n; ++k)
    					if (a[k] < a[i]) G[i][j] = G[j][i] = true;
    	
    	for (i = 1; i <= n; ++i)
    		if (!c[i] && !Color(i, 1)) {cout << 0 << endl; return 0;}
    	for (i = 1; i <= n; ++i)
    		if (c[i] == 1)
    		{
    			s += "a "; s1.push(a[i]);
    			while (!s1.empty() && s1.top() == Afterwards[Top]) {s += "b "; s1.pop(); ++Top;}
    		}
    		else {
    			s += "c "; s2.push(a[i]);
    			while (!s2.empty() && s2.top() == Afterwards[Top]) {s += "d "; s2.pop(); ++Top;}
    		}
    		while (!s1.empty() || !s2.empty())
    			if (!s1.empty() && s1.top() == Afterwards[Top]) {s += "b "; s1.pop(); ++Top;}
    			else if (!s2.empty() && s2.top() == Afterwards[Top]) {s += "d "; s2.pop(); ++Top;}
    	cout << s.substr(0, s.size() - 1) << endl;
    	
    	return 0;
    }
    
    

    Pascal :

    var
     n,i,j,st1,st2,next_num,min,t:longint;
     q1,color,s1,s2:array[0..1500] of longint;
     map:array[1..1500,1..1500] of boolean;
     vis:array[1..1500] of boolean;
     ans:array[1..3000] of char;
    procedure print;
    begin
     writeln(0);
     halt;
    end;
    procedure dfs(x,nowcolor:longint);
    var
     i:longint;
    begin
     vis[x]:=true;
     color[x]:=nowcolor;
     for i:=1 to n do
      if map[x,i] then begin
       if not vis[i] then dfs(i,3-nowcolor)
       else if color[i]<>3-nowcolor then print;
      end;
    end;
    begin
     readln(n);
     for i:=1 to n do read(q1[i]);
     readln;
     min:=maxlongint;
     for i:=n downto 1 do begin
      for j:=1 to i-1 do if (q1[j]<q1[i]) and (min<q1[j]) then begin
       map[q1[i],q1[j]]:=true;
       map[q1[j],q1[i]]:=true;
      end;
      if q1[i]<min then min:=q1[i];
     end;
     fillchar(vis,sizeof(vis),0);
     for i:=1 to n do if not vis[q1[i]] then dfs(q1[i],1);
     i:=1;
     next_num:=1;
     t:=q1[i];
     st1:=0;
     st2:=0;
     j:=0;
     while (i<=n) or (next_num<=n) do begin
      if (i<=n) and (color[t]=1) and ((st1=0) or (t<s1[st1])) then begin
       inc(j);
       ans[j]:='a';
       inc(st1);
       s1[st1]:=t;
       inc(i);
       if i<=n then t:=q1[i] else t:=0;
       continue;
      end;
      if (st1>0) and (s1[st1]=next_num) then begin
       inc(j);
       ans[j]:='b';
       dec(st1);
       inc(next_num);
       continue;
      end;
      if (i<=n) and (color[t]=2) and ((st2=0) or (t<s2[st2])) then begin
       inc(j);
       ans[j]:='c';
       inc(st2);
       s2[st2]:=t;
       inc(i);
       if i<=n then t:=q1[i] else t:=0;
       continue;
      end;
      if (st2>0) and (s2[st2]=next_num) then begin
       inc(j);
       ans[j]:='d';
       dec(st2);
       inc(next_num);
       continue;
      end;
     end;
     for i:=1 to j-1 do write(ans[i],' ');
     writeln(ans[j]);
    end.
    

    Java :

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Stack;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            int size = sc.nextInt();
            List<Integer> stackList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                stackList.add(sc.nextInt());
            }
    
            Stack<Integer> stack1 = new Stack<>();
            Stack<Integer> stack2 = new Stack<>();
            List<Integer> result = new Stack<>();
            List<String> operation = new ArrayList<>();
            int i = 0;
    
            for (int j = i; j < size; j++) {
    //            int k = i;
                // 一直丢进栈直到找出最小
                while (i<size&&stackList.get(i) != result.size()+1) {
                    // 丢到1中有个必要条件就是2中的数要大于1中两个数之间的最小数
                    boolean canPush1 = stack2.empty()||stackList.get(i)+1<stack2.get(stack2.size()-1)||stackList.get(i)+1==stack1.get(stack1.size()-1);
                    if (stack1.size() == 0 || (stack1.get(stack1.size() - 1) >= stackList.get(i)&&canPush1)) {
                        stack1.push(stackList.get(i));
                        operation.add("a");
    //                    System.out.print("a ");
                        i++;
                    } else if (stack2.size() == 0 || stack2.get(stack2.size() - 1) >= stackList.get(i)) {
                        stack2.push(stackList.get(i));
                        operation.add("c");
    //                    System.out.print("c ");
                        i++;
                    } else {
                        System.out.println("0");
                        return;
                    }
                }
                if (i<size) {
                    stack1.push(stackList.get(i++));
                    operation.add("a");
                    // System.out.print("a ");
    
                }
                // 如果找的到下一个数字则推出,如果找不到就结束继续
                while (!stack1.empty() || !stack2.empty()) {
                    if (!stack1.empty()&&(result.size() == 0 || stack1.get(stack1.size() - 1) == result.get(result.size()-1)+1)) {
                        result.add(stack1.pop());
                        operation.add("b");
    //                    System.out.print("b ");
    //                System.out.print("b");
                    } else if (!stack2.empty()&&stack2.get(stack2.size() - 1) == result.get(result.size()-1)+1) {
                        result.add(stack2.pop());
                        operation.add("d");
    //                    System.out.print("d ");
    //                System.out.print("d");
                    } else{
                        break;
                    }
                }
            }
    
            for (i = 0; i < operation.size(); i++) {
                if (i < operation.size() - 1) {
                    System.out.print(operation.get(i) + " ");
                } else {
                    System.out.print(operation.get(i));
                }
            }
    
    
        }
    }
    
    
    • 1

    信息

    ID
    1896
    时间
    1000ms
    内存
    50MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者