Codeforces Round #582 (Div. 3) F. Unstable String Sort

Portal

Meaning of the questions:

You need to output a sequence of length n characters (lowercase letters), and the string contains at least k different characters. Further subject is the requirement: to give you two sequences of length p and q, s sequence is present in the character set

Then there will be s [Pi] <= s [P (i + 1)] (i <p)    

     s [Qi] <= s [Q (i + 1)] (i <q) you can find out if you meet these conditions string s, s and YES outputs, or output NO

This will be a non-decreasing string

 

answer:

 Because the final result is a non-decreasing string, recognition of the entire string s, there is s [i] <= s [i + 1] (0 <= i <strlen (s) -1)

So this is the case only if Pi> P (i + 1) or Qi> when Q (i + 1), will affect the character string in [P (i + 1), Pi] or [Q (i + 1) , Qi] values, character interval within this period will certainly be equal

Because the string s requirement to have at least k different characters on the subject, so they do not encounter such a situation above, the position of each character is bigger than a character (we are here is to use a, then b, etc. Wait)

So we only need to pay attention Pi> P (i + 1) or Qi> Q (i + 1) of this section will be able to

Thought Positive Solutions here out, find the smallest jjj each P [i], such that P [j] is located P [i] in the Q of the rear (for convenience, that i itself is a legitimate jjj), then P [j..i] must be the same letter. If the two intersect with the letters section, then merge the whole period must be the same letter, and therefore processed all the letters section and then sweep again, the intersection with the letters section merge. Finally, see if there are k segments, greedy to let different segments with different letters letters to the complexity of O (n)

 

If you use the z characters, but not yet finished sequence s, then all the rest position output z

If the subject aside to see you output a sequence of length n, and if this sequence contains only one character, then in addition to the k does not satisfy the requirements of different character, the other conditions are met

 

Another method, P [i]] to P [i-1] even edge, Q [i] is Q [i-1] even side edge (x, Y) to represent the letters on the x position must not be less than y position, all strongly connected components of a position must be the same letter again can tarjan

 

I had to write one, but too violent, T the T_T

T Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=2e5+5;
 9 const int INF=0x3f3f3f3f;
10 typedef long long ll;
11 //vector<int>w[maxn];
12 int a[maxn],b[maxn],v[maxn],w[maxn];
13 int main()
14 {
15     int n,k;
16     //memset(v,INF,sizeof(v));
17     scanf("%d%d",&n,&k);
18     for(int i=1; i<=n; ++i)
19     {
20         scanf("%d",&a[i]);
21     }
22     for(int i=1; i<=n; ++i)
23         scanf("%d",&b[i]);
24     for(int i=1; i<n; ++i)
25     {
26         if(a[i+1]<a[i])
27         {
28             v[a[i+1]]=max(v[a[i+1]],a[i]);
29         }
30     }
31     for(int i=1; i<n; ++i)
32     {
33         if(b[i+1]<b[i])
34         {
35             v[b[i+1]]=max(v[b[i+1]],b[i]);
36         }
37     }
38     int ans=0,flag=0;
39     for(int i=1; i<=n; ++i)
40     {
41         if(w[i]==0)
42         {
43             if(v[i]==0)
44             {
45                 w[i]='a'+ans++;
46             }
47             else
48             {
49                 for(int j=i; j<=v[i]; ++j)
50                     w[j]='a'+ans;
51                 ans++;
52                 //i=v[i];
53             }
54         }
55         else
56         {
57             if(v[i]==0)
58             {
59                 continue;
60             }
61             else
62             {
63                 for(int j=i+1; j<=v[i]; ++j)
64                     w[j]=w[i];
65                 //i=v[i];
66             }
67         }
68     }
69     if(ans<k) flag=1;
70     if(!flag)
71     {
72         printf("YES\n");
73         for(int i=1; i<=n; ++i)
74         {
75             if(i!=n) printf("%c",w[i]);
76             else printf("%c\n",w[i]);
77         }
78     }
79     else
80     {
81         printf("NO\n");
82     }
83     return 0;
84 }
View Code

 

Correct answer:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<queue>
 5 #include<cmath>
 6 #include<string.h>
 7 #include<set>
 8 #define LL long long
 9 using namespace std;
10 LL read( )
11 {
12   LL sum=0;char c=getchar( );bool f=0;
13   while(c<'0' || c>'9') {if(c=='-') f=1;c=getchar( );}
14   while(c>='0' && c<='9') {sum=sum*10+c-'0';c=getchar( );}
15   if(f) return -sum;
16   return sum;
17 }
18 const int N=200005;
19 int n,m,P[N],Q[N],pos[N],L[N],bel[N];
20 char ans[N];
21 int main( )
22 {
23   int i,j,k;
24   n=read( );m=read( );
25   for(i=1;i<=n;i++) P[i]=read( ),pos[P[i]]=i;
26   for(i=1;i<=n;i++) Q[i]=read( );
27   for(k=n,i=n;i>=1;i--) j=pos[Q[i]],k=min(k,j),L[j]=k;
28   for(j=1,k=n,i=n;i>=1;i--)
29     {
30       k=min(k,L[i]);bel[i]=j;
31       if(k==i) j++;
32     }
33   if(bel[1]<m) {puts("NO");return 0;}
34   puts("YES");
35   for(j=-1,i=1;i<=n;i++)
36     {
37       if(bel[i]!=bel[i-1]) j++;
38       ans[P[i]]='a'+min(25,j);
39     }
40   for(i=1;i<=n;i++) printf("%c",ans[i]);
41   return 0;
42 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11519274.html