You are not logged in. Please login at www.codechef.com to post your questions!

×

What is the problem with my code?

using System;
using System.Text;

namespace Orders
{
    class Program
    {
        static int Main(string[] args)
        {
            int numberOfTestCases = Convert.ToInt16(Console.ReadLine());
            for (int i = 0; i < numberOfTestCases; i++)
            {
                int size = Convert.ToInt16(Console.ReadLine());
                int[] arr = new int[size];
                for (int k = 0; k < size; k++)
                {
                    StringBuilder sb = new StringBuilder();
                    char c;
                    while (true)
                    {
                        c = Convert.ToChar(Console.Read());
                        if (c == ' ' || c == '\n')
                            break;
                        sb.Append(c);
                    }
                    arr[k] = Convert.ToInt16(sb.ToString());
                }
                //Input completed
                MyLinkedList stack = new MyLinkedList(arr);
                stack.reArrange();
                stack.populateRank();
                stack.printRank();
            }

            return 0;
        }
    }

    class MyLinkedList
    {
        public MyLinkedList(int[] arr)
        {
            Head = null;
            Tail = null;
            Count = 0;
            rank = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                this.addNode(arr[i], i);
            }
        }

        public int[] rank;

        public class Node
        {
            public int val;
            public int mov;
            public Node left;
            public Node right;
        }

        public Node Head
        {
            get;
            set;
        }

        public Node Tail
        {
            get;
            set;
        }

        public int Count
        {
            get;
            set;
        }

        public void addNode(int n, int k)
        {
            Node node = new Node { val = k, mov = n };
            if (this.Count == 0)
            {
                this.Head = node;
                this.Tail = node;
                this.Count++;
            }
            else
            {
                node.left = this.Tail;
                node.right = this.Head;
                this.Head.left = node;
                this.Tail.right = node;
                this.Tail = node;
                this.Count++;
            }
        }

        public void populateRank()
        {
            Node temp = this.Head;
            for (int i = 0; i < this.Count; i++)
            {
                rank[temp.val] = i + 1;
                temp = temp.right;
            }
        }

        public void printRank()
        {
            string str = "";
            for (int i = 0; i < rank.Length - 1; i++)
            {
                str += rank[i].ToString() + " ";
            }
            str += rank[rank.Length-1].ToString();
            Console.WriteLine(str);
        }

        public void reArrange()
        {
            Node cur = this.Head;
            int i = 0;
            while (i<this.Count)
            {
                Node temp = cur;
                for (int j = 0; j < cur.mov; j++)
                {
                    temp = temp.left;
                }
                i++;
                i--;
                if (cur == temp)
                {
                    i++;
                    cur = cur.right;
                    continue;
                }
                Node k = cur.right;
                addBefore(temp, cur);
                cur = k;
                i++;
            }

        }

        private void addBefore(Node n1, Node n2)
        {
            //remove n2
            if (n1 == this.Head)
            {
                this.Head = n2;
            }
            if (n2 == this.Tail)
            {
                this.Tail = n2.left;
            }
            n2.left.right = n2.right;
            n2.right.left = n2.left;
            //add n2
            n1.left.right = n2;
            n2.left = n1.left;
            n1.left = n2;
            n2.right = n1;
        }

    }
}

asked 30 Mar '12, 11:50

neeraj080's gravatar image

3★neeraj080
1222
accept rate: 0%

edited 25 Jun '12, 12:45

admin's gravatar image

0★admin ♦♦
19.3k348495534


I'm getting runtime error (OTHERS)

link

answered 30 Mar '12, 11:51

neeraj080's gravatar image

3★neeraj080
1222
accept rate: 0%

please provide us a link toward the problem statement.

(30 Mar '12, 18:02) cyberax ♦2★
if (c == ' ' || c == '\n')

maybe you should rather check if c is not a digit. you can't be really sure of what the input string is made (there could be '\t' or '\r' in the line, even alpha chars...). it's a hard-to-detect problem, as when we generate sample test cases ourselves, the code is obviously written in order to parse the data correctly. but when submitted to the online judge, who knows what can happen. :) please tell us if you get AC with that. otherwise, i'll try to go deeper into your code. good luck.

link

answered 31 Mar '12, 00:20

cyberax's gravatar image

2★cyberax ♦
3.4k21955
accept rate: 20%

edited 31 Mar '12, 00:21

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×496
×58

question asked: 30 Mar '12, 11:50

question was seen: 1,118 times

last updated: 25 Jun '12, 12:45