public static int Solve(int[] a, int d)
        {
            int   result = 0;
            Treap treap  = new Treap();

            for (int i = 0; i < a.Length && i < d; i++)
            {
                treap.Add(a[i]);
            }

            int median;

            for (int i = d; i < a.Length; i++)
            {
                if (d % 2 == 1)
                {
                    median = treap.Get(d / 2 + 1) * 2;
                }
                else
                {
                    median = treap.Get(d / 2) + treap.Get(d / 2 + 1);
                }

                if (a[i] >= median)
                {
                    result++;
                }
                treap.Add(a[i]);
                treap.Remove(a[i - d]);
            }

            return(result);
        }
        public void GivenEmptyTreapWhenAddElementsThenElementsAdded()
        {
            var treap = new Treap <int>();

            treap.Add(1);
            treap.Add(2);
            treap.Add(3);
            treap.Add(4);
            Assert.Equal(1, treap[0]);
            Assert.Equal(2, treap[1]);
            Assert.Equal(3, treap[2]);
            Assert.Equal(4, treap[3]);
        }
        public void GivenEmptyCollectionWhenSpecifyMaxComparerThenOrdererMaxToMin()
        {
            var comparer = Comparer <int> .Create((x, y) => y.CompareTo(x));

            var treap = new Treap <int>(comparer);

            treap.Add(1);
            treap.Add(20);
            treap.Add(10);
            treap.Add(5);
            Assert.Equal(20, treap[0]);
            Assert.Equal(1, treap[treap.Count - 1]);
        }
예제 #4
0
        public Treap <int> TestTreap()
        {
            Treap <int> keep = new Treap <int>();

            for (int i = 0; i < count; ++i)
            {
                keep.Add(i);
            }
            for (int i = 0; i < count; ++i)
            {
                keep.Add(~i);
            }
            return(keep);
        }
        public void GivenTreapWhenAddingAndRemovingElementsThenCountStaysRight()
        {
            var treap = new Treap <int>();

            Assert.Empty(treap);
            treap.Add(4);
            Assert.Single(treap);
            treap.Add(1);
            Assert.Equal(2, treap.Count);
            treap.Remove(4);
            Assert.Single(treap);
            treap.Remove(1);
            Assert.Empty(treap);
        }
예제 #6
0
파일: Program.cs 프로젝트: Zmiecer/BSU
 static void Main(string[] args)
 {
     Treap treap = new Treap();
     using (TextReader reader = File.OpenText(@"archive.in"))
     {
         int n, q;
         string text = reader.ReadLine();
         string[] bits = text.Split(' ');
         n = int.Parse(bits[0]);
         q = int.Parse(bits[1]);
         for (int i = 1; i <= n; i++)
         {
             treap.Add(i);
         }
         for (int i = 0; i < q; i++)
         {
             text = reader.ReadLine();
             bits = text.Split(' ');
             int left = int.Parse(bits[0]);
             int right = int.Parse(bits[1]);
             treap.Request(left, right);
         }
     }
     treap.WriteAnswer();
     using (StreamWriter file = new StreamWriter(@"archive.out"))
     {
         foreach(int number in ints)
         {
             file.Write(number + " ");
         }
     }
 }
예제 #7
0
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());

        var r   = new List <int>();
        var set = new Treap <int>();

        for (int i = 0; i < n; i++)
        {
            var q = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            if (q[0] == 0)
            {
                set.Add(q[1]);
                r.Add(set.Count);
            }
            else if (q[0] == 1)
            {
                r.Add(set.Contains(q[1]) ? 1 : 0);
            }
            else if (q[0] == 2)
            {
                set.Remove(q[1]);
            }
            else
            {
                r.AddRange(set.GetItems(x => x >= q[1], x => x <= q[2]));
            }
        }
        Console.WriteLine(string.Join("\n", r));
    }
예제 #8
0
    public void Solve()
    {
        int n = ReadInt();
        var a = ReadIntMatrix(n);

        var q   = new Heap <int[]>((t1, t2) => t1[0].CompareTo(t2[0]));
        var tr1 = new Treap <int>();
        var tr2 = new Treap <int>();

        tr1.Max = tr2.Max = int.MinValue;
        tr1.Min = tr2.Min = int.MaxValue;
        for (int i = 0; i < n; i++)
        {
            if (a[i][0] > a[i][1])
            {
                int t = a[i][0];
                a[i][0] = a[i][1];
                a[i][1] = t;
            }
            q.Push(a[i]);
            tr1.Add(a[i][0]);
            tr2.Add(a[i][1]);
        }

        long ans = 1L * (tr1.Max - tr1.Min) * (tr2.Max - tr2.Min);

        while (q.Count > 0)
        {
            var t = q.Pop();
            tr1.Add(t[1]);
            tr1.Remove(t[0]);
            tr2.Add(t[0]);
            tr2.Remove(t[1]);
            ans = Math.Min(ans, 1L * (tr1.Max - tr1.Min) * (tr2.Max - tr2.Min));
        }

        Write(ans);
    }
        public void GivenLotOfRandomDataWhenStuffTreapThenItWorks()
        {
            var treap = new Treap <double>();
            var rand  = new Random();

            for (int i = 0; i < 100_000; i++)
            {
                treap.Add(rand.NextDouble());
            }

            var previous = double.MinValue;

            foreach (var item in treap)
            {
                Assert.True(previous <= item);
                previous = item;
            }
        }
