Пример #1
0
        void SolveSmall()
        {
            ans = 0;
            var ids = new int[k];

            double[,] dp = new double[20, 40];
            for (int mask = 0; mask < 1 << n; ++mask)
            {
                if (BitUtils.CountOnes(mask) != k)
                {
                    continue;
                }

                int z = 0;
                for (int i = 0; i < n; i++)
                {
                    if (BitUtils.BitInMask(mask, i))
                    {
                        ids[z++] = i;
                    }
                }

                dp[0, 0] = 1;
                for (int i = 1; i <= k; i++)
                {
                    for (int j = 0; j <= i; ++j)
                    {
                        dp[i, j] = p[ids[i - 1]] * (j == 0 ? 0 : dp[i - 1, j - 1]) + (1 - p[ids[i - 1]]) * dp[i - 1, j];
                    }
                }

                ans = Math.Max(ans, dp[k, k / 2]);
            }
        }
Пример #2
0
        void Solve()
        {
            all = new Dictionary <string, int>();
            int[][] wordInts = new int[n][];
            for (int i = 0; i < n; i++)
            {
                wordInts[i] = new int[words[i].Length];
                for (int j = 0; j < words[i].Length; j++)
                {
                    wordInts[i][j] = GetId(words[i][j]);
                }
            }
            var eng = new bool[n];

            eng[0] = true;
            ans    = int.MaxValue;
            var a = new int[all.Count + 1];

            for (int mask = 0; mask < 1 << (n - 2); mask++)
            {
                for (int i = 0; i < n - 2; i++)
                {
                    if (BitUtils.BitInMask(mask, i))
                    {
                        eng[i + 2] = true;
                    }
                    else
                    {
                        eng[i + 2] = false;
                    }
                }
                Array.Clear(a, 0, a.Length);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < words[i].Length; j++)
                    {
                        if (eng[i])
                        {
                            a[wordInts[i][j]] |= 1;
                        }
                        else
                        {
                            a[wordInts[i][j]] |= 2;
                        }
                    }
                }
                ans = Math.Min(ans, a.Count(c => c == 3));
            }
        }