Пример #1
0
            public void Solve()
            {
                int T;

                input.Line().Read(out T);

                int R, N, M, K;

                for (int _T = 0; _T < T; ++_T)
                {
                    input.Line().Read(out R).Read(out N).Read(out M).Read(out K);

                    output.WriteLine("Case #{0}:", _T + 1);

                    int[] C;
                    for (int i = 0; i < R; ++i)
                    {
                        input.Line().Read(K, out C);

                        bool found = false;
                        for (int a = 2; !found && a <= 5; ++a)
                        {
                            for (int b = 2; !found && b <= 5; ++b)
                            {
                                for (int c = 2; !found && c <= 5; ++c)
                                {
                                    bool[] possible = new bool[200];
                                    possible[1] = possible[a] = possible[b] = possible[c] = possible[a * b] = possible[a * c] = possible[b * c] = possible[a * b * c] = true;

                                    bool valid = true;
                                    for (int j = 0; j < K; ++j)
                                    {
                                        if (!possible[C[j]])
                                        {
                                            valid = false;
                                        }
                                    }

                                    if (valid)
                                    {
                                        found = true;
                                        output.WriteLine("{0}{1}{2}", a, b, c);
                                    }
                                }
                            }
                        }
                    }
                }
            }
Пример #2
0
            public void Solve()
            {
                int T;

                input.Line().Read(out T);

                int N;

                int[] A, B;
                for (int _T = 0; _T < T; ++_T)
                {
                    input.Line().Read(out N)
                    .Line().Read(N, out A)
                    .Line().Read(N, out B);

                    List <int> temp = new List <int>();
                    for (int i = 1; i <= N; ++i)
                    {
                        temp.Add(i);
                    }

                    int[] X = new int[N], dpInc = new int[N], dpDec = new int[N];

                    for (int i = 0; i < N; ++i)
                    {
                        dpInc[i] = -1;
                    }
                    for (int i = 0; i < N; ++i)
                    {
                        dpDec[i] = -1;
                    }

                    if (!f(temp, 0, N, X, dpInc, A, B))
                    {
                        output.WriteLine("FAIL!");
                    }

                    output.Write("Case #{0}:", _T + 1);
                    for (int i = 0; i < N; ++i)
                    {
                        output.Write(" {0}", X[i]);
                    }
                    output.WriteLine();
                }
            }
Пример #3
0
            public void Solve()
            {
                int T;

                input.Line().Read(out T);

                for (int _T = 0; _T < T; ++_T)
                {
                    int K, N;
                    input.Line().Read(out K).Read(out N);

                    int last = (1 << N) - 1;

                    int[] prev          = new int[1 << N];
                    var   availableKeys = new Dictionary <int, int> [1 << N];
                    var   lastKeys      = new Dictionary <int, int>();

                    availableKeys[last] = lastKeys;
                    prev[last]          = -1;

                    int[] keys = new int[K];
                    input.Line().Read(K, out keys);
                    for (int i = 0; i < K; ++i)
                    {
                        if (!lastKeys.ContainsKey(keys[i]))
                        {
                            lastKeys[keys[i]] = 0;
                        }
                        ++lastKeys[keys[i]];
                    }

                    int[]   requiredKey   = new int[N];
                    int[][] containedKeys = new int[N][];
                    for (int i = 0, k; i < N; ++i)
                    {
                        input.Line().Read(out requiredKey[i]).Read(out k).Read(k, out containedKeys[i]);

                        if (!lastKeys.ContainsKey(requiredKey[i]))
                        {
                            lastKeys[requiredKey[i]] = 0;
                        }
                        --lastKeys[requiredKey[i]];
                        for (int j = 0; j < k; ++j)
                        {
                            if (!lastKeys.ContainsKey(containedKeys[i][j]))
                            {
                                lastKeys[containedKeys[i][j]] = 0;
                            }
                            ++lastKeys[containedKeys[i][j]];
                        }
                    }

                    var idx = Enumerable.Range(0, (1 << N) - 1).OrderByDescending(n => CountPopBits(n));
                    foreach (var i in idx)
                    {
                        for (int j = 0; j < N; ++j)
                        {
                            if ((i & (1 << j)) == 0 && prev[i | (1 << j)] != 0)
                            {
                                var d = new Dictionary <int, int>(availableKeys[i | (1 << j)]);
                                ++d[requiredKey[j]];
                                foreach (var x in containedKeys[j])
                                {
                                    --d[x];
                                }
                                if (d[requiredKey[j]] > 0)
                                {
                                    prev[i]          = j + 1;
                                    availableKeys[i] = d;
                                    break;
                                }
                            }
                        }
                    }

                    if (prev[0] == 0)
                    {
                        output.WriteLine("Case #{0}: IMPOSSIBLE", _T + 1);
                    }
                    else
                    {
                        output.Write("Case #{0}:", _T + 1);
                        for (int i = 0, mask = 0; i < N; ++i)
                        {
                            output.Write(" {0}", prev[mask]);
                            mask |= (1 << (prev[mask] - 1));
                        }
                        output.WriteLine();
                    }
                }
            }
Пример #4
0
            public void Solve()
            {
                List <string> dictionary = new List <string>();

                //
                using (var file = new StreamReader(@"C:\Development\Visual Studio 2012\Projects\OnlineJudge\Judge .NET\Google Code Jam\2013\03 Round 1B\garbled_email_dictionary.txt"))
                {
                    String line;
                    while ((line = file.ReadLine()) != null)
                    {
                        dictionary.Add(line);
                    }
                }

                int T;

                input.Line().Read(out T);

                int[] changes = new int[100];

                string S;

                for (int _T = 0; _T < T; ++_T)
                {
                    input.Line().Read(out S);

                    int N = S.Length;

                    int[,] dp = new int[N + 1, 5];

                    for (int i = 0; i <= N; ++i)
                    {
                        for (int j = 0; j < 5; ++j)
                        {
                            dp[i, j] = 0x3fffffff;
                        }
                    }
                    dp[0, 4] = 0;

                    for (int i = 0; i < N; ++i)
                    {
                        foreach (var word in dictionary)
                        {
                            if (i + 1 < word.Length)
                            {
                                continue;
                            }

                            int cnt = 1;
                            for (int k = i - word.Length + 1, z = 0; k <= i; ++k, ++z)
                            {
                                if (word[z] != S[k])
                                {
                                    changes[cnt++] = k;
                                }
                            }

                            for (int k = 0; k < 5; ++k)
                            {
                                changes[0] = i - word.Length - k;

                                bool valid = true;
                                for (int z = 1; z < cnt; ++z)
                                {
                                    if (changes[z] - changes[z - 1] < 5)
                                    {
                                        valid = false;
                                    }
                                }

                                if (valid)
                                {
                                    dp[i + 1, Math.Min(i - changes[cnt - 1], 4)] = Math.Min(dp[i + 1, Math.Min(i - changes[cnt - 1], 4)], dp[i - word.Length + 1, k] + cnt - 1);
                                }
                            }
                        }
                    }

                    int best = 0x3fffffff;
                    for (int i = 0; i < 5; ++i)
                    {
                        best = Math.Min(best, dp[N, i]);
                    }
                    output.WriteLine("Case #{0}: {1}", _T + 1, best);
                }
            }