Ejemplo n.º 1
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(n, m) = inputStream.ReadValue <int, int>();
            var edges = new HashSet <Edge>();

            for (int i = 0; i < m; i++)
            {
                var(a, b) = inputStream.ReadValue <int, int>();
                a--;
                b--;
                edges.Add(new Edge(a, b));
                edges.Add(new Edge(b, a));
            }

            var orders = Enumerable.Range(1, n - 1);
            var count  = 0;

            foreach (var order in PermutationAlgorithms.GetPermutations(orders))
            {
                if (Check(order.Span, edges))
                {
                    count++;
                }
            }

            yield return(count);
        }
Ejemplo n.º 2
0
        public void PermutationEmptyTest()
        {
            var input    = Enumerable.Empty <int>();
            var expected = new [] { new int[] { } };

            var actual = PermutationAlgorithms.GetPermutations(input, false);

            AssertSequentialEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void Permutation2WithDuplicationTest()
        {
            var input    = new int[] { 1, 1 };
            var expected = new[] { new int[] { 1, 1 } };

            var actual = PermutationAlgorithms.GetPermutations(input, false);

            AssertSequentialEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void Permutation2ReversedTest()
        {
            var input    = Enumerable.Range(1, 2).Reverse();
            var expected = new[] { new int[] { 1, 2 }, new int[] { 2, 1 } };

            var actual = PermutationAlgorithms.GetPermutations(input, false);

            AssertSequentialEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public override void Solve(IOManager io)
        {
            var camels = io.ReadInt();
            var parts  = io.ReadInt();

            var weights = io.ReadIntArray(camels);

            weights.Sort();

            var weightCandidates = new SortedSet <int>();

            for (var flag = BitSet.One; flag < (1 << camels); flag++)
            {
                var sum = 0;
                for (int i = 0; i < weights.Length; i++)
                {
                    if (flag[i])
                    {
                        sum += weights[i];
                    }
                }
                weightCandidates.Add(sum);
            }

            var bridges = new Bridge[parts];

            for (int i = 0; i < bridges.Length; i++)
            {
                bridges[i] = new Bridge(io.ReadInt(), io.ReadInt());
            }

            if (weights.Max() > bridges.Min(b => b.Strength))
            {
                io.WriteLine(-1);
                return;
            }

            bridges.Sort();

            var toCheckList = new List <Bridge>(weightCandidates.Count);

            var index = 0;

            foreach (var w in weightCandidates.Reverse())
            {
                var maxLength = 0;

                while (index < bridges.Length && bridges[index].Strength >= w)
                {
                    maxLength.ChangeMax(bridges[index].Length);
                    index++;
                }

                if (maxLength > 0)
                {
                    toCheckList.Add(new Bridge(maxLength, w));
                }
            }


            var toCheck   = toCheckList.ToArray();
            var distances = new int[camels, camels];
            var ls        = new int[camels];
            int min       = int.MaxValue;

            foreach (var weightOrder in PermutationAlgorithms.GetPermutations(weights))
            {
                distances.Fill(0);
                var ws = weightOrder.Span;

                foreach (var bridge in toCheck)
                {
                    for (int i = 0; i < camels; i++)
                    {
                        var w = ws[i];

                        for (int j = i + 1; j < camels; j++)
                        {
                            w += ws[j];

                            if (bridge.Strength < w)
                            {
                                distances[i, j].ChangeMax(bridge.Length);
                            }
                        }
                    }
                }

                ls.AsSpan().Clear();

                for (int i = 1; i < camels; i++)
                {
                    for (int j = 0; j < i; j++)
                    {
                        ls[i].ChangeMax(ls[j] + distances[j, i]);
                    }
                }

                min.ChangeMin(ls[^ 1]);
Ejemplo n.º 6
0
 public void PermutationNullThrowsExceptionTest()
 {
     Assert.Throws <ArgumentNullException>(() => PermutationAlgorithms.GetPermutations(default(IEnumerable <int>), false).ToArray());
 }