java – Runtime error with a specific entry – Johnson Planning Algorithm

Two twins, James Johnson and Jonathan Johnson, work in a bicycle production plant. Their job is probably the most important: James attaches the front wheel and Jonathan does the same with the rear wheel. At the beginning of their work day, they receive construction kits including the front wheel, the rear wheel and the rest of the bike. It is known that:

  • for the ith building kit, James will attach the front wheel
    Ai seconds;
  • for the i th building kit, Jonathan will fix the rear wheel Bi
    in seconds;
  • James and Jonathan can not work simultaneously on the same bike.
  • the front wheel must be fixed before the rear wheel.

James and Jonathan are very experienced in fitting bikes. In fact, given the information on all the construction kits, they act optimally, so that the last wheel is fixed as soon as possible. However, the new director of the factory is not yet experienced and so she needs help. Please write a program that tells you how fast the twins are assembling all the current bikes and how exactly that could be done.

Contribution

The first line of the input file contains N, the number of bike building kits.

The second line contains N integers, the i-th of them equals Ai. The third line also contains N integers, whose i-th is equal to Bi. All Ai and Bi are positive and do not exceed 10 ^ 9.

Exit

In the first line, indicate the minimum time required to assemble all the bikes.

The second line must contain integers. Among them, the i-th integer indicates the moment when James begins to assemble the i-th construction kit. The third line must contain integers, this time for Jonathan. If there are several optimal scenarios, indicate one.

Examples

input.txt 3

1 2 3

2 1 3

output.txt 8

0 4 1

1 7 4

The problem is that we have to take out the start time of each process. My code works perfectly (in almost all cases), but in some cases it generates a runtime error. Here is the hint of the judge system:

Hint: it is a random test with n = 8000, max (A_i, B_i) = 2, such that the sum A_i = the sum B_i

I have also written a unit test for the index, I have run it for several hours and I still have no errors.

That's the code:

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;

Public class Week3Exercise8 {

Static class reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;

public player () {
din = new DataInputStream (System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public drive (String file_name) throws IOException {
din = new DataInputStream (new FileInputStream (filename));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public String readLine () throws IOException {
byte[] buf = new byte[64]; // length of the line
int cnt = 0, c;
while ((c = read ())! = -1) {
if (c == ###)
Pause;
buf[cnt++] = (byte) c;
}
returns a new string (buf, 0, cnt);
}

public int nextInt () throws IOException {
int ret = 0;
byte c = read ();
while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= & # 39; 0 & # 39; && c <= & # 39; 9 & # 39;);

if (neg)
return -ret;
return ret;
}

public long nextLong () throws IOException {
long ret = 0;
byte c = read ();
while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= & # 39; 0 & # 39; && c <= & # 39; 9 & # 39;);
if (neg)
return -ret;
return ret;
}

fillBuffer () empty private throws IOException {
bytesRead = din.read (buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}

readte private byte throws IOException {
if (bufferPointer == bytesRead)
fillBuffer ();
return buffer[bufferPointer++];
}

public void close () throws IOException {
if (din == null)
return;
din.close ();
}
}

static class Job implements Comparable {
long a;
long b;
int index;

public work (long a, long b, int index) {
this.a = a;
this.b = b;
this.index = index;
}

@Override
public int compareTo (Job that) {
long minThis = Math.min (this.a, this.b);
long minThat = Math.min (que.a, que.b);
if (minThis == minThat) {
if (this.a == that.a) {
return Long.compare (that.b, this.b);
} else if (this.b == that.b) {
return Long.compare (that.a, this.a);
}
}

return Long.compare (minThis, minThat);
}

@Override
public channel toString () {
returns "(" + a + "," + b + ") -" + index;
}
}

public static empty solve (long[] a long[] b, int[] order) throws IOException {
BufferedWriter bw = new BufferedWriter (new FileWriter ("output.txt"));
LinkedList l1 = new LinkedList <> ();
LinkedList l2 = new LinkedList <> ();
for (int i = 0; i <order.length; ++ i) {
int o = order[i];
if a[o] <b[o] || a[o] == b[o])
l1.addLast (o);
other
l2.addFirst (o);
}

order = new int[a.length];
int k = 0;
for (int o: l1)
order[k++] = o;
for (int o: l2)
order[k++] = o;

long[][]    start = new long[2][a.length];
int prev = -1;
for (int i: order) {
if (prev == -1) {
beginning[0][i]    = 0;
beginning[1][i]    = one[i];
} other {
beginning[0][i]    = start[0][prev]    + one[prev];
beginning[1][i]    = Math.max (start[1][prev]    + b[prev]start[0][i]    + one[i])
}

prev = i;
}

long time = start[1][prev]    + b[prev];
bw.write (time + " n");
System.out.println (time);
for (int i = 0; i <start.length; ++ i) {
StringBuilder sb = new StringBuilder ();
for (int j = 0; j <start[0].length; ++ j)
sb.append (begin[i][j]    + "");
bw.write (sb.toString (). trim () + " n");
}
pc.close ();
}

public static void main (String[] args) throws IOException {
Reader Reading = new Reader ("input.txt");
long n = scan.nextInt ();
Employment[] jobs = new Job[(int) n];
long[] a = new long[(int) n];
long[] b = new long[(int) n];
for (int i = 0; i <n; ++ i) {
a[i] = scan.nextLong ();
}
for (int i = 0; i <n; ++ i) {
b[i] = scan.nextLong ();
}
for (int i = 0; i <n; ++ i) {
jobs[i] = new work (a[i]b[i], I);
}
Arrays.sort (jobs);
int[] order = new int[(int) n];
for (int i = 0; i <n; ++ i) {
order[i] = jobs[i].index;
}
solve (a, b, order);
}

}