ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1136. Parliament

always get Runtime error (access violation) C#
Posted by ArtemArtem 31 Jul 2015 16:31
On my machine all work fine, I'll delete code after I get AC. I think I allow wrong input, stackoverflow, more then 3000 number.


Here code
private static void Main(string[] args)
        {
            StringBuilder builder = new StringBuilder();
            int count; //= int.Parse(Console.ReadLine().Trim(' ', '\t', '\n','\r'));
            //if (count == 0)
            //    return;
            List<int> val = new List<int>();
            string text = "";
#if !Test
            while((text = Console.ReadLine()) != "")
            {
                builder.Append(text.Trim());
                //val.AddRange(text.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList());
            }
#else
            Random rand = new Random();
            builder.AppendLine(3000.ToString());
            for (int i = 0; i < 3000; i++)
                builder.AppendLine(rand.Next(0, 65535).ToString());
#endif
            val = builder.ToString().Split(new[] { ' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
            if (val.Count <= 0 || val[0] <= 0)
                return;
            count = val[0];
            BinnaryTreeNode tree = new BinnaryTreeNode();
            //if (val.Count == 1)
            //{
            //    tree.value = val[0];
            //    for (int i = 0; i < count - 1; i++)
            //    {
            //        tree = CreateTree(int.Parse(Console.ReadLine()), tree);
            //    }
            //}
            //else
            {
                tree.value = val[0];
                for (int i = 1; i < val.Count && i < 3001; i++)
                    tree = CreateTree(val[i], tree);
            }

            RightToLeft(tree);
        }
       static Stack<BinnaryTreeNode> stackNodes = new Stack<BinnaryTreeNode>();
        static Stack<int> values = new Stack<int>();
        static void RightToLeft(BinnaryTreeNode node)
        {
            // stackNodes.Push(node);
            // while(stackNodes.Count > 0)
            // {
            //     var nn = stackNodes.Pop();
            //     if (nn.Right != null)
            //         stackNodes.Push(nn.Right);
            //     if (nn.Left != null)
            //         stackNodes.Push(nn.Left);

            //     values.Push(nn.value);
            // }

            //for (int i = 0; i < values.Count; i++)
            // {
            //     Console.WriteLine(values.Pop());
            // }
            if (node == null)
                return;
            if (node.Right != null)
                RightToLeft(node.Right);
            if (node.Left != null)
                RightToLeft(node.Left);
            Console.WriteLine(node.value);
        }

       static  BinnaryTreeNode CreateTree(int value, BinnaryTreeNode node)
        {
            if (value > node.value)
            {
                var parentNode = new BinnaryTreeNode() { value = value };
                parentNode.Left = node;
                node.Parent = parentNode;
                return parentNode;
            }
            BinnaryTreeNode tempNOde = node;
            while(tempNOde.Left != null)
            {
                if (tempNOde.Left.value < value)
                {
                    var parentNode = new BinnaryTreeNode() { value = value };
                    parentNode.Right = node;
                    parentNode.Left = tempNOde.Left;
                    tempNOde.Left = null;
                    parentNode.Left.Parent = parentNode;
                    parentNode.Right.Parent = parentNode;
                    return parentNode;
                }
                tempNOde = tempNOde.Left;
            }
            return node;

        }

        class BinnaryTreeNode
        {
            public int value;
            public BinnaryTreeNode Left;
            public BinnaryTreeNode Right;
            public BinnaryTreeNode Parent;

            public override string ToString()
            {
                return value.ToString();
            }
        }
Re: always get Runtime error (access violation) C#
Posted by mberdyshev 12 Sep 2016 15:33
I suppose your program fails because of a recursion limit