Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created March 17, 2025 01:49
Show Gist options
  • Select an option

  • Save unilecs/8ab18a06ecff6e47d48402499c90ec75 to your computer and use it in GitHub Desktop.

Select an option

Save unilecs/8ab18a06ecff6e47d48402499c90ec75 to your computer and use it in GitHub Desktop.
Задача: Оптимальное заполнение мешков
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