791C - Bear and Different Names - CodeForces Solution


constructive algorithms greedy *1500

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
noticed = list(map(lambda x: x == "YES", input().split()))
names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'Ba', 'Bb', 'Bc', 'Bd', 'Be', 'Bf', 'Bg', 'Bh', 'Bi', 'Bj',
         'Ca', 'Cb', 'Cc', 'Cd', 'Ce', 'Cf', 'Cg', 'Ch', 'Ci', 'Cj', 'Da', 'Db', 'Dc', 'Dd', 'De', 'Df', 'Dg', 'Dh',
         'Di', 'Dj', 'Ea', 'Eb', 'Ec', 'Ed', 'Ee', 'Ef', 'Eg', 'Eh', 'Ei', 'Ej', 'Fa', 'Fb', 'Fc', 'Fd', 'Fe', 'Ff',
         'Fg', 'Fh', 'Fi', 'Fj', 'Ga', 'Gb', 'Gc', 'Gd', 'Ge', 'Gf', 'Gg', 'Gh', 'Gi', 'Gj', 'Ha', 'Hb', 'Hc', 'Hd',
         'He', 'Hf', 'Hg', 'Hh', 'Hi', 'Hj', 'Ia', 'Ib', 'Ic', 'Id', 'Ie', 'If', 'Ig', 'Ih', 'Ii', 'Ij', 'Ja', 'Jb',
         'Jc', 'Jd', 'Je', 'Jf', 'Jg', 'Jh', 'Ji', 'Jj']
solider = [names[i] for i in range(n)]

t = n
for i in range(len(noticed) - 1, -1, -1):
    if not noticed[i]:
        solider[i] = solider[i + k - 1]
    else:
        t += 1
        solider[i] = names[t]

print(*solider)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int read() {
	int x = 0,f = 0;char ch = getchar();
	while(!isdigit(ch)) {if(ch == '-')f = 1;ch = getchar();}
	while(isdigit(ch)) {x = x * 10 + ch - '0';ch = getchar();}
	return f ? -x : x;
}
const int N = 1e6 + 10;
int fa[N],vis[N];
int main() {
	int n = read(),m = read();
	for(int i = 1;i <= n - m + 1;i++) {
		string a;
		cin >> a;
		if(a == "NO") vis[i] = 0;
		else vis[i] = 1;
	}
	for(int i = 1;i <= n;i++) fa[i] = i;
	for(int i = n - m + 1;i >= 1;i--) {
		if(vis[i] == 0) {
			fa[i] = fa[i + m - 1];
		}
	}
	for(int i = 1;i <= n;i++) {
		string x = "A";
		int p = fa[i];
		while(p) {
			x += 'a' + p % 26;
			p /= 26;
		}
		cout << x << " ";
	}
}
	  		 	 	   					 					 		  	 	


Comments

Submit
0 Comments
More Questions

1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold