Codeforces Round #617 (Div. 3) E1String Coloring (easy vers&&E2. String Coloring (hard version)(贪心)

Here Insert Picture Description
Here Insert Picture Description
Meaning of the questions: initial string to you, want you to give each character coloring, the provisions of adjacent characters of different colors can be exchanged, there is a deposit does not ask you for coloring strings can become an orderly way?
Ideas: we follow the greedy idea, if front of me bigger than my character in front of me, I am not sure you want to swap, so you can traverse bigger than my character, to see their greatest numbers in front of me is how much, then plus 1.

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
char s[maxn];
int n,mx=0,ans[maxn],Max[maxn];
int main()
{
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=0;i<n;++i)
	{
		int cnt=0;
		for(int j=s[i]+1;j<='z';++j)
		cnt=max(cnt,Max[j]);
		cnt++;
		Max[s[i]]=cnt;
		ans[i]=cnt;
		mx=max(mx,cnt);
	}
	printf("%d\n",mx);
	for(int i=0;i<n;++i) printf("%d ",ans[i]);
}

E1String Coloring (easy version)
meaning of the questions and ideas: and almost above, the color is specified only two, we put the above change it, once mx 2 outputs no greater than on the line.

#include<bits/stdc++.h>
using namespace std;
const int maxn=200+1;
char s[maxn];
int n,mx=0,ans[maxn],Max[maxn];
int main()
{
	scanf("%d",&n);
	scanf("%s",s);
	for(int i=0;i<n;++i)
	{
		int cnt=0;
		for(int j=s[i]+1;j<='z';++j)
		cnt=max(cnt,Max[j]);
		cnt++;
		Max[s[i]]=cnt;
		ans[i]=cnt;
		mx=max(mx,cnt);
	}
	if(mx>2) puts("NO");
	else {
		puts("YES");
	for(int i=0;i<n;++i) printf("%d",ans[i]-1);
	}
}
Published 70 original articles · won praise 0 · Views 2437

Guess you like

Origin blog.csdn.net/qq_42479630/article/details/104187522