Exemplo n.º 1
0
        public byte[] ToBytes()
        {
            new Random().NextBytes(Nonce);
            const int constructorNumber       = 0x05162463;
            const int vectorConstructorNumber = 0x1cb5c415;


            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    binaryWriter.Write(constructorNumber);
                    binaryWriter.Write(ServerNonce);
                    binaryWriter.Write(Nonce);
                    Serializers.Bytes.write(binaryWriter, Pq.ToByteArrayUnsigned());
                    binaryWriter.Write(vectorConstructorNumber);
                    binaryWriter.Write(Fingerprints.Count);
                    foreach (var fingerprint in Fingerprints)
                    {
                        binaryWriter.Write(fingerprint);
                    }

                    return(memoryStream.ToArray());
                }
            }
        }
Exemplo n.º 2
0
            public override void Collect(int doc)
            {
                float score = Scorer_Renamed.Score();

                // this collector cannot handle these scores:
                Debug.Assert(score != float.NegativeInfinity);
                Debug.Assert(!float.IsNaN(score));

                TotalHits_Renamed++;

                if (score > After.Score || (score == After.Score && doc <= AfterDoc))
                {
                    // hit was collected on a previous page
                    return;
                }

                if (score <= PqTop.Score)
                {
                    // Since docs are returned in-order (i.e., increasing doc Id), a document
                    // with equal score to pqTop.score cannot compete since HitQueue favors
                    // documents with lower doc Ids. Therefore reject those docs too.
                    return;
                }
                CollectedHits++;
                PqTop.Doc   = doc + DocBase;
                PqTop.Score = score;
                PqTop       = Pq.UpdateTop();
            }
Exemplo n.º 3
0
    static void Main()
    {
        Sc  sc = new Sc();
        int n  = sc.I;
        var pq = new Pq <Mo <int> >(100000, true);

        bool[] b = new bool[100000];
        pq.Push(new Mo <int>(1, 1));
        b[1] = true;
        while (pq.Top.d != 0)
        {
            int p = pq.Top.n, q = pq.Top.d;
            pq.Pop();
            b[q] = true;
            if (!b[(q * 10) % n])
            {
                pq.Push(new Mo <int>(p, (q * 10) % n));
            }
            if (!b[(q + 1) % n])
            {
                pq.Push(new Mo <int>(p + 1, (q + 1) % n));
            }
        }
        Console.WriteLine("{0}", pq.Top.n);
    }
Exemplo n.º 4
0
        protected internal override TopDocs NewTopDocs(ScoreDoc[] results, int start)
        {
            if (results == null)
            {
                return(EMPTY_TOPDOCS);
            }

            // We need to compute maxScore in order to set it in TopDocs. If start == 0,
            // it means the largest element is already in results, use its score as
            // maxScore. Otherwise pop everything else, until the largest element is
            // extracted and use its score as maxScore.
            float maxScore = float.NaN;

            if (start == 0)
            {
                maxScore = results[0].Score;
            }
            else
            {
                for (int i = Pq.Size(); i > 1; i--)
                {
                    Pq.Pop();
                }
                maxScore = Pq.Pop().Score;
            }

            return(new TopDocs(TotalHits_Renamed, results, maxScore));
        }
Exemplo n.º 5
0
 // prevents instantiation
 private TopScoreDocCollector(int numHits)
     : base(new HitQueue(numHits, true))
 {
     // HitQueue implements getSentinelObject to return a ScoreDoc, so we know
     // that at this point top() is already initialized.
     PqTop = Pq.Top();
 }
Exemplo n.º 6
0
            public override void Collect(int doc)
            {
                float score = Scorer_Renamed.Score();

                // this collector cannot handle NaN
                Debug.Assert(!float.IsNaN(score));

                TotalHits_Renamed++;
                if (score > After.Score || (score == After.Score && doc <= AfterDoc))
                {
                    // hit was collected on a previous page
                    return;
                }
                if (score < PqTop.Score)
                {
                    // Doesn't compete w/ bottom entry in queue
                    return;
                }
                doc += DocBase;
                if (score == PqTop.Score && doc > PqTop.Doc)
                {
                    // Break tie in score by doc ID:
                    return;
                }
                CollectedHits++;
                PqTop.Doc   = doc;
                PqTop.Score = score;
                PqTop       = Pq.UpdateTop();
            }
