Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

v.angelov92 avatar v.angelov92 8 Точки

03. Moving Target

Здравейте, реших задачата, но ми дава 70/100. Единият случай гърми, защото решението ми е над 16 MB. Може ли някой да даде насоки или решение как да сваля размера на програмата, както и да се получат верни отговори на останалите 2 случая. Благодаря за отделеното време.

Условие:

Problem 3. Moving Target

You are at the shooting gallery again and you need a program that helps you keep track of moving targets. On the first line, you will receive a sequence of targets with their integer values, split by a single space. Then, you will start receiving commands for manipulating the targets, until the "End" command. The commands are the following:

  • Shoot {index} {power}
    • Shoot the target at the index, if it exists by reducing its value by the given power (integer value).A target is considered shot when its value reaches 0
    • Remove the target, if it is shot.
  • Add {index} {value}
    • Insert a target with the received value at the received index, if it exist. If not, print: "Invalid placement!"
  • Strike {index} {radius}
    • Remove the target at the given index and the ones before and after it depending on the radius, if such exist. If any of the indices in the range is invalid print:

"Strike missed!" and skip this command.

 Example:  Strike 2 2

 

  •  
  •  
  • strikeIndex}
  •  
  •  

 

 

 

    • Print the sequence with targets in the following format:
      •  

Input / Constraints

  • On the first line you will receive the sequence of targets integer values [1-10000].
  • On the next lines, until the "End" will be receiving the command described above – strings.
  • There will never be a case when "Strike" command would empty the whole sequence.

Output

  • Print the appropriate message in case of "Strike" command if necessary.
  • In the end, print the sequence of targets in the format described above.

Examples

Input

Output

Comments

52 74 23 44 96 110

Shoot 5 10

Shoot 1 80

Strike 2 1

Add 22 3

End

Invalid placement!

52|100

The first command is "Shoot", so we reduce the target on index 5, which is valid, with the given power10.

Then we receive the same command but we need to reduce the target on the 1st index, with power 80. The value of this target is 74, so it is considered shot and we remove it.

Then we receive the "Strike" command on the 2nd index and we need to check if the range with radius 1 is valid:

52 23 44 96 100

And it is, so we remove the targets.

At last we receive the "Add" command, but the index is invalid so we print the appropriate message and in the end we have the following result:

52|100

47 55 85 78 99 20

Shoot 1 55

Shoot 8 15

Strike 2 3

Add 0 22

Add 2 40

Add 2 50

End

Strike missed!

22|47|50|40|85|78|99|20

 

 

Мое решение: https://pastebin.com/nu8MWVCU

Тагове:
0
Fundamentals of Programming (with C#) 05/09/2020 14:41:46
Axiomatik avatar Axiomatik 2422 Точки
Best Answer

The problem was in the Strike-method, which required checking if the remove-indexes were valid (instead of radius < targets.Count), because you still could receive invalid cases with wrong indexes but with a valid radius that was smaller than targets.Count.

Refactored code (100%):

        private static void StrikeMethod(string[] command, List<int> targets)
        {
            int index = int.Parse(command[1]);
            int value = int.Parse(command[2]);
            int radius = 1 + (value * 2);

            int radiusStart = index - value;
            int radiusEnd = index + value;
 

            if (radiusStart >= 0 && radiusEnd < targets.Count)
            {                
                targets.RemoveRange(radiusStart, radius);
            }
            else
            {
                Console.WriteLine($"Strike missed!");
            }
        }

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.