Example #1
0
    static object Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var a = Read();

        var r1 = a.ToArray();
        var r2 = a.ToArray();

        var q1 = new Queue <int>();
        var q2 = PQ <int> .Create(true);

        var t = 0;

        for (int i = 0; i < n; i++)
        {
            if (t == a[i])
            {
                r1[i] = q1.Dequeue();
                r2[i] = q2.Pop();
            }
            else
            {
                for (t++; t < a[i]; t++)
                {
                    q1.Enqueue(t);
                    q2.Push(t);
                }
            }
        }

        return(string.Join(" ", r1) + "\n" + string.Join(" ", r2));
    }
Example #2
0
    static void Main()
    {
        var r = new List <int>();
        var h = Read();
        var n = h[0];

        var qs = new int[n].Select(_ => PQ <int> .Create(desc: true)).ToArray();

        for (int i = 0; i < h[1]; i++)
        {
            var q = Read();
            if (q[0] == 0)
            {
                qs[q[1]].Push(q[2]);
            }
            else if (q[0] == 1)
            {
                if (qs[q[1]].Any())
                {
                    r.Add(qs[q[1]].First);
                }
            }
            else
            {
                if (qs[q[1]].Any())
                {
                    qs[q[1]].Pop();
                }
            }
        }
        Console.WriteLine(string.Join("\n", r));
    }
Example #3
0
    static Edge[] Prim(int n, int root, List <Edge>[] map)
    {
        var u = new bool[n];
        var q = PQ <Edge> .Create(e => e.cost, map[root].ToArray());

        u[root] = true;
        var mes = new List <Edge>();

        // 実際の頂点数に注意。
        while (q.Count > 0 && mes.Count < n - 1)
        {
            var e = q.Pop();
            if (u[e.j])
            {
                continue;
            }
            u[e.j] = true;
            mes.Add(e);
            foreach (var ne in map[e.j])
            {
                if (ne.j != e.i)
                {
                    q.Push(ne);
                }
            }
        }
        return(mes.ToArray());
    }
Example #4
0
    static object Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var a = ReadL();

        var q = PQ <long> .Create();

        var c = 0;

        foreach (var v in a)
        {
            var x = v;
            while ((x & 1) == 0)
            {
                x >>= 1;
                c++;
            }
            q.Push(x);
        }

        while (c-- > 0)
        {
            var x = q.Pop();
            q.Push(x * 3);
        }

        return(q.First);
    }
Example #5
0
    static object Solve()
    {
        var(n, k) = Read2();
        var s = Console.ReadLine();

        var r = new List <char>();

        var q = PQ <int> .Create(i => s[i] * 100000L + i);

        q.PushRange(Enumerable.Range(0, n - k).ToArray());

        var t = -1;

        for (int i = n - k; i < n; i++)
        {
            q.Push(i);
            while (q.First < t)
            {
                q.Pop();
            }

            t = q.Pop();
            r.Add(s[t]);
        }

        return(string.Join("", r));
    }
Example #6
0
    static void Main()
    {
        var qc = int.Parse(Console.ReadLine());
        var qs = Array.ConvertAll(new bool[qc], _ => Read());

        var pq = PQ <long> .Create();

        var d = 0L;

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        foreach (var q in qs)
        {
            if (q[0] == 1)
            {
                pq.Push(q[1] - d);
            }
            else if (q[0] == 2)
            {
                d += q[1];
            }
            else
            {
                Console.WriteLine(pq.Pop() + d);
            }
        }
        Console.Out.Flush();
    }
Example #7
0
    static object Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var a = Read();

        var r   = n;
        var sum = 0L;
        var q   = PQ <int> .Create();

        foreach (var x in a)
        {
            sum += x;

            if (x < 0)
            {
                q.Push(x);
            }

            while (sum < 0)
            {
                r--;
                sum -= q.Pop();
            }
        }

        return(r);
    }
Example #8
0
    static object Solve()
    {
        var n = int.Parse(Console.ReadLine());
        var a = Read();

        var counts = a.GroupBy(x => x).Select(g => g.Count()).ToArray();
        var q      = PQ <int> .Create(true);

        q.PushRange(counts);

        while (q.Count >= 2)
        {
            var c1 = q.Pop() - 1;
            var c2 = q.Pop() - 1;

            if (c1 > 0)
            {
                q.Push(c1);
            }
            if (c2 > 0)
            {
                q.Push(c2);
            }
        }

        if (q.Count == 0)
        {
            return(0);
        }
        return(q.Pop());
    }