Exemplo n.º 7
0
    static void Main()
    {
        Sc     sc = new Sc();
        string s = sc.S;
        int    n = 0, m = 1 << s.Length, ans = 0;

        int[] dp = new int[m];
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'o')
            {
                n |= 1 << i;
            }
        }
        var pq = new Pq <Mo <int> >(200, true);

        pq.Push(new Mo <int>(1, n));
        if (n == m - 1)
        {
            ans = 1;
        }
        while (ans == 0)
        {
            int k = pq.Top.n, d = pq.Top.d;
            pq.Pop();
            for (int i = 1; i < s.Length; i++)
            {
                int q = (((n | (n << s.Length)) >> i) & (m - 1)) | d;
                if (q == m - 1)
                {
                    ans = k + 1; break;
                }
                else if (dp[q] == 0)
                {
                    pq.Push(new Mo <int>(k + 1, q));
                    dp[q] = k + 1;
                }
            }
        }
        Console.WriteLine(ans);
    }
Exemplo n.º 8
0
    static void Main()
    {
        Sc     sc = new Sc();
        int    n = sc.I, t = n - 2;
        double ans = 0;
        var    pq  = new Pq <Mo <int> >(n * n, true);

        int[][]  a = new int[n][];
        double[] h = new double[n];
        a[0] = sc.Ia;
        for (int i = 1; i < n; i++)
        {
            a[i] = sc.Ia;
            double d = Math.Sqrt((a[0][0] - a[i][0]) * (a[0][0] - a[i][0]) + (a[0][1] - a[i][1]) * (a[0][1] - a[i][1]));
            d /= Math.Min(a[0][2], a[i][3]);
            pq.Push(new Mo <int>(d, i));
        }
        while (t >= 0)
        {
            double k = pq.Top.n;
            int    g = pq.Top.d;
            if (h[g] == 0)
            {
                h[g] = k;
                ans  = Math.Max(ans, k + t);
                t--;
                for (int i = 1; i < n; i++)
                {
                    if (h[i] == 0)
                    {
                        double d = Math.Sqrt((a[g][0] - a[i][0]) * (a[g][0] - a[i][0]) + (a[g][1] - a[i][1]) * (a[g][1] - a[i][1]));
                        d /= Math.Min(a[g][2], a[i][3]);
                        pq.Push(new Mo <int>(d + k, i));
                    }
                }
            }
            pq.Pop();
        }
        Console.WriteLine(ans);
    }
Exemplo n.º 9
0
    static void Main()
    {
        Sc  sc = new Sc();
        int n  = sc.I;
        var pq = new Pq <Mo <int> >(100000, true);

        int[] b = new int[100000];
        pq.Push(new Mo <int>(1, 1));
        b[1] = 1;
        while (pq.Top.d != 0)
        {
            int p = pq.Top.n, q = pq.Top.d;
            pq.Pop();
            if (b[(q * 10) % n] == 0 || b[(q * 10) % n] > p)
            {
                pq.Push(new Mo <int>(p, (q * 10) % n)); b[(q * 10) % n] = p;
            }
            if (b[(q + 1) % n] == 0 || b[(q + 1) % n] > p + 1)
            {
                pq.Push(new Mo <int>(p + 1, (q + 1) % n)); b[(q + 1) % n] = p + 1;
            }
        }
        Console.WriteLine("{0}", pq.Top.n);
    }
Exemplo n.º 10
0
            protected internal override TopDocs NewTopDocs(ScoreDoc[] results, int start)
            {
                if (results == null)
                {
                    return(EMPTY_TOPDOCS);
                }

                float maxScore = float.NaN;

                if (start == 0)
                {
                    maxScore = results[0].Score;
                }
                else
                {
                    for (int i = Pq.Size(); i > 1; i--)
                    {
                        Pq.Pop();
                    }
                    maxScore = Pq.Pop().Score;
                }

                return(new TopDocs(TotalHits, results, maxScore));
            }