예제 #10
0
		public static void Test(){
			const int Count = 1*1000;

			var treap = new Treap<int>();
			Assert.AreEqual(treap.Count, 0);

			var rand = new Random(1);
			var items = Enumerable.Range(0, Count *2).Select(n=> rand.Next()).Distinct().Take(Count).ToArray();


			for(int i=0;i<items.Length;i++){
				treap.Add(items[i]);
				Assert.AreEqual(i+1, treap.Count);
				for(int j= 0;j<=i;j++)
					Assert.IsTrue(treap.Contains(items[j]));
			}
			Assert.LessOrEqual(treap.Count, Count);

			var x = treap.First();
			int count =1;
			foreach(var item in treap.Skip(1)){
				Assert.Greater(item, x);
				x = item;
				count++;
			}
			Assert.AreEqual(count, treap.Count);

			foreach(var item in items){
				int v;
				Assert.IsTrue(treap.TryGetValue(item, out v));
				Assert.AreEqual(item, v);
			}

			count = treap.Count;
			for(int i=0;i<items.Length;i++){
				treap.Remove(items[i]);
				count--;

				Assert.AreEqual(count, treap.Count);
				for(int j= 0;j<=i;j++)
					Assert.IsFalse(treap.Contains(items[j]));
			}

		}
예제 #11
0
		public static void TestPerformance(){
			const int Count = 1*1000*1000;
			var rand = new Random(1);
			var w = Stopwatch.StartNew();
			for(int i=0;i<Count;i++)
				rand.Next();
			Console.WriteLine("rand " + w.Elapsed);

			var items = Enumerable.Range(0, Count).Select(n=>rand.Next()).ToArray();
			var treap = new Treap<int>();
			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Add(item);
			Console.WriteLine("insert " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Contains(item);
			Console.WriteLine("lookup " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items){
				int v;
				treap.TryGetValue(item, out v);
			}
			Console.WriteLine("tryget " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
				treap.Remove(item);
			Console.WriteLine("remove " + w.Elapsed);

			w = Stopwatch.StartNew();
			foreach(var item in items)
			{ //
			}
			Console.WriteLine("iterate " + w.Elapsed);

		}
예제 #12
0
        static void Main(string[] args)
        {
            myData         data   = new myData();
            Treap <string> Treap1 = new Treap <string>();
            Treap <string> Treap2 = new Treap <string>();


            for (int i = 0; i < 20; i++)
            {
                if (i < 13)
                {
                    Treap1.Add(data.words[i]);
                }
                if (i > 10)
                {
                    Treap2.Add(data.words[i]);
                }
            }

            Treap1.Print();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Treap2.Print();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Treap <string> Treap12 = Treap1.Union(Treap1, Treap2);

            Treap12.Print();


            System.Console.ReadLine();
        }
예제 #13
0
        public static void Main(string[] args)
        {
            const int   count = 100;
            Treap <int> treap = new Treap <int>(Comparer <int> .Default);

            for (int i = 0; i < count; ++i)
            {
                treap.Add(i);
            }
            for (int i = 0; i < count; ++i)
            {
                treap.Add(~i);
            }
            //foreach (int item in treap)
            //{
            //    Console.Write(item);
            //    Console.Write(' ');
            //}
            OrderKeeper <int> keep = new OrderKeeper <int>(0);

            for (int i = 1; i < count; ++i)
            {
                keep.AddAfter(i - 1, i);
            }
            for (int i = 0; i < count; ++i)
            {
                keep.AddBefore(-i, ~i);
            }
            //foreach (int item in keep)
            //{
            //    Console.Write(item);
            //    Console.Write(' ');
            //}

            LinnormPRNG rng = new LinnormPRNG(1337);

            for (int i = 0; i < count; i++)
            {
                int idx1 = rng.Next(-count, count), idx2 = rng.Next(-count, count);
                Console.WriteLine("idx1 " + idx1 + " with path " + treap.Path(idx1) + ", idx2 " + idx2 + " with path " + treap.Path(idx2));
                if (keep.OrderOf(idx1, idx2) != treap.Before(idx1, idx2))
                {
                    Console.WriteLine("PROBLEM: idx1 " + idx1 + ", id2 " + idx2 + "; OrderKeeper gives " + keep.OrderOf(idx1, idx2) + "; Treap gives " + treap.Before(idx1, idx2));
                }
            }

            /*
             * start = DateTime.Now;
             * OrderKeeper<string> keeper;
             * List<string> items = new List<string>(count);
             * keeper = new OrderKeeper<string>("0");
             * for(int i = 1; i < count; ++i)
             * {
             *  keeper.AddAfter((i - 1).ToString(), i.ToString());
             * }
             * Console.WriteLine("ADD AT END done in " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms");
             #if EXTRA
             * Console.WriteLine("AT END:  Relabelings: " + keeper.relabelings);
             #endif
             * start = DateTime.Now;
             * keeper = new OrderKeeper<string>("0");
             * for(int i = 1; i < count; ++i)
             * {
             *  keeper.AddAfter(keeper.First, i.ToString());
             * }
             * Console.WriteLine("ADD AT START done in " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms");
             #if EXTRA
             * Console.WriteLine("AT START:  Relabelings: " + keeper.relabelings);
             #endif
             * items.Add("0");
             * start = DateTime.Now;
             * keeper = new OrderKeeper<string>("0");
             * for(int i = 1; i < count; ++i)
             * {
             *  string istr = i.ToString();
             *  keeper.AddAfter(items[items.Count / 2], istr);
             *  items.Add(istr);
             * }
             * Console.WriteLine("ADD IN MIDDLE done in " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms");
             #if EXTRA
             * Console.WriteLine("IN MIDDLE:  Relabelings: " + keeper.relabelings);
             #endif
             * items.Clear();
             * items.Add("0");
             * start = DateTime.Now;
             * keeper = new OrderKeeper<string>("0");
             * RNG random = new RNG(0xDADABAFFL);
             * for(int i = 1; i < count; ++i)
             * {
             *  string istr = i.ToString();
             *  keeper.AddAfter(items[random.Next(items.Count)], istr);
             *  items.Add(istr);
             * }
             * Console.WriteLine("ADD AT RANDOM done in " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms");
             #if EXTRA
             * Console.WriteLine("RANDOM:  Relabelings: " + keeper.relabelings);
             #endif
             *
             *
             * Console.WriteLine("Finished in " + DateTime.Now.Subtract(start).TotalMilliseconds + " ms");
             * //int size = keeper.Count;
             * //SortedSet<ulong> labels = new SortedSet<ulong>();
             * //Record<string> current;
             * //foreach(string s in keeper)
             * //{
             * //    current = keeper[s];
             * //    if(labels.Add(current.Label))
             * //    {
             * //        // this info takes way too long to print; commented out
             * //        //Console.WriteLine($"{s,-3}" + ": " + current.Label);
             * //    }
             * //    else
             * //    {
             * //        // shouldn't happen
             * //        Console.WriteLine("DUPLICATE LABEL FOR ITEM " + s + ": " + current.Label);
             * //        break;
             * //    }
             * //}
             * //foreach(string s in keeper)
             * //{
             * //    current = keeper[s];
             * //    if(current.Next.Equals(keeper.First)) break;
             * //    if(!keeper.OrderOf(s, current.Next.Item))
             * //    {
             * //        Console.WriteLine("BAD LABEL ORDER; ITEM " + s + " (LABEL " + current.Label + ") IS AFTER ITEM " + (current.Next.Item) + " (LABEL " + current.Next.Label + ")");
             * //        break;
             *
             * //    }
             * //}
             */
            Console.WriteLine("Press any key to close (maybe twice?)");
            Console.ReadKey();
        }
예제 #14
0
        public string RunTests()
        {
            Treap<int, double> _treetree = new Treap<int, double>();
               var vystup = "";
            double[] linked = new double[1000];

            vystup += "\nPridanie 100 prvkov";
            Random r = new Random();
            for (int a = 0; a < 1000; a++)
            {
                double a2 = r.Next(0, 100000000);
                _treetree.Add(a, a2);
                linked[a] = a2;
            }
            var velkostPredVymazanim = _treetree.Size;
            vystup += "\nBolo pridanych prvokov v treape (size of treap): " + _treetree.Size;
            vystup += "\nBolo pridanych prvokov v liste (size of liste - o polovicu menej): " + linked.Length;

            vystup += "\nVymazanie kazdy treti prvok\n ";
            int vymazanych = 0;
            for (int a = 0; a < 100; a++)
            {
                int cislo = random.Next(1, linked.Length);
                _treetree.Remove(linked[cislo]);
                vymazanych++;
                }
            vystup += "\nBolo vymazanych prvokov v treape (size of treap): " + vymazanych;
            vystup += "\nRozdiel prvokov v treape pred - terajsia velkost: " + velkostPredVymazanim + " , " + _treetree.Size  ;

            return vystup;
        }
예제 #15
0
        static public void Main()
        {
            // create MyObjs containing key and string data
            MyObj obj1 = new MyObj(0003, "MyObj C");
            MyObj obj2 = new MyObj(0001, "MyObj A");
            MyObj obj3 = new MyObj(0002, "MyObj B");
            MyObj obj4 = new MyObj(0004, "MyObj D");
            MyObj obj5 = new MyObj(0005, "MyObj E");

            try
            {
                // format: Add(key, value)
                treap.Add(new MyKey(obj1.Key), obj1);
                Console.WriteLine("Added to Treap, key: " + obj1.ToString());
                treap.Add(new MyKey(obj2.Key), obj2);
                Console.WriteLine("Added to Treap, key: " + obj2.ToString());
                treap.Add(new MyKey(obj3.Key), obj3);
                Console.WriteLine("Added to Treap, key: " + obj3.ToString());
                treap.Add(new MyKey(obj4.Key), obj4);
                Console.WriteLine("Added to Treap, key: " + obj4.ToString());
                treap.Add(new MyKey(obj5.Key), obj5);
                Console.WriteLine("Added to Treap, key: " + obj5.ToString());
                Console.WriteLine(Environment.NewLine);

                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Treap Values -");
                TreapEnumerator t = treap.Values();
                while (t.MoveNext())
                {
                    Console.WriteLine(((MyObj)(t.Value)).Data);
                }
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Treap Keys -");
                TreapEnumerator k = treap.Keys();
                while (k.MoveNext())
                {
                    Console.WriteLine(k.Key);
                }
                Console.WriteLine(Environment.NewLine);

                DumpMinMaxValue();
                Console.WriteLine(Environment.NewLine);

                // test Remove
                MyKey tObjKey = (MyKey)treap.GetMinKey();
                MyObj tObj    = (MyObj)treap.GetData(tObjKey);
                Console.WriteLine("Remove Min Key: " + tObj.ToString());
                Console.WriteLine(Environment.NewLine);
                treap.Remove(tObjKey);
                DumpTreap(false);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Remove Max Value:" + treap.GetMaxValue().ToString());
                treap.RemoveMax();
                Console.WriteLine("Remove Min Value:" + treap.GetMinValue().ToString());
                treap.RemoveMin();
                Console.WriteLine(Environment.NewLine);

                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Remove Min Key:" + ((MyKey)(treap.GetMinKey())).ToString());
                treap.RemoveMin();
                Console.WriteLine("Remove Max Key:" + ((MyKey)(treap.GetMaxKey())).ToString());
                treap.RemoveMax();

                Console.WriteLine(Environment.NewLine);
                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                // add some more and clear the treap
                Console.WriteLine("- Adding MyKeyObjs - ");
                Console.WriteLine(Environment.NewLine);

                MyKeyObj myKeyObj1 = new MyKeyObj(0025, "MyKeyObj W");
                MyKeyObj myKeyObj2 = new MyKeyObj(0023, "MyKeyObj X");
                MyKeyObj myKeyObj3 = new MyKeyObj(0026, "MyKeyObj Y");
                MyKeyObj myKeyObj4 = new MyKeyObj(0024, "MyKeyObj Z");
                treap.Add(myKeyObj1.Key, myKeyObj1);
                Console.WriteLine("Added to Treap, key: " + myKeyObj1.ToString());
                treap.Add(myKeyObj2.Key, myKeyObj2);
                Console.WriteLine("Added to Treap, key: " + myKeyObj2.ToString());
                treap.Add(myKeyObj3.Key, myKeyObj3);
                Console.WriteLine("Added to Treap, key: " + myKeyObj3.ToString());
                treap.Add(myKeyObj4.Key, myKeyObj4);
                Console.WriteLine("Added to Treap, key: " + myKeyObj4.ToString());
                Console.WriteLine(Environment.NewLine);

                TraverseEnumerator();
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("- Clearing Treap -");
                Console.WriteLine(Environment.NewLine);
                treap.Clear();
                DumpTreap(true);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("Press enter to terminate");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Press enter to terminate");
                Console.ReadLine();
            }
        }