Example #9
0
    static int[][] Prim(int n, int root, List <int[]>[] map)
    {
        var u = new bool[n];
        var q = PQ <int[]> .Create(e => e[2]);

        u[root] = true;
        q.PushRange(map[root].ToArray());
        var mes = new List <int[]>();

        // 実際の頂点数に注意。
        while (q.Count > 0 && mes.Count < n - 1)
        {
            var e = q.Pop();
            if (u[e[1]])
            {
                continue;
            }
            u[e[1]] = true;
            mes.Add(e);
            foreach (var ne in map[e[1]])
            {
                if (ne[1] != e[0])
                {
                    q.Push(ne);
                }
            }
        }
        return(mes.ToArray());
    }
Example #10
0
    static int[] TopologicalSort(int n, int[][] map, int[] indeg0, int[] svs)
    {
        var indeg = (int[])indeg0.Clone();

        var r = new List <int>();
        var q = PQ <int> .Create(v => random.Next());

        q.PushRange(svs);

        while (q.Count > 0)
        {
            var v = q.Pop();
            r.Add(v);
            foreach (var nv in map[v])
            {
                if (--indeg[nv] > 0)
                {
                    continue;
                }
                q.Push(nv);
            }
        }
        if (r.Count < n - 1)
        {
            return(null);
        }
        return(r.ToArray());
    }
Example #11
0
    static void Main()
    {
        var(n, m, qc) = Read3();
        var es = Array.ConvertAll(new bool[m], _ => Read());
        var x  = Read();

        var map = ToMap(n + 1, es, false);
        var sv  = 1;

        var r = 1;
        var u = new bool[n + 1];
        var q = PQ <int[]> .Create(e => e[2]);

        var l = new List <int>();

        u[sv] = true;
        q.PushRange(map[sv]);

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        for (int i = 0; i < qc; i++)
        {
            while (q.Count > 0 && q.First[2] <= x[i])
            {
                var e  = q.Pop();
                var to = e[1];
                if (u[to])
                {
                    continue;
                }

                r++;
                u[to] = true;
                l.Add(to);
            }

            foreach (var v in l)
            {
                foreach (var e in map[v])
                {
                    var to = e[1];
                    if (u[to])
                    {
                        continue;
                    }
                    q.Push(e);
                }
            }
            l.Clear();

            Console.WriteLine(r);
        }
        Console.Out.Flush();
    }
Example #12
0
    static void Main()
    {
        var h = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var a = Console.ReadLine().Split().Select(double.Parse).ToArray();

        var q = PQ <double> .Create(a, true);

        for (var i = 0; i < h[1]; i++)
        {
            q.Push(q.Pop() / 2);
        }
        Console.WriteLine(q.Sum(x => (long)x));
    }
Example #13
0
    static void Main()
    {
        var w = Console.ReadLine().GroupBy(c => c).Select(g => g.Count()).ToList();
        var k = w.Count;

        if (k == 1)
        {
            Console.WriteLine(w[0]); return;
        }

        var empty = new int[0];
        var map   = w.Select(_ => empty).ToList();
        var pq    = PQ <int> .Create(v => w[v], Enumerable.Range(0, k).ToArray());

        var root = -1;

        while (pq.Any())
        {
            var v1 = pq.Pop();
            if (!pq.Any())
            {
                root = v1; break;
            }
            var v2 = pq.Pop();

            w.Add(w[v1] + w[v2]);
            map.Add(new[] { v1, v2 });
            pq.Push(w.Count - 1);
        }

        var r = 0L;
        Action <int, int> Dfs = null;

        Dfs = (v, d) =>
        {
            if (v < k)
            {
                r += d * w[v];
            }
            foreach (var nv in map[v])
            {
                Dfs(nv, d + 1);
            }
        };

        Dfs(root, 0);
        Console.WriteLine(r);
    }
Example #14
0
        public void SortDescending_String()
        {
            var values = RandomHelper.CreateData(100000).ToArray();
            var actual = PQ <int> .Create(x => x.ToString(), true);

            TimeHelper.Measure(() => actual.PushRange(values));
            var a = new List <int>();

            TimeHelper.Measure(() => { while (actual.Any())
                                       {
                                           a.Add(actual.Pop());
                                       }
                               });
            var e = TimeHelper.Measure(() => values.OrderByDescending(x => x.ToString()).ToArray());

            CollectionAssert.AreEqual(e, a);
        }
Example #15
0
        public void SortDescending()
        {
            var values = RandomHelper.CreateData(100000).ToArray();
            var actual = PQ <int> .Create(true);

            TimeHelper.Measure(() => actual.PushRange(values));
            var a = new int[values.Length];

            TimeHelper.Measure(() => { for (var i = 0; i < a.Length; i++, actual.Pop())
                                       {
                                           a[i] = actual.First;
                                       }
                               });
            var e = TimeHelper.Measure(() => values.OrderByDescending(x => x).ToArray());

            CollectionAssert.AreEqual(e, a);
        }