Exemplo n.º 11
0
    static void Main()
    {
        Sc sc = new Sc();

        int[]  s = sc.Ia;
        int    sy = -1, sx = -1;
        bool   e   = false;
        double ans = double.MaxValue;

        string[] h = new string[s[0]];
        int[,] b = new int[s[0], s[1]];
        for (int i = 0; i < s[0]; i++)
        {
            h[i] = sc.S;
            for (int j = 0; j < s[1]; j++)
            {
                if (h[i][j] == 's')
                {
                    sy = i; sx = j; b[i, j] = 1;
                }
                else if (h[i][j] == '#')
                {
                    b[i, j] = 1;
                }
                else
                {
                    b[i, j] = int.MaxValue;
                }
            }
        }
        var pq = new Pq <Mo <int[]> >(s[0] * s[1] * 3, false);

        if (sy != 0 && b[sy - 1, sx] != 1)
        {
            pq.Push(new Mo <int[]>((h[sy - 1][sx] - '0') * 0.99, new int[] { sy - 1, sx, 2 })); b[sy - 1, sx] = 2;
        }
        if (sy != s[0] - 1 && b[sy + 1, sx] != 1)
        {
            pq.Push(new Mo <int[]>((h[sy + 1][sx] - '0') * 0.99, new int[] { sy + 1, sx, 2 })); b[sy + 1, sx] = 2;
        }
        if (sx != 0 && b[sy, sx - 1] != 1)
        {
            pq.Push(new Mo <int[]>((h[sy][sx - 1] - '0') * 0.99, new int[] { sy, sx - 1, 2 })); b[sy, sx - 1] = 2;
        }
        if (sx != s[1] - 1 && b[sy, sx + 1] != 1)
        {
            pq.Push(new Mo <int[]>((h[sy][sx + 1] - '0') * 0.99, new int[] { sy, sx + 1, 2 })); b[sy, sx + 1] = 2;
        }
        while (pq.cnt > 0)
        {
            int ky = pq.Top.d[0], kx = pq.Top.d[1], t = pq.Top.d[2];
            ans = Math.Min(pq.Top.n, ans);
            pq.Pop();
            if (ky != 0 && b[ky - 1, kx] > t + 1)
            {
                if (h[ky - 1][kx] == 'g')
                {
                    e = true; break;
                }
                pq.Push(new Mo <int[]>((h[ky - 1][kx] - '0') * Math.Pow(0.99, t), new int[] { ky - 1, kx, t + 1 }));
                b[ky - 1, kx] = t + 1;
            }
            if (ky != s[0] - 1 && b[ky + 1, kx] > t + 1)
            {
                if (h[ky + 1][kx] == 'g')
                {
                    e = true; break;
                }
                pq.Push(new Mo <int[]>((h[ky + 1][kx] - '0') * Math.Pow(0.99, t), new int[] { ky + 1, kx, t + 1 }));
                b[ky + 1, kx] = t + 1;
            }
            if (kx != 0 && b[ky, kx - 1] > t + 1)
            {
                if (h[ky][kx - 1] == 'g')
                {
                    e = true; break;
                }
                pq.Push(new Mo <int[]>((h[ky][kx - 1] - '0') * Math.Pow(0.99, t), new int[] { ky, kx - 1, t + 1 }));
                b[ky, kx - 1] = t + 1;
            }
            if (kx != s[1] - 1 && b[ky, kx + 1] > t + 1)
            {
                if (h[ky][kx + 1] == 'g')
                {
                    e = true; break;
                }
                pq.Push(new Mo <int[]>((h[ky][kx + 1] - '0') * Math.Pow(0.99, t), new int[] { ky, kx + 1, t + 1 }));
                b[ky, kx + 1] = t + 1;
            }
        }
        Console.WriteLine(e?ans:-1);
    }
