RahulAhuja2901's blog

By RahulAhuja2901, history, 22 months ago, In English

I was solving the problem 1561C Deep Down Below where I was using Binary Search on Answer. Initially I was getting Wrong Answer on Test Case 14 so I double checked my code but was unable to find any error.

Link to my Submission — https://mirror.codeforces.com/problemset/submission/1561/193939724

So after a lot of Wrong Submissions I removed the Try-Catch Block and then I got Runtime Error (Exit Code is 11).

Link to my Submission — https://mirror.codeforces.com/problemset/submission/1561/193939801

Then I replaced "out.println ()" with "System.out.println ()" and I again got Wrong Answer on Test Case 14 saying "Answer contains longer sequence [length = 1000], but output contains 750 elements" so at some point my code is going through a runtime error but I am not able to figure it out.

Link to my Submission — https://mirror.codeforces.com/problemset/submission/1561/193939951

Link to the Problem — https://mirror.codeforces.com/problemset/problem/1561/C

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
22 months ago, # |
Rev. 3   Vote: I like it +2 Vote: I do not like it

I tried to use Arraylist< Pair> instead [submission:194002572]of long[][] to sort, and got AC. My submission:194002572

Part of my code:

final class Pair implements Comparable<Pair> {
	long x;
	long y;

	Pair(long x, long y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}

	@Override
	public int hashCode() {
		return Long.hashCode(x + y + (y << 16));
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Pair other = (Pair) obj;
		return x == other.x && y == other.y;
	}

	@Override
	public int compareTo(Pair o) {
		if (this.x < o.x)
			return -1;
		if (this.x > o.x)
			return 1;
		if (this.y < o.y)
			return -1;
		if (this.y > o.y)
			return 1;
		return 0;
	}
}
»
22 months ago, # |
  Vote: I like it +1 Vote: I do not like it

And this is the Error messege of the original submission: (You can see it by adding e.printStackTrace(System.out); in the catch block.

java.lang.IllegalArgumentException: Comparison method violates its general contract!
	at java.util.TimSort.mergeHi(TimSort.java:899)
	at java.util.TimSort.mergeAt(TimSort.java:516)
	at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
	at java.util.TimSort.sort(TimSort.java:254)
	at java.util.Arrays.sort(Arrays.java:1438)
	at Deep_Down_Below.SortByColumn(Deep_Down_Below.java:146)
	at Deep_Down_Below.main(Deep_Down_Below.java:244)
»
22 months ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

By searching Comparison method violates its general contract! on the internet, I've found the reason of the java.lang.IllegalArgumentException:

In java7, a valid Comparator must satisfy the reflexivity (compare(a, a)==0), if any comparator violates this conditions (which means it doesn't consider the case when a==b), Arrays.sort() method will throw java.lang.IllegalArgumentException.

»
22 months ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Therefore, we can fix the original solution by modify the compare method: (AC submission:194005512)

public static void SortByColumn (long arr[][])
    {
        Arrays.sort (arr, new Comparator<long[]> () {
            public int compare (long[] arr1, long[] arr2)
            {
                if (arr1[0] > arr2[0])
                {
                    return 1;
                }
                else if (arr1[0] < arr2[0])
                {
                    return -1;
                }
                else
                {
                    if (arr1[1] > arr2[1])
                    {
                        return 1;
                    }
                    else if(arr1[1] < arr2[1])
                    {
                        return -1;
                    }
                    else return 0;
                }
            }
        });
    }
  • »
    »
    22 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you so much for your reply. I really appreciate you taking the time to write such an informative and helpful message.