Example #16
0
    static object Solve()
    {
        var(n, m) = Read2();
        var es = Array.ConvertAll(new bool[m], _ => Read2()).Distinct().ToArray();

        var r = new List <int>();
        var q = PQ <int> .Create();

        var map    = Array.ConvertAll(new bool[n + 1], _ => new List <int>());
        var counts = new int[n + 1];

        foreach (var(a, b) in es)
        {
            map[a].Add(b);
            counts[b]++;
        }

        for (int i = 1; i <= n; i++)
        {
            if (counts[i] == 0)
            {
                q.Push(i);
            }
        }

        while (q.Count > 0)
        {
            var v = q.Pop();
            r.Add(v);

            foreach (var nv in map[v])
            {
                if (--counts[nv] == 0)
                {
                    q.Push(nv);
                }
            }
        }

        if (r.Count < n)
        {
            return(-1);
        }
        return(string.Join(" ", r));
    }
Example #17
0
        public void SortTake()
        {
            var values = RandomHelper.CreateData(100000).ToArray();
            var actual = TimeHelper.Measure(() =>
            {
                var pq = PQ <int> .Create();
                pq.PushRange(values);
                var a = new int[100];
                for (var i = 0; i < a.Length; i++)
                {
                    a[i] = pq.Pop();
                }
                return(a);
            });
            var expected = TimeHelper.Measure(() => values.OrderBy(x => x).Take(100).ToArray());

            CollectionAssert.AreEqual(expected, actual);
        }
Example #18
0
        public void Sort()
        {
            var values = RandomHelper.CreateData(100000).ToArray();
            var actual = PQ <int> .Create();

            TimeHelper.Measure(() => actual.PushRange(values));
            var a = new int[values.Length];

            TimeHelper.Measure(() => { for (var i = 0; i < a.Length; i++)
                                       {
                                           a[i] = actual.Pop();
                                       }
                               });
            var e = (int[])values.Clone();

            TimeHelper.Measure(() => Array.Sort(e));
            CollectionAssert.AreEqual(e, a);
        }
Example #19
0
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var t = new int[n].Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).ToLookup(x => x[0], x => x[1]);

        var pq = PQ <int> .Create(desc : true);

        var p = new int[n + 1];

        for (int i = 1; i <= n; i++)
        {
            foreach (var b in t[i])
            {
                pq.Push(b);
            }
            p[i] = p[i - 1] + pq.Pop();
        }
        Console.WriteLine(string.Join("\n", p.Skip(1)));
    }
Example #20
0
    static void Main()
    {
        var qc = int.Parse(Console.ReadLine());

        var q  = new Queue <int>();
        var pq = PQ <int> .Create();

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        while (qc-- > 0)
        {
            var query = Read();

            if (query[0] == 1)
            {
                var x = query[1];
                q.Enqueue(x);
            }
            else if (query[0] == 2)
            {
                if (pq.Count > 0)
                {
                    Console.WriteLine(pq.Pop());
                }
                else
                {
                    Console.WriteLine(q.Dequeue());
                }
            }
            else
            {
                while (q.Count > 0)
                {
                    pq.Push(q.Dequeue());
                }
            }
        }
        Console.Out.Flush();
    }
Example #21
0
    static void Main()
    {
        var h  = Read();
        var js = new int[h[0]].Select(_ => Read()).ToLookup(j => j[0], j => j[1]);

        var r  = 0;
        var pq = PQ <int> .Create(null, true);

        for (var i = 1; i <= h[1]; i++)
        {
            foreach (var b in js[i])
            {
                pq.Push(b);
            }
            if (pq.Any())
            {
                r += pq.Pop();
            }
        }
        Console.WriteLine(r);
    }
Example #22
0
    static object Solve()
    {
        var(n, m, x) = Read3();
        var h = Read();

        var r = new int[n];
        var t = new int[m];
        var q = PQ <int> .Create(j => t[j]);

        q.PushRange(Enumerable.Range(0, m).ToArray());

        for (int i = 0; i < n; i++)
        {
            var j = q.Pop();
            r[i]  = j + 1;
            t[j] += h[i];
            q.Push(j);
        }

        return("YES\n" + string.Join(" ", r));
    }
Example #23
0
    static void Main()
    {
        var r  = new List <int>();
        var pq = PQ <int> .Create(desc : true);

        string s;

        while ((s = Console.ReadLine()) != "end")
        {
            var q = s.Split();
            if (q[0] == "insert")
            {
                pq.Push(int.Parse(q[1]));
            }
            else
            {
                r.Add(pq.Pop());
            }
        }
        Console.WriteLine(string.Join("\n", r));
    }