Exemplo n.º 12
0
    static void Main()
    {
        Sc sc = new Sc();

        string[] s = sc.Sa;
        int      n = sc.I;
        int      m = 0;

        int[] h = new int[n + 2];
        h[0]     = -2;
        h[n + 1] = -1;
        var sb = new StringBuilder();

        if (s[0] == s[1] || Le(s[0], s[1]) == 1)
        {
            sb.Append(0 + "\n" + s[0] + "\n" + s[1] + "\n");
            Console.Write(sb);
        }
        else
        {
            string[] a = new string[n + 2];
            a[0]      = s[0];
            a[n + 1]  = s[1];
            li        = new List <int> [n + 2];
            li[0]     = new List <int>();
            li[n + 1] = new List <int>();
            for (int i = 1; i <= n; i++)
            {
                h[i]  = -1;
                a[i]  = sc.S;
                li[i] = new List <int>();
                for (int j = 0; j < i; j++)
                {
                    if (Le(a[i], a[j]) == 1)
                    {
                        li[i].Add(j);
                        li[j].Add(i);
                        m++;
                    }
                }
                if (Le(a[i], a[n + 1]) == 1)
                {
                    li[i].Add(n + 1);
                    li[n + 1].Add(i);
                    m++;
                }
            }
            var pq = new Pq <Mo <int[]> >(m, true);
            for (int i = 0; i < li[0].Count; i++)
            {
                pq.Push(new Mo <int[]>(0, new int[] { li[0][i], 0 }));
            }
            while (pq.cnt > 0)
            {
                if (h[pq.Top.d[0]] == -1)
                {
                    h[pq.Top.d[0]] = pq.Top.d[1];
                    for (int i = 0; i < li[pq.Top.d[0]].Count; i++)
                    {
                        if (h[li[pq.Top.d[0]][i]] == -1)
                        {
                            pq.Push(new Mo <int[]>(pq.Top.n + 1, new int[] { li[pq.Top.d[0]][i], pq.Top.d[0] }));
                        }
                    }
                }
                if (pq.Top.d[0] == n + 1)
                {
                    break;
                }
                pq.Pop();
            }
            if (pq.cnt == 0)
            {
                Console.WriteLine(-1);
            }
            else
            {
                int q = n + 1, z = -1;
                while (h[q] != -2)
                {
                    z++;
                    sb.Insert(0, a[q] + "\n");
                    q = h[q];
                }
                sb.Insert(0, z + "\n" + a[0] + "\n");
                Console.Write(sb);
            }
        }
    }
Exemplo n.º 13
0
 public override void Collect(int doc)
 {
     ++TotalHits;
     Pq.InsertWithOverflow(new ScoreDoc(doc + @base, Scores[Idx++]));
 }
Exemplo n.º 14
0
    static void Main()
    {
        Sc sc = new Sc();

        int[]    s = sc.Ia;
        string[] h = new string[s[0]];
        int[,] b = new int[s[0], s[1]];
        var    pq  = new Pq <Mo <int[]> >(s[0] * s[1], true);
        string ans = "NO";

        for (int i = 0; i < s[0]; i++)
        {
            h[i] = sc.S;
            for (int j = 0; j < s[1] && pq.cnt == 0; j++)
            {
                if (h[i][j] == 's')
                {
                    pq.Push(new Mo <int[]>(1, new int[] { i, j }));
                    b[i, j] = 1;
                    break;
                }
            }
        }
        while (pq.cnt > 0)
        {
            int y = pq.Top.d[0], x = pq.Top.d[1], d = pq.Top.n;
            if (h[y][x] == 'g')
            {
                ans = "YES"; break;
            }
            pq.Pop();
            if (y != 0 && b[y - 1, x] == 0)
            {
                if (h[y - 1][x] != '#')
                {
                    pq.Push(new Mo <int[]>(d, new int[] { y - 1, x })); b[y - 1, x] = d;
                }
                else if (d < 3)
                {
                    pq.Push(new Mo <int[]>(d + 1, new int[] { y - 1, x })); b[y - 1, x] = d + 1;
                }
            }
            if (y != s[0] - 1 && b[y + 1, x] == 0)
            {
                if (h[y + 1][x] != '#')
                {
                    pq.Push(new Mo <int[]>(d, new int[] { y + 1, x })); b[y + 1, x] = d;
                }
                else if (d < 3)
                {
                    pq.Push(new Mo <int[]>(d + 1, new int[] { y + 1, x })); b[y + 1, x] = d + 1;
                }
            }
            if (x != 0 && b[y, x - 1] == 0)
            {
                if (h[y][x - 1] != '#')
                {
                    pq.Push(new Mo <int[]>(d, new int[] { y, x - 1 })); b[y, x - 1] = d;
                }
                else if (d < 3)
                {
                    pq.Push(new Mo <int[]>(d + 1, new int[] { y, x - 1 })); b[y, x - 1] = d + 1;
                }
            }
            if (x != s[1] - 1 && b[y, x + 1] == 0)
            {
                if (h[y][x + 1] != '#')
                {
                    pq.Push(new Mo <int[]>(d, new int[] { y, x + 1 })); b[y, x + 1] = d;
                }
                else if (d < 3)
                {
                    pq.Push(new Mo <int[]>(d + 1, new int[] { y, x + 1 })); b[y, x + 1] = d + 1;
                }
            }
        }
        Console.WriteLine(ans);
    }
Exemplo n.º 15
0
 protected internal override int TopDocsSize()
 {
     return(CollectedHits < Pq.Size() ? CollectedHits : Pq.Size());
 }
