哪个测试案例导致了运行时错误?

huangapple 未分类评论46阅读模式
英文:

Which test case causes the Runtime Error?

问题

import java.util.*;
import java.io.*;

// Parenting Partnering Returns
public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = in.nextInt();
        for (int ijk = 1; ijk <= t; ++ijk) {

            int N = in.nextInt();
            String ans;
            StringBuilder sb = new StringBuilder();
            boolean[] cal = new boolean[1442];
            boolean[] jal = new boolean[1442];
            int si, se;
            boolean canC, canJ, isPossible = true;

            for (int i = 0; i < N; i++) {
                si = in.nextInt();
                se = in.nextInt();
                canC = false;
                canJ = false;

                if (cal[si + 1] == false && cal[se] == false || cal[si] == false && cal[se - 1] == false) {
                    for (int j = si; j <= se; j++) cal[j] = true;
                    sb.append('C');
                    canC = true;
                    continue;
                } else if (jal[si + 1] == false && jal[se] == false || jal[si] == false && jal[se - 1] == false) {
                    for (int k = si; k <= se; k++) jal[k] = true;
                    sb.append('J');
                    canJ = true;
                    continue;
                } else {
                    isPossible = false;
                    break;
                }
            }

            ans = sb.toString();

            if (isPossible)
                System.out.println("Case #" + ijk + ": " + ans);
            else
                System.out.println("Case #" + ijk + ": " + "IMPOSSIBLE");
        }
        //in.close();
    }

}
英文:

This was one of coding jam 2020 questions with the link attached below (round is complete), and while I could not reproduce it, the google engine reported that this was a runtime error even for the small test case. It could pretty much handle any test case I threw at it. Perhaps I am missing an edge case, but not sure what it is.

My understanding is that this is O(N) performance. All it really does is to check if the boolean sub array is free(false) and if so, assign the task to that sub array(true) with may be 2N compares.

https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/000000000020bdf9

import java.util.*;
import java.io.*;

//Parenting Partnering Returns
public class Solution {

	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
		for (int ijk = 1; ijk &lt;= t; ++ijk) {
			
			int N = in.nextInt();
			String ans;
			StringBuilder sb = new StringBuilder();
			boolean[] cal = new boolean[1442];
			boolean[] jal = new boolean[1442];
			int si, se;
			boolean canC,canJ, isPossible = true;

			for (int i = 0 ; i &lt; N ; i++) {
				si = in.nextInt();
				se = in.nextInt();
				canC = false; canJ = false;

				if (cal[si + 1] == false &amp;&amp; cal[se] == false || cal[si] == false &amp;&amp; cal[se -1] == false) {
					for (int j = si ; j &lt;= se; j++) cal[j] = true;
					sb.append(&#39;C&#39;); canC = true; continue;

				} else if (jal[si + 1] == false &amp;&amp; jal[se] == false || jal[si] == false &amp;&amp; jal[se -1] == false) {
					for (int k = si ; k &lt;= se; k++) jal[k] = true;
					sb.append(&#39;J&#39;); canJ = true; continue;
				} else {
					isPossible = false;
					break;
				}
			}

			ans = sb.toString();

			if(isPossible)
			System.out.println(&quot;Case #&quot; + ijk + &quot;: &quot; + ans);
			else System.out.println(&quot;Case #&quot; + ijk + &quot;: &quot; + &quot;IMPOSSIBLE&quot;);
		}
		//in.close();
	}

}

huangapple
  • 本文由 发表于 2020年4月8日 22:11:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61102764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定