Example #24
0
    static void Main()
    {
        var h   = Read();
        var n   = h[0];
        var map = UndirectedMap(n, new int[h[1]].Select(_ => Read()).ToArray());

        var c = Enumerable.Repeat(long.MaxValue, n + 1).ToArray();

        c[1] = 0;
        var u = new bool[n + 1];
        // BUG: descending order
        var pq = PQ <R> .Create(new[] { new R {
                                            to = 1
                                        } }, x => x.cost, true);

        while (pq.Any())
        {
            var p = pq.Pop().to;
            if (u[p])
            {
                continue;
            }
            u[p] = true;

            foreach (var r in map[p])
            {
                var v = c[p] + r.cost;
                if (v >= c[r.to])
                {
                    continue;
                }
                c[r.to] = v;
                pq.Push(new R {
                    to = r.to, cost = v
                });
            }
        }
        Console.WriteLine(c[n] < long.MaxValue ? c[n] : -1);
    }
Example #25
0
    static void Main()
    {
        var L10 = 1000000L;
        var h = Read();
        int n = h[0], k = h[1], d = h[2];
        var a = Read().Select((x, i) => L10 * x + i).ToArray();

        if (n < d * (k - 1) + 1)
        {
            Console.WriteLine(-1); return;
        }

        var r  = new List <long>();
        var pq = PQ <long> .Create(a.Take(n - d * (k - 1)).ToArray());

        var q = new Queue <long>(a.Skip(n - d * (k - 1)));

        var t  = pq.Pop();
        var ci = t % L10;

        r.Add(t / L10);

        for (int i = 1; i < k; i++)
        {
            for (int j = 0; j < d; j++)
            {
                pq.Push(q.Dequeue());
            }

            while ((t = pq.Pop()) % L10 < ci + d)
            {
                ;
            }
            ci = t % L10;
            r.Add(t / L10);
        }
        Console.WriteLine(string.Join(" ", r));
    }
Example #26
0
    static void Main()
    {
        Func <int[]> read = () => Console.ReadLine().Split().Select(int.Parse).ToArray();
        var          h = read();
        int          n = h[0], w = h[1], c = h[2];
        var          ps = new int[n].Select(_ => read()).OrderBy(x => x[0]);

        var lq = new Queue <int[]>(ps);
        var rq = PQ <int[]> .Create(p => p[1]);

        long m = long.MaxValue, s = 0;
        int  l = 0, r = c;

        while (r <= w)
        {
            while (rq.Any && rq.First[1] <= l)
            {
                var p = rq.Pop();
                s -= p[2];
            }
            while (lq.Any() && lq.Peek()[0] < r)
            {
                var p = lq.Dequeue();
                rq.Push(p);
                s += p[2];
            }
            m = Math.Min(m, s);

            var d = Math.Min(rq.Any ? rq.First[1] - l : int.MaxValue, lq.Any() ? lq.Peek()[0] + 1 - r : int.MaxValue);
            if (d == int.MaxValue)
            {
                break;
            }
            l += d;
            r += d;
        }
        Console.WriteLine(m);
    }
Example #27
0
    static void Main()
    {
        var n  = int.Parse(Console.ReadLine());
        var ps = Enumerable.Range(0, n).Select(i => Read().Skip(1).Select((t, j) => new P(i, j, t)).ToArray()).ToArray();
        var m  = int.Parse(Console.ReadLine());
        var a  = Read();

        var qs  = Enumerable.Range(0, n).Select(i => new Queue <P>(ps[i].Skip(1))).ToArray();
        var pq1 = PQ <P> .Create(p => p.t, ps.SelectMany(x => x.Take(1)).ToArray(), true);

        var pq2 = PQ <P> .Create(p => p.t, ps.SelectMany(x => x.Take(2)).ToArray(), true);

        var r = new List <int>();

        foreach (var x in a)
        {
            P p;
            if (x == 1)
            {
                while ((p = pq1.Pop()).end)
                {
                    ;
                }
                p.end = true;
                r.Add(p.t);

                var q = qs[p.i];
                if (!q.Any())
                {
                    continue;
                }
                pq1.Push(q.Dequeue());
                if (q.Any())
                {
                    pq2.Push(q.Peek());
                }
            }
            else
            {
                while ((p = pq2.Pop()).end)
                {
                    ;
                }
                p.end = true;
                r.Add(p.t);

                var q = qs[p.i];
                if (!q.Any())
                {
                    continue;
                }
                if (q.Peek() == p)
                {
                    q.Dequeue();
                }
                else
                {
                    pq1.Push(q.Dequeue());
                }
                if (q.Any())
                {
                    pq2.Push(q.Peek());
                }
            }
        }
        Console.WriteLine(string.Join("\n", r));
    }