diff --git a/README.md b/README.md index 662651b..cb10713 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,4 @@ Find release dates at https://javaarchive.github.io/Java/. Moved to https://gith # Important 2019 update, goes into effect 12/14/2018 The usacotools java library will be offically moved to it's own repositroy. Link is https://github.com/javaarchive/java-tools/tree/master # Maintaining -Sorry everyone! I only upload my usaco files for now +I only upload my usaco files for now...check out JavaProjects for other stuff diff --git a/convention.java b/convention.java new file mode 100644 index 0000000..a784fc5 --- /dev/null +++ b/convention.java @@ -0,0 +1,91 @@ + +import java.io.*; +import java.util.*; +public class convention { + public static boolean check(int test,int M, int C, List buses) { + int busStart=buses.get(0); + int busCount = 1; + int cowCount = 0; + for(int i = 0; i < buses.size(); i ++) { + if(busCount > M) { + return false; + } + int time = buses.get(i); + if(time - busStart > test || cowCount == C) { + busCount ++; + busStart = time; + cowCount = 0; + } + cowCount++; + } + return true; + } + public static void endProgram(int answer) throws IOException{ + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("convention.out"))); + pw.println(answer); + pw.close(); + System.exit(0); + } + public static void main(String[] args) throws IOException{ + BufferedReader f = new BufferedReader(new FileReader("convention1.in")); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N,M,C; + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + C = Integer.parseInt(st.nextToken()); + st = new StringTokenizer(f.readLine()); + f.close(); + System.out.println(N+" "+M+" "+C); + List arrivTime = new ArrayList<>(); + for(int i = 0; i < N; i ++) { + arrivTime.add(Integer.parseInt(st.nextToken())); + } + arrivTime.sort(null); + // 123456789 + int max = 1000000000; + int min = 0; + while(true) { + int mid = (int) Math.floor((min + max)/2); + if(check(mid,M,C,arrivTime)) { + max = mid; + }else { + min = mid; + } + System.out.println(min+" "+mid+" "+max); + } + } + +} + +class Cow implements Comparator{ + int id; + int aTime; + public Cow(int id, int aTime) { + this.id = id; + this.aTime = aTime; + } + @Override + public int compare(Cow arg0, Cow arg1) { + // TODO Auto-generated method stub + return Integer.compare(arg0.aTime, arg1.aTime); + } +} +class PriorityElem implements Comparator, Comparable{ + public int multiplier = 1; + public int priority; + public int item; + public PriorityElem(int item, int priority) { + this.item = item; + this.priority = priority; + } + @Override + public int compare(PriorityElem arg0, PriorityElem arg1) { + // TODO Auto-generated method stub + return Integer.compare(arg0.priority, arg1.priority); + } + @Override + public int compareTo(PriorityElem arg0) { + + return this.compare(this, arg0); + } +} \ No newline at end of file diff --git a/cowjump.java b/cowjump.java index cbf5e89..2e7c5e0 100644 --- a/cowjump.java +++ b/cowjump.java @@ -31,8 +31,8 @@ public static void testIntersections() { assert linesCompare(new LineSegement(new Point(0,0), new Point(2,3)),new LineSegement(new Point(0,3),new Point(9,1))) == -1; System.out.println("All Tests OK!"); } - - public static boolean sweepCheck(LineSegement s,Point[][] input) { + + public static boolean sweepCheck(LineSegement s,Point[][] input) { for(int i = 0; i < input.length; i ++) { System.out.println("Checking line "+i); if(input[i][0] == null && input[i][1] == null) { @@ -68,8 +68,8 @@ public static boolean sweepCheck(LineSegement s,Point[][] input) { System.out.println("No Intersection!"); } } - - } + + } return false; } public static void main(String[] args) throws IOException{ @@ -103,8 +103,8 @@ public static void main(String[] args) throws IOException{ if(status) { break; } - - input[i][0] = a; + + input[i][0] = a; input[i][1] = b; /*for(int j = 0; j < i; j ++) { if(Point.intersection(input[j][0], input[j][1], input[i][0], input[i][1])) { @@ -198,11 +198,17 @@ public static void main(String[] args) throws IOException{ pw.println(output+1); pw.close(); System.exit(0); + + } +<<<<<<< HEAD <<<<<<< HEAD } } +======= +} +>>>>>>> parent of 84bd17b... Merge branch 'master' of https://github.com/javaarchive/Java class Point{ double x,y; ======= @@ -344,7 +350,7 @@ public int compareTo(Point arg0) { } } - class LineSegement { +class LineSegement { Point a,b; public LineSegement(Point a,Point b) { if(a.x > b.x) { @@ -385,4 +391,4 @@ public Point atX_(double x) { public String toString() { return this.a.toString() + " -- "+this.b.toString(); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/dining.java b/dining.java new file mode 100644 index 0000000..c1178e9 --- /dev/null +++ b/dining.java @@ -0,0 +1,135 @@ +import java.io.*; +import java.util.*; +public class dining { + public static int[] dijkstra(Map cost, List> graph, final int N, int source) { + int[] dist = new int[N]; + PriorityQueue visited = new PriorityQueue<>(N,new Comparator() { + @Override + public int compare(Integer arg0, Integer arg1) { + return Integer.compare(dist[arg0], dist[arg1]); + //return 0; + } + }); + + Arrays.fill(dist, Integer.MAX_VALUE); + dist[N-1] = 0; // Starting value + //nodeQueue.add(N-1); + visited.add(source); + int u; + while(!visited.isEmpty()) { + //int u = nodeQueue.remove(); // Get next node + u = visited.remove(); + List adj = graph.get(u); + for(int node:adj) { + //if(dist[node] == Integer.MAX_VALUE) + { // If this node has not been visited yet + int edgeDist = cost.get(new Pair(u,node)); + int totalDist = dist[u] + edgeDist; + if (totalDist < dist[node]) + { + dist[node] = totalDist; + visited.add(node); + } + //nodeQueue.add(node); + } + } + } + return dist; + } + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("1.in")); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N,M,K; + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + final int numOfCows = N - 1; + List> graph = new ArrayList<>(M+1); + for(int i = 0; i < M+1; i ++) { + graph.add(new ArrayList<>()); + } + Map cost = new HashMap<>(); + for(int i =0 ; i < M; i ++) { + st = new StringTokenizer(f.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + int edgeCost = Integer.parseInt(st.nextToken()); + graph.get(start-1).add(end-1); + graph.get(end-1).add(start-1); + cost.put(new Pair(start-1,end-1), edgeCost); + cost.put(new Pair(end-1,start-1), edgeCost); + } + //System.out.println("Cost : "+cost); + Map costWithHaybales = new HashMap<>(cost); + System.out.println("Graph as an edgelist: "+graph); + int[] taste = new int[K]; + List bales = new ArrayList<>(); + for(int i = 0; i < K; i ++) { + st = new StringTokenizer(f.readLine()); + int index = Integer.parseInt(st.nextToken()) - 1; + bales.add(index); + int value = Integer.parseInt(st.nextToken()); + taste[i] = value; + } + f.close(); + //int[] empty = new int[N]; + //Arrays.fill(empty, Integer.MAX_VALUE); + //empty[N-1] = 0; + int[] distOrig,distTo; + distOrig = dijkstra(cost, graph, N, N - 1); + //for(int bale: graph.get(N-1)) { + // graph.get(bale).remove((Object) (N-1)); + //} + //graph.get(N-1).clear(); + for(int i = 0 ; i < K; i++) { + int target = bales.get(i); + graph.get(target).add(N); + graph.get(N).add(target); + //System.out.println("Dist: "+distOrig[target]+" Taste: "+taste[i]); + //System.out.println(target+" -- "+(N-1) + " connected with weight "+(distOrig[target] - taste[i])); + costWithHaybales.put(new Pair(target,N), distOrig[target] - taste[i]); + //costWithHaybales.put(new Pair(N,target), distOrig[target] - taste[i]); + } + //System.out.println("Modified New Graph : "+graph); + System.out.println("Cost : "+cost); + System.out.println("Cost with haybales: "+costWithHaybales); + distTo = dijkstra(costWithHaybales, graph, N+1, N); + System.out.println("Output :" + Arrays.toString(distOrig)); + System.out.println("Output(haybales):" + Arrays.toString(distTo)); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("dining.out"))); + for(int i =0 ; i < numOfCows; i ++) { + if(distTo[i] <= distOrig[i]) { + pw.println(1); + }else { + pw.println(0); + } + } + pw.close(); + } +} +// Order does matter pair +class Pair{ + int x,y; + public Pair(int x,int y) { + this.x =x; + this.y =y; + } + @Override + public String toString() { + return "("+this.x+","+this.y+")"; + } + @Override + public boolean equals(Object obj) { + if(obj instanceof Pair) { + Pair p = (Pair) obj; + if((p.x == this.x && p.y == this.y)) { + return true; + } + } + return false; + } + @Override + public int hashCode(){ + return Integer.hashCode(this.x) * (Integer.hashCode(this.y)+1); + } +} \ No newline at end of file diff --git a/meetings.in b/meetings.in new file mode 100644 index 0000000..e3ab96c --- /dev/null +++ b/meetings.in @@ -0,0 +1,4 @@ +3 5 +1 1 1 +2 2 -1 +3 3 -1 \ No newline at end of file diff --git a/meetings.java b/meetings.java new file mode 100644 index 0000000..90d1956 --- /dev/null +++ b/meetings.java @@ -0,0 +1,38 @@ +import java.io.*; +import java.util.*; +public class meetings { + + public static void main(String[] args) throws IOException{ + // TODO Auto-generated method stub + BufferedReader f= new BufferedReader(new FileReader("meetings.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("meetings.in"))); + int N, L; + StringTokenizer st = new StringTokenizer(f.readLine()); + N = Integer.parseInt(st.nextToken()); + L = Integer.parseInt(st.nextToken()); + List c = new ArrayList<>(); + for(int i = 0 ; i < N ; i++) { + int w = Integer.parseInt(st.nextToken()); + int x = Integer.parseInt(st.nextToken()); + int d = Integer.parseInt(st.nextToken()); + c.add(new Cow(x,d,w)); + } + // Simulate + while(true) { + break; // Temporary to prevent infinite loop + } + pw.close(); + f.close(); + } + +} +class Cow{ + int pos; + int velocity; + int weight; + public Cow(int pos, int v, int w) { + this.weight = w; + this.velocity = v; + this.pos = pos; + } +} \ No newline at end of file diff --git a/meetings.out b/meetings.out new file mode 100644 index 0000000..78c6bae --- /dev/null +++ b/meetings.out @@ -0,0 +1 @@ +2 diff --git a/milkvisits.in b/milkvisits.in new file mode 100644 index 0000000..b8cbd0b --- /dev/null +++ b/milkvisits.in @@ -0,0 +1,11 @@ +5 5 +HHGHG +1 2 +2 3 +2 4 +1 5 +1 4 H +1 4 G +1 3 G +1 3 H +5 5 H \ No newline at end of file diff --git a/milkvisits.java b/milkvisits.java new file mode 100644 index 0000000..06c9535 --- /dev/null +++ b/milkvisits.java @@ -0,0 +1,98 @@ +import java.io.*; +import java.util.*; +public class milkvisits { + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("milkvisits.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("milkvisits.out"))); + StringTokenizer st = new StringTokenizer(f.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + char[] lookup = f.readLine().toCharArray(); + Map> connections = new HashMap<>(); + for(int i =1 ; i < (N+1); i ++) { + connections.put(i, new ArrayList<>()); + } + for(int i = 0; i < N-1; i ++) { + st = new StringTokenizer(f.readLine()); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + connections.get(x).add(y); + connections.get(y).add(x); + } + int[] seg = new int[N]; + int segNum = 1; + Stack ts = new Stack<>(); + + + //int test = 0; + //char target = lookup[test]; + + for(int i = 1; i < (N+1); i ++) { + segNum++; + if(seg[i-1] != 0) { + continue; + } + char target = lookup[i-1]; + ts.push(new Traversal(i)); + while(!ts.empty()) { + Traversal t = ts.pop(); + seg[t.lastNode-1] = segNum; + for(int node: connections.get(t.lastNode)) { + if(lookup[node-1] == target && seg[node-1]==0) { + ts.push(new Traversal(node)); + } + } + } + } + //System.out.println("Node numbers : {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,...}"); + //System.out.println("Generated Seg Data: "+Arrays.toString(seg)); + //System.out.println("Lookup database : "+Arrays.toString(lookup)); + for(int i = 0; i < M; i ++) { + st = new StringTokenizer(f.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + char T = st.nextToken().charAt(0); + if(seg[a-1] != seg[b-1]) { + pw.print("1"); + }else { + if(lookup[a-1] == T && lookup[b-1] == T) { + pw.print("1"); + }else { + pw.print("0"); + } + } + } + pw.println(); + f.close(); + pw.close(); + } + +} +class Traversal { + int lastNode; + + public Traversal(int lastNode) { + this.lastNode = lastNode; + } +} +class Path{ + public int x,y,w; + public Path(int x,int y) { + this.x = x; + this.y = y; + //this.w = length; + } + @Override + public boolean equals(Object o) { + if(o instanceof Path) { + Path p = (Path) o; + return (this.x == p.x)&&(this.y == p.y); + }else { + return false; + } + } + @Override + public int hashCode() { + return Integer.hashCode(this.x) * Integer.hashCode(this.y+1); + } +} \ No newline at end of file diff --git a/milkvisits.out b/milkvisits.out new file mode 100644 index 0000000..7c5652b --- /dev/null +++ b/milkvisits.out @@ -0,0 +1 @@ +10110 diff --git a/moobuzz.in b/moobuzz.in new file mode 100644 index 0000000..da2d398 --- /dev/null +++ b/moobuzz.in @@ -0,0 +1 @@ +14 \ No newline at end of file diff --git a/moobuzz.java b/moobuzz.java new file mode 100644 index 0000000..3584279 --- /dev/null +++ b/moobuzz.java @@ -0,0 +1,33 @@ +// Extremely Simple MooBuzz solution +// Generates precalc lookup table +// Utilizes/"exploits" fact that the moobuzz sequence will repeat! +import java.io.*; +import java.util.*; +public class moobuzz { + public static void main(String[] args) throws IOException { + BufferedReader f = new BufferedReader(new FileReader("moobuzz.in")); + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("moobuzz.out"))); + int N = Integer.parseInt(f.readLine()) -1; + int[] baseNumberTable = new int[9]; + int x = 0; + for(int i = 0; i < 9; i ++) { + while(true) { + x++; + if(x % 3 == 0 || x % 5 == 0) { + continue; + }else { + baseNumberTable[i] = x; + break; + } + } + } + //System.out.println(Arrays.toString(baseNumberTable)); + int num = N % 8; + int base = baseNumberTable[num]; + int offset = 15 * Math.floorDiv(N, 8); + pw.println(base + offset); + f.close(); + pw.close(); + } + +} diff --git a/moobuzz.out b/moobuzz.out new file mode 100644 index 0000000..d600f47 --- /dev/null +++ b/moobuzz.out @@ -0,0 +1 @@ +26 diff --git a/multimoo.java b/multimoo.java new file mode 100644 index 0000000..f63cf8a --- /dev/null +++ b/multimoo.java @@ -0,0 +1,155 @@ +import java.io.*; +import java.util.*; +public class multimoo { + static Queue qx = new LinkedList<>(); + static Queue qy = new LinkedList<>(); + static Set visited = new HashSet<>(); + public static int[][] grid; + public static int N; + public static int floodFill(int tx,int ty, Segement s) { + visited.clear(); + int covered = 0; + //reset(); + int color = grid[tx][ty]; + qx.add(tx); + qy.add(ty); + while(!qx.isEmpty()) { + covered++; + int x = qx.poll(); + int y = qy.poll(); + s.pieces.add(new Position(x,y)); + //System.out.println(x+" "+y+" "+visited); + visited.add(new Position(x,y)); + if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] == color&& !visited.contains(new Position(x-1,y))) { + qx.add(x-1); + qy.add(y); + } + if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] == color&& !visited.contains(new Position(x+1,y))) { + qx.add(x+1); + qy.add(y); + } + if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] == color&& !visited.contains(new Position(x,y+1))) { + qx.add(x); + qy.add(y+1); + } + if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] == color && !visited.contains(new Position(x,y-1))) { + qx.add(x); + qy.add(y-1); + } + } + return covered; + } + + public static void main(String[] args) throws IOException{ + BufferedReader f = new BufferedReader(new FileReader("multimoo.in")); + N = Integer.parseInt(f.readLine()); + grid = new int[N][N]; + for(int i = 0; i < N; i ++) { + StringTokenizer st = new StringTokenizer(f.readLine()); + for(int j = 0 ; j < N; j ++) { + grid[i][j] = Integer.parseInt(st.nextToken()); + } + } + f.close(); + int maxID = 0; + int maxCount = 0; + List pieceList = new ArrayList<>(); + for(int i = 0; i < N; i ++) { + for(int j = 0; j < N; j ++) { + Segement s = new Segement(grid[i][j]); + if(!visited.contains(new Position(i,j))) { + int id = grid[i][j]; + int count = floodFill(i, j,s); + if(count > maxCount) { + maxCount = count; + maxID = id; + } + } + if(s.pieces.size() > 0) { + pieceList.add(s); + } + } + } + int LSize = 0; + for(Segement s:pieceList) { + System.out.println(s.pieces); + } + for(int i = 0; i < N; i ++) { + Segement p = pieceList.get(i); + for(Position pos:p.pieces) { + int x = pos.x; + int y = pos.y; + int color= grid[x][y]; + Set check = new HashSet(); + if(x-1 >= 0 && x-1 < N && y < N && y >= 0 && grid[x-1][y] != color&& !visited.contains(new Position(x-1,y))) { + check.add(new Position(x-1,y)); + } + if(x+1 >= 0 && x+1 < N && y < N && y >= 0 && grid[x+1][y] != color&& !visited.contains(new Position(x+1,y))) { + check.add(new Position(x+1,y)); + } + if(x >= 0 && x < N && y +1 < N && y+1 >= 0 && grid[x][y+1] != color&& !visited.contains(new Position(x,y+1))) { + check.add(new Position(x,y+1)); + } + if(x >= 0 && x < N && y-1 < N && y-1 >= 0 && grid[x][y-1] != color && !visited.contains(new Position(x,y-1))) { + check.add(new Position(x,y-1)); + } + for(int j = 0; j < pieceList.size(); j ++) { + if(i==j) { + continue; + } + Set checked = new HashSet<>(check); + checked.retainAll(pieceList.get(j).pieces); + if(checked.size() > 0) { + int size = pieceList.get(j).pieces.size() + pieceList.get(i).pieces.size(); + System.out.println("1: "+i+" "+j+" "+size); + System.out.println("2: "+pieceList.get(i).id+" "+pieceList.get(j).id+" "+size); + if(size>LSize) { + LSize = size; + } + } + checked.clear(); + checked = null; + } + } + } + PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("multimoo.out"))); + System.out.println("Max Count "+maxCount+" (obtained with id "+maxID+")"); + System.out.println("Max Count team up "+LSize); + pw.println(maxCount); + pw.println(LSize); + pw.close(); + } + +} +class Position{ + int x,y; + //boolean perimeter = false; + public Position(int x,int y){ + this.x = x; + this.y = y; + } + @Override + public boolean equals(Object s) { + if(s instanceof Position) { + Position p = (Position) s; + return ((p.y == this.y) && (p.x == this.x)); + } + return false; + } + @Override + public int hashCode() { + return ((Integer.hashCode(this.x) -7)*(Integer.hashCode(this.y)-5)); + } + public String toString() { + return "("+this.x+","+this.y+")"; + } +} +class Segement{ + public Set pieces; + public int id; + public Segement(int id) { + this.id = id; + this.pieces = new HashSet<>(); + } + +} \ No newline at end of file