Exemplo n.º 16
0
    static void Main()
    {
        Sc sc = new Sc();

        int[]    s = sc.Ia;
        string[] a = new string[s[0]];
        int[,] g = new int[3, 2];
        int[][] h = new int[s[0]][];
        int[,,,] dp = new int[3, s[0], s[1], s[2] + 1];
        for (int i = 0; i < s[0]; i++)
        {
            a[i] = sc.S;
            h[i] = Enumerable.Repeat(-1, s[1]).ToArray();
            for (int j = 0; j < s[1]; j++)
            {
                if (a[i][j] == 'S')
                {
                    g[0, 0] = i; g[0, 1] = j;
                }
                else if (a[i][j] == 'G')
                {
                    g[1, 0] = i; g[1, 1] = j;
                }
                else if (a[i][j] == 'C')
                {
                    g[2, 0] = i; g[2, 1] = j;
                }
            }
        }
        for (int k = 0; k < 3; k++)
        {
            if (k != 0)
            {
                for (int i = 0; i < s[0]; i++)
                {
                    h[i] = Enumerable.Repeat(-1, s[1]).ToArray();
                }
            }
            h[g[k, 0]][g[k, 1]] = 0;
            var pq = new Pq <Mo <int[]> >(s[0] * s[1] * (s[2] + 1), true);
            for (int i = 0; i < 4; i++)
            {
                int y = g[k, 0] + di[i][0], x = g[k, 1] + di[i][1];
                if (0 <= y && y < s[0] && 0 <= x && x < s[1] && a[y][x] != 'T' && (a[y][x] != 'E' || s[2] != 0))
                {
                    pq.Push(new Mo <int[]>(a[y][x] == 'E'?1:0, 1, new int[] { y, x }));
                }
            }
            while (pq.cnt > 0)
            {
                Mo <int[]> q = pq.Top;
                pq.Pop();
                if (h[q.d[0]][q.d[1]] == q.n)
                {
                    continue;
                }
                h[q.d[0]][q.d[1]]          = q.n;
                dp[k, q.d[0], q.d[1], q.n] = q.m;
                for (int i = 0; i < 4; i++)
                {
                    int y = q.d[0] + di[i][0], x = q.d[1] + di[i][1];
                    if (0 <= y && y < s[0] && 0 <= x && x < s[1] && a[y][x] != 'T')
                    {
                        int p = q.n + (a[y][x] == 'E'?1:0);
                        if (s[2] >= p && h[y][x] < p)
                        {
                            pq.Push(new Mo <int[]>(p, q.m + 1, new int[] { y, x }));
                        }
                    }
                }
            }
        }
        int ans = 10000;

        for (int i = 0; i < s[0]; i++)
        {
            for (int j = 0; j < s[1]; j++)
            {
                if (a[i][j] == 'T')
                {
                    continue;
                }
                int[] z = Enumerable.Repeat(10000, s[2] + 1).ToArray();
                if (a[i][j] != 'E')
                {
                    for (int k = 0; k <= s[2]; k++)
                    {
                        for (int l = 0; l <= k; l++)
                        {
                            if ((a[i][j] == 'S' || dp[0, i, j, l] != 0) && (a[i][j] == 'C' || dp[2, i, j, k - l] != 0))
                            {
                                z[k] = Math.Min(z[k], dp[0, i, j, l] + dp[2, i, j, k - l] * 2);
                            }
                        }
                    }
                    for (int k = 0; k <= s[2]; k++)
                    {
                        for (int l = 0; l <= k; l++)
                        {
                            if ((a[i][j] == 'G' || dp[1, i, j, l] != 0))
                            {
                                ans = Math.Min(ans, z[k - l] + dp[1, i, j, l]);
                            }
                        }
                    }
                }
                else
                {
                    for (int k = 1; k <= s[2]; k++)
                    {
                        for (int l = 1; l <= k; l++)
                        {
                            if (dp[0, i, j, l] != 0 && dp[2, i, j, k - l + 1] != 0)
                            {
                                z[k] = Math.Min(z[k], dp[0, i, j, l] + dp[2, i, j, k - l + 1] * 2);
                            }
                        }
                    }
                    for (int k = 1; k <= s[2]; k++)
                    {
                        for (int l = 1; l <= k; l++)
                        {
                            if (dp[1, i, j, l] != 0)
                            {
                                ans = Math.Min(ans, z[k - l + 1] + dp[1, i, j, l]);
                            }
                        }
                    }
                }
            }
        }
        Console.WriteLine(ans == 10000?-1:ans);
    }