-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
import java.util.Set; | ||
|
||
public class Parity { | ||
|
||
|
||
public static class DSU { | ||
|
||
Map<Integer, Integer> root = new HashMap<Integer, Integer>(); | ||
Map<Integer, Integer> size = new HashMap<Integer, Integer>(); | ||
|
||
private int getRoot(int i) { | ||
while (root.get(i) != i) { | ||
i = root.get(i); | ||
} | ||
return i; | ||
} | ||
|
||
public int union(int i, int j) { | ||
|
||
if (root.get(i) == null) { | ||
root.put(i, i); | ||
size.put(i, 1); | ||
} | ||
if (root.get(j) == null) { | ||
root.put(j, j); | ||
size.put(j, 1); | ||
} | ||
|
||
int root_i = getRoot(i); | ||
int root_j = getRoot(j); | ||
int parent = 0; | ||
/* same set then just return the parent */ | ||
if (root_i == root_j) { | ||
parent = root_i; | ||
} | ||
/* Else different sets */ | ||
else if (size.get(root_i) > size.get(root_j)) { | ||
parent = root_i; | ||
root.put(root_j, parent); | ||
} else { | ||
parent = root_j; | ||
root.put(root_i, parent); | ||
} | ||
|
||
return parent; | ||
} | ||
|
||
public boolean find(int i, int j) { | ||
if (root.get(i) != null && root.get(j) != null) { | ||
return getRoot(i) == getRoot(j); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public Set<Integer> keys() { | ||
return root.keySet(); | ||
} | ||
|
||
public void print() { | ||
for (Integer key : root.keySet()) { | ||
System.out.println(key + " = " + root.get(key)); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
|
||
Scanner sc = new Scanner(System.in); | ||
|
||
|
||
while (true) { | ||
|
||
int N = sc.nextInt(); | ||
if (N == -1) { | ||
System.exit(0); | ||
} | ||
|
||
DSU dsu = new DSU(); | ||
|
||
|
||
int M = sc.nextInt(); | ||
int result = M; | ||
for (int i = 0; i < M; i++) { | ||
int k = sc.nextInt(); | ||
int m = sc.nextInt() + 1; | ||
if ((k > N || m > N + 1) && result == M) { | ||
result = i; | ||
} | ||
String s = sc.next(); | ||
/* odd */ | ||
if (s.charAt(0) == 'o') { | ||
dsu.union(k, -m); | ||
dsu.union(-k, m); | ||
} else { | ||
dsu.union(k, m); | ||
dsu.union(-k, -m); | ||
} | ||
|
||
if ((dsu.find(m, -m) || dsu.find(k, -k)) && result == M) { | ||
result = i; | ||
} | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.util.Scanner; | ||
|
||
public class DiscreteFunction { | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
|
||
long k = 0; | ||
int p1 = 0; | ||
int p2 = 0; | ||
int N = sc.nextInt(); | ||
long prev = sc.nextLong(); | ||
for (int i = 2; i <= N; i++) { | ||
long current = sc.nextLong(); | ||
if (Math.abs(current - prev) > k) { | ||
k = Math.abs(current - prev); | ||
p1 = i - 1; | ||
p2 = i; | ||
} | ||
prev = current; | ||
} | ||
|
||
System.out.println(p1 + " " + p2); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import java.util.Scanner; | ||
|
||
|
||
|
||
/* Pure Brute Force solutions ~ O(n^6) */ | ||
public class Main { | ||
|
||
public static int Q[]; | ||
public static int N; | ||
|
||
/* Will queen q beat at least one queen? */ | ||
public static boolean isSafe(int q, int ignore1, int ignore2) { | ||
|
||
boolean safe = true; | ||
|
||
for (int i = 0; i < N; i++) { | ||
|
||
/* skip queen q */ | ||
if (i == q || i == ignore1 || i == ignore2) { | ||
continue; | ||
} | ||
|
||
/* same column */ | ||
if ((Q[q] == Q[i]) || (Math.abs(q - i) == Math.abs(Q[q] - Q[i]))) { | ||
safe = false; | ||
break; | ||
} | ||
} | ||
return safe; | ||
} | ||
|
||
public static int QPerm(int q1, int q2, int q3) { | ||
|
||
int qperm = 0; | ||
/* Save the old values so we can restore them later */ | ||
int q1old = Q[q1]; | ||
int q2old = Q[q2]; | ||
int q3old = Q[q3]; | ||
|
||
for (int i = 0; i < N; i ++) | ||
{ | ||
/* skip the positions the first queen already is, | ||
* check another position | ||
* if it won't result in a safe position, no reason to check another queens q2 and q3 | ||
* ignore q2 and q3 since we can permute them later which can result in an unsafe pos for q1 */ | ||
Q[q1] = i; | ||
if (q1old == i || !isSafe(q1, q2, q3)) { | ||
continue; | ||
} | ||
|
||
for (int j = 0; j < N; j++) | ||
{ | ||
/* permute second queen until safe position */ | ||
Q[q2] = j; | ||
/* ignore q3 and q3, i.e the same queen , redudant but we should use both parameters */ | ||
if (q2old == j || !isSafe(q2, q3, q3)) { | ||
continue; | ||
} | ||
|
||
/* permute third queen until safe position */ | ||
for (int k = 0; k < N; k++) { | ||
|
||
Q[q3] = k; | ||
if (q3old == k) { | ||
continue; | ||
} | ||
/* check for every queen if the position is safe */ | ||
|
||
if (isSafe(q3, q3, q3)) { | ||
qperm++; | ||
} | ||
} | ||
} | ||
} | ||
Q[q1] = q1old; | ||
Q[q2] = q2old; | ||
Q[q3] = q3old; | ||
|
||
return qperm; | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
Scanner sc = new Scanner(System.in); | ||
|
||
N = sc.nextInt(); | ||
|
||
/* Q[i] = j => column of the queen from the row i */ | ||
Q = new int[N]; | ||
|
||
for (int i = 0; i < N; i++) { | ||
int x = sc.nextInt(); | ||
int y = sc.nextInt(); | ||
Q[x - 1] = y - 1; /* subtract 1 to normalise form 1..N to 0..N-1 */ | ||
} | ||
|
||
int output = 0; | ||
for (int q1 = 0; q1 < N - 2; q1++) | ||
{ | ||
for (int q2 = q1 + 1; q2 < N - 1; q2++) | ||
{ | ||
for (int q3 = q2 + 1; q3 < N; q3++) | ||
{ | ||
/* choose 3 queens q1, q2, q3 */ | ||
|
||
output = output + QPerm(q1, q2, q3); | ||
} | ||
} | ||
} | ||
|
||
System.out.println(output); | ||
sc.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import java.io.FileInputStream; | ||
import java.io.IOException;import java.io.InputStream; | ||
import java.util.Scanner; | ||
import java.util.TreeMap; | ||
|
||
import javax.xml.stream.events.NotationDeclaration; | ||
|
||
public class MemoryManagement { | ||
|
||
|
||
public static class TreeNode implements Comparable<TreeNode> { | ||
|
||
public long timestamp; | ||
public int id; | ||
|
||
public TreeNode(int id, long timestamp){ | ||
this.timestamp = timestamp; | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public int compareTo(TreeNode obj) { | ||
|
||
if (this.timestamp < obj.timestamp) { | ||
return -1; | ||
} else if (this.timestamp == obj.timestamp) { | ||
if (this.id < obj.id) { | ||
return -1; | ||
} else if (this.id == obj.id) { | ||
return 0; | ||
} else { | ||
return 1; | ||
} | ||
} else { | ||
} | ||
return 1; | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) throws IOException { | ||
|
||
TreeMap<TreeNode, Integer> blocks = new TreeMap<TreeNode, Integer>(); | ||
|
||
/* references to the blocks nodes in the tree */ | ||
TreeNode nodes[] = new TreeNode[30001]; | ||
|
||
Scanner sc = new Scanner(System.in); | ||
int N = 30000; | ||
int T = 10*60; /* 10 min */ | ||
int minFreeBlock = 1; | ||
int oldtime = 0; | ||
while (sc.hasNext()) { | ||
|
||
int time = 0; | ||
char c = 0; | ||
try { | ||
time = sc.nextInt(); | ||
c = (char) sc.next().charAt(0); | ||
} catch (Exception e) { | ||
System.exit(0); | ||
} | ||
|
||
/* Remove expired blocks */ | ||
if (time != oldtime) { | ||
|
||
TreeNode n = null; | ||
while (!blocks.isEmpty()) { | ||
n = blocks.firstKey(); | ||
/* expired */ | ||
if (time - n.timestamp >= T ) { | ||
blocks.remove(n); | ||
nodes[n.id] = null; | ||
if (n.id < minFreeBlock) { | ||
minFreeBlock = n.id; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
oldtime = time; | ||
} | ||
|
||
if (c == '+') { | ||
System.out.println(minFreeBlock); | ||
TreeNode n = new TreeNode(minFreeBlock, time); | ||
blocks.put(n, minFreeBlock); | ||
nodes[n.id] = n; | ||
for (int i = minFreeBlock + 1; i <= 30000; i++) { | ||
if (nodes[i] == null) { | ||
minFreeBlock = i; | ||
break; | ||
} | ||
} | ||
} else { | ||
/* request for block access */ | ||
int blockNo = sc.nextInt(); | ||
//System.out.println(blockNo); | ||
if (blockNo > N || nodes[blockNo] == null) { | ||
System.out.println("-"); | ||
} | ||
else { | ||
/* update block time */ | ||
blocks.remove(nodes[blockNo]); | ||
nodes[blockNo].timestamp = time; | ||
blocks.put(nodes[blockNo], blockNo); | ||
System.out.println("+"); | ||
} | ||
} | ||
|
||
|
||
} | ||
sc.close(); | ||
|
||
} | ||
|
||
} |