Блог пользователя redsexx

Автор redsexx, история, 8 месяцев назад, По-английски

https://codeforces.me/contest/2209/submission/367971487 367971487

hi everyone,

i tried solving this problem but my logic is failing on some cases. i'm not looking for a completely different solution, i just want to understand what is wrong with my reasoning.

my idea was the following.

first i find the color with the maximum count among r, g and b.

the reason is simple: the color with the largest frequency is the hardest one to place without breaking the constraints. so it feels natural to place it first. if we space the maximum element properly, it automatically creates room for the other two colors.

so if, for example, r is the largest, i start by placing all r at even indices (0, 2, 4, ...). this spreads them out and avoids adjacency.

then i start filling the odd indices with the second largest color, and after that with the third color.

so the structure becomes something like:

R _ R _ R _ R

and the blanks are filled with the other colors.

the idea was that this spacing should prevent: same colors being adjacent same colors appearing at distance 3

after building this temporary array, i convert it into the final string. if some elements of the third color are still left, i try to prepend or append them carefully so that adjacency is not violated.

i repeat the same logic for all three cases:

when r is maximum when g is maximum when b is maximum

so the main logic is:

place the most frequent color first (to spread it out) fill the gaps with the other colors attach any remaining characters carefully at the ends

i feel like the reasoning should work, but clearly something in this construction is wrong since some tests fail.

would appreciate if someone could point out where the logical flaw is.

thanks

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
8 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Lets consider a sample input R=2,G=2,B=2, thats the fisrt case in your code (R>=G>=B).

The structure is "R_R". Then u fill the blank with G, so u get "RGR". Then, cause B is not 0, then u add B at begin and end. Finally u get "BRGRB", the length is 5. One possible answer can be "RGBGBR", the length is 6.

I think the "R _ R _ R _ R" structure is not necessary.

»
8 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

just try placing the one with the largest count at the end, and use the constraints to check before you place. if r is maximum, your still going to end up with something like R_R_R, but if its more close, its going to be something like RGBGBR/

»
8 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think your approach is somewhat correct : but your missing a key piece

Lets say R is the largest : R = 9

B = 7 , G = 6

it does not make sense to place R_R_R_R... 9 times

but it does make sense to place R_R_R ( few times till we reach a equilibrium where all the colors remaining are same R=G=B ). RBRGRBR ( now R = 5 , B = 5 , G = 5 , start cycling the rest from here ).

After this you start cycling . This was my solution which got accepted , used some AI for implementation : 367818494

»
8 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This is one of those problems where you just need to write out a bunch of cases until you see the pattern.