Created
March 17, 2025 01:49
-
-
Save unilecs/8ab18a06ecff6e47d48402499c90ec75 to your computer and use it in GitHub Desktop.
Задача: Оптимальное заполнение мешков
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| public class Program | |
| { | |
| public static int MaxBags(int[] capacity, int[] rocks, int additionalRocks) { | |
| for (int i = 0; i < capacity.Length; i++) | |
| { | |
| capacity[i] -= rocks[i]; | |
| } | |
| Array.Sort(capacity); | |
| int result = 0; | |
| for (int i = 0; i < capacity.Length; i++) | |
| { | |
| if (capacity[i] > 0 && additionalRocks == 0) | |
| return result; | |
| if (capacity[i] == 0) | |
| { | |
| result++; | |
| } | |
| else if (capacity[i] <= additionalRocks) | |
| { | |
| additionalRocks -= capacity[i]; | |
| result++; | |
| } | |
| } | |
| return result; | |
| } | |
| public static void Main() | |
| { | |
| Console.WriteLine("UniLecs"); | |
| // tests | |
| var capacity = new int[] { 2, 3, 4, 5 }; | |
| var rocks = new int[] { 1, 2, 4, 4 }; | |
| Console.WriteLine(MaxBags(capacity, rocks, 2).ToString()); // 3 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment