1 条题解

  • 0
    @ 2021-6-15 1:39:36

    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.
    
    • 1

    信息

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