Skip to content

Instantly share code, notes, and snippets.

@bhi5hmaraj
Last active May 29, 2017 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhi5hmaraj/9f3821e78701e42aa2d6a524b7b7a188 to your computer and use it in GitHub Desktop.
Save bhi5hmaraj/9f3821e78701e42aa2d6a524b7b7a188 to your computer and use it in GitHub Desktop.
Stresstester for CONSENK
import java.util.*;
import java.io.*;
class CONSESNK {
/************************ SOLUTION STARTS HERE ************************/
static int N;
static long L;
static long arr[];
static long f(long X) {
long cost = 0;
for(int i = 0; i < N; i++)
cost += Math.abs(arr[i] - (X + (1L * L * i)));
return cost;
}
private static void solve() {
int T = nextInt();
while(T-->0) {
N = nextInt();
L = nextLong();
long A = nextLong();
long B = nextLong();
arr = nextLongArray(N);
Arrays.sort(arr);
long maxX = B - (L * N);
long lo = A , hi = maxX - 1;
long opt = A;
while(lo <= hi) {
long mid = (lo + hi) / 2L;
long c = f(mid);
long cc = f(mid + 1);
if(c >= cc) {
opt = mid + 1;
lo = mid + 1;
} else
hi = mid - 1;
}
println(f(opt));
}
}
/************************ SOLUTION ENDS HERE ************************/
/************************ TEMPLATE STARTS HERE **********************/
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
st = null;
solve();
reader.close();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer st;
static String next()
{while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}
st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
static String nextLine() {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}
static int nextInt() {return Integer.parseInt(next());}
static long nextLong() {return Long.parseLong(next());}
static double nextDouble(){return Double.parseDouble(next());}
static char nextChar() {return next().charAt(0);}
static int[] nextIntArray(int n) {int[] a= new int[n]; int i=0;while(i<n){a[i++]=nextInt();} return a;}
static long[] nextLongArray(int n) {long[]a= new long[n]; int i=0;while(i<n){a[i++]=nextLong();} return a;}
static int[] nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}
static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}
static void print(Object o) { writer.print(o); }
static void println(Object o){ writer.println(o);}
/************************ TEMPLATE ENDS HERE ************************/
}
import java.util.*;
import java.io.*;
public class Stresstester_CONSENK
{
/************************ SOLUTION STARTS HERE ***********************/
private static void generator(OutputStream file , OutputStream judge , OutputStream cand){ //Writes a single instance for a test case
/*
* This is the random test generator which generates a single instance
* use input object for writing into input.txt
*
*
*/
Random rand = new Random();
StringBuilder dump = new StringBuilder();
dump.append(T + "\n");
for(int i = 1; i <= T; i++) {
int N = N_MAX;
int L = 1 + rand.nextInt(L_MAX);
int A = 1 + rand.nextInt(B_MAX - (N * L) - 1);
int B = A + (N * L) + rand.nextInt(B_MAX - (A + (N * L)));
dump.append(N + " " + L + " " + A + " " + B + "\n");
while(N-->0)
dump.append((1 + rand.nextInt(S_MAX)) + " ");
dump.append("\n");
if(A > B || N * L > B - A)
throw new RuntimeException("wrong input " + dump);
}
byte temp[] = dump.toString().getBytes();
try {
file.write(temp);
judge.write(temp);
cand.write(temp);
file.close();
judge.close();
cand.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static int JUDGE(InputStream judge, InputStream cand) {
BufferedReader correct = new BufferedReader(new InputStreamReader(judge));
BufferedReader unchecked = new BufferedReader(new InputStreamReader(cand));
String read = null;
String A = "";
String B = "";
int line = 0;
try {
while ((read = correct.readLine()) != null) {
line++;
String read2 = unchecked.readLine();
if (!read.equals(read2)) {
return line;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
private static String getString(InputStream stream) {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String read = null;
try {
while((read = br.readLine()) != null)
sb.append(read);
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
static int runs = 5; //Number of times to run the stresstest bed
static int T = 10;
static int N_MAX = 5;
static int L_MAX = 10;
static int S_MAX = 100;
static int B_MAX = 200;
/*
Instructions
1). compile CONSENK.java and get the .class file
2). Change the directory variable to point to the directory of the class file
3). Fill the command in line 101 (The path should be absolute)
4). Tweak the limits
5) compile and run the stresstester
*/
static final String directory = ""; // change to the directory which contains the class file and also the program to be tested
static final String judgeClass = "CONSESNK";
public static void main(String []args) throws IOException {
while(runs-->0) {
Process judgeProcess = Runtime.getRuntime().exec("java -cp " + directory + " " + judgeClass);
Process candProcess = Runtime.getRuntime().exec("command to run the program to be checked");
generator(new FileOutputStream("input.txt"), judgeProcess.getOutputStream(), candProcess.getOutputStream());
try {
judgeProcess.waitFor();
candProcess.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
int ret = JUDGE(judgeProcess.getInputStream(), candProcess.getInputStream());
if(ret > 0) {
System.err.println("WA at line " + ret);
System.exit(1);
}
else {
System.out.println("PASS " + runs);
}
}
System.out.println("ALL TESTS PASSED !!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment