git reimport
This commit is contained in:
32
dcj/src/Main.java
Normal file
32
dcj/src/Main.java
Normal file
@@ -0,0 +1,32 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().run();
|
||||
}
|
||||
|
||||
int myId = message.MyNodeId();
|
||||
int nodeCount = message.NumberOfNodes();
|
||||
long itemCount = 0;
|
||||
|
||||
long perNode = itemCount / (nodeCount - 1) + 1;
|
||||
long iL = Math.min((myId - 1) * perNode, itemCount);
|
||||
long iR = Math.min(myId * perNode, itemCount);
|
||||
|
||||
void run() {
|
||||
if (myId == 0) {
|
||||
server();
|
||||
} else {
|
||||
client();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void client() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void server() {
|
||||
|
||||
}
|
||||
}
|
165
dcj/src/SolutionFlagpoles.java
Normal file
165
dcj/src/SolutionFlagpoles.java
Normal file
@@ -0,0 +1,165 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SolutionFlagpoles {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SolutionFlagpoles().run();
|
||||
}
|
||||
|
||||
int myId = message.MyNodeId();
|
||||
int nodeCount = message.NumberOfNodes();
|
||||
long itemCount = flagpoles.GetNumFlagpoles();
|
||||
|
||||
long perNode = itemCount / (nodeCount - 1) + 1;
|
||||
long iL = Math.min((myId - 1) * perNode, itemCount);
|
||||
long iR = Math.min(myId * perNode, itemCount);
|
||||
|
||||
void run() {
|
||||
if (myId == 0) {
|
||||
server();
|
||||
} else {
|
||||
client();
|
||||
}
|
||||
}
|
||||
|
||||
static class Sequence {
|
||||
long d;
|
||||
long length;
|
||||
long startIndex;
|
||||
long startElement;
|
||||
long endElement;
|
||||
|
||||
public Sequence(long d, long length, long startIndex, long startElement, long endElement) {
|
||||
this.d = d;
|
||||
this.length = length;
|
||||
this.startIndex = startIndex;
|
||||
this.startElement = startElement;
|
||||
this.endElement = endElement;
|
||||
}
|
||||
|
||||
static Sequence zero(long startIndex) {
|
||||
return new Sequence(0, 0, startIndex, -1, -1);
|
||||
}
|
||||
|
||||
long endIndex() {
|
||||
return Math.max(startIndex, startIndex + length - 1);
|
||||
}
|
||||
|
||||
Sequence addElementRight(long element) {
|
||||
if (endElement == -1) {
|
||||
return new Sequence(0, 1, startIndex, element, element);
|
||||
}
|
||||
|
||||
if (length == 1) {
|
||||
return new Sequence(element - startElement, 2, startIndex, startElement, element);
|
||||
}
|
||||
|
||||
if (element - endElement == d) {
|
||||
return new Sequence(d, length + 1, startIndex, startElement, element);
|
||||
}
|
||||
|
||||
return new Sequence(element - endElement, 2, startIndex + length - 1, endElement, element);
|
||||
}
|
||||
|
||||
Sequence addSequenceRight(Sequence o) {
|
||||
if (endIndex() + 1 != o.startIndex) {
|
||||
return o;
|
||||
}
|
||||
if (o.length == 1) {
|
||||
return addElementRight(o.startElement);
|
||||
}
|
||||
if (d != o.d) {
|
||||
if (o.startElement - endElement == o.d) {
|
||||
return new Sequence(o.d, o.length + 1, o.startIndex - 1, endElement, o.endElement);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
if (o.startElement - endElement != d) {
|
||||
return o;
|
||||
}
|
||||
return new Sequence(d, length + o.length, startIndex, startElement, o.endElement);
|
||||
}
|
||||
|
||||
boolean isBetter(Sequence o) {
|
||||
return length > o.length;
|
||||
}
|
||||
|
||||
void send(int target) {
|
||||
message.PutLL(target, d);
|
||||
message.PutLL(target, length);
|
||||
message.PutLL(target, startIndex);
|
||||
message.PutLL(target, startElement);
|
||||
message.PutLL(target, endElement);
|
||||
message.Send(target);
|
||||
}
|
||||
|
||||
static Sequence receive(int source) {
|
||||
message.Receive(source);
|
||||
return new Sequence(message.GetLL(source), message.GetLL(source), message.GetLL(source), message.GetLL(source), message.GetLL(source));
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "{" + d + " " + length + " " + startIndex + " " + startElement + " " + endElement + "}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void client() {
|
||||
Map<Long, Long> map = new HashMap<>();
|
||||
for (long i = iL; i < iR; i++) {
|
||||
map.put(i, flagpoles.GetHeight(i));
|
||||
}
|
||||
|
||||
Sequence best = Sequence.zero(iL);
|
||||
Sequence current = Sequence.zero(iL);
|
||||
Sequence leftmost = Sequence.zero(iL);
|
||||
|
||||
for (long i = iL; i < iR; i++) {
|
||||
current = current.addElementRight(map.get(i));
|
||||
|
||||
if (current.startIndex == iL) {
|
||||
leftmost = current;
|
||||
}
|
||||
|
||||
if (current.isBetter(best)) {
|
||||
best = current;
|
||||
}
|
||||
}
|
||||
|
||||
leftmost.send(0);
|
||||
current.send(0);
|
||||
best.send(0);
|
||||
}
|
||||
|
||||
|
||||
void server() {
|
||||
Sequence best = Sequence.zero(0);
|
||||
Sequence current = Sequence.zero(0);
|
||||
|
||||
for (int clientId = 1; clientId < nodeCount; clientId++) {
|
||||
Sequence clientLeftmost = Sequence.receive(clientId);
|
||||
Sequence clientRightmost = Sequence.receive(clientId);
|
||||
Sequence clientBest = Sequence.receive(clientId);
|
||||
|
||||
// System.out.println(clientId + " -> " + clientLeftmost.toString());
|
||||
// System.out.println(clientId + " -> " + clientBest.toString());
|
||||
|
||||
if (clientBest.isBetter(best)) {
|
||||
best = clientBest;
|
||||
}
|
||||
|
||||
current = current.addSequenceRight(clientLeftmost);
|
||||
// System.out.println(current.toString());
|
||||
|
||||
if (current.isBetter(best)) {
|
||||
best = current;
|
||||
}
|
||||
|
||||
if (clientRightmost.endIndex() > current.endIndex()) {
|
||||
current = clientRightmost;
|
||||
}
|
||||
}
|
||||
System.out.println(best.length);
|
||||
}
|
||||
}
|
210
dcj/src/SolutionMemory.java
Normal file
210
dcj/src/SolutionMemory.java
Normal file
@@ -0,0 +1,210 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class SolutionMemory {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SolutionMemory().run();
|
||||
}
|
||||
|
||||
int myId = message.MyNodeId();
|
||||
int nodeCount = message.NumberOfNodes();
|
||||
long itemCount = broken_memory.GetLength();
|
||||
|
||||
void run() {
|
||||
client();
|
||||
if (myId == 0) {
|
||||
server();
|
||||
}
|
||||
}
|
||||
|
||||
int binSearchIters = 30;
|
||||
|
||||
Map<Long, Long> map;
|
||||
|
||||
long hash(long l, long r) {
|
||||
long res1 = 0;
|
||||
long res2 = 0;
|
||||
for (long i = l; i < r; i++) {
|
||||
res1 *= 31;
|
||||
res1 += map.get(i);
|
||||
res1 %= 1000000009L;
|
||||
|
||||
res2 *= 7;
|
||||
res2 += map.get(i);
|
||||
res2 %= 1000000007L;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
|
||||
void serveRequest(int source) {
|
||||
while (true) {
|
||||
message.Receive(source);
|
||||
long status = message.GetLL(source);
|
||||
|
||||
if (status == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
long l = message.GetLL(source);
|
||||
long r = message.GetLL(source);
|
||||
|
||||
long res = hash(l, r);
|
||||
message.PutLL(source, res);
|
||||
message.Send(source);
|
||||
}
|
||||
}
|
||||
|
||||
boolean sendRequestIsMatch(int target, long l, long r) {
|
||||
message.PutLL(target, 1);
|
||||
message.PutLL(target, l);
|
||||
message.PutLL(target, r);
|
||||
message.Send(target);
|
||||
|
||||
long res = hash(l, r);
|
||||
|
||||
message.Receive(target);
|
||||
long otherHash = message.GetLL(target);
|
||||
|
||||
// System.out.println(myId + " " + target + " (" + l + " " + r + ") = " + (res == otherHash));
|
||||
|
||||
return res == otherHash;
|
||||
}
|
||||
|
||||
Set<Long> decide(int other) {
|
||||
Set<Long> candidates = new HashSet<>();
|
||||
{
|
||||
long leftL = 0;
|
||||
long leftR = itemCount;
|
||||
|
||||
while (leftR - leftL > 1) {
|
||||
long m1 = (long) (leftL + (leftR - leftL) * 0.3333);
|
||||
long m2 = (long) (leftL + (leftR - leftL) * 0.6666);
|
||||
|
||||
if (m1 - leftL > 0) {
|
||||
if (!sendRequestIsMatch(other, leftL, m1)) {
|
||||
leftL = leftL;
|
||||
leftR = m1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (m2 - m1 > 0) {
|
||||
if (!sendRequestIsMatch(other, m1, m2)) {
|
||||
leftL = m1;
|
||||
leftR = m2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
leftL = m2;
|
||||
leftR = leftR;
|
||||
}
|
||||
candidates.add(leftL);
|
||||
}
|
||||
|
||||
{
|
||||
long rightL = 0;
|
||||
long rightR = itemCount;
|
||||
|
||||
while (rightR - rightL > 1) {
|
||||
long m1 = (long) (rightL + (rightR - rightL) * 0.3333);
|
||||
long m2 = (long) (rightL + (rightR - rightL) * 0.6666);
|
||||
|
||||
if (rightR - m2 > 0) {
|
||||
if (!sendRequestIsMatch(other, m2, rightR)) {
|
||||
rightL = m2;
|
||||
rightR = rightR;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (m2 - m1 > 0) {
|
||||
if (!sendRequestIsMatch(other, m1, m2)) {
|
||||
rightL = m1;
|
||||
rightR = m2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
rightL = rightL;
|
||||
rightR = m1;
|
||||
}
|
||||
candidates.add(rightL);
|
||||
}
|
||||
|
||||
// System.out.println(leftL + " " + leftR + " | " + rightL + " " + rightR);
|
||||
|
||||
|
||||
message.PutLL(other, 0);
|
||||
message.Send(other);
|
||||
|
||||
// candidates.add(leftL);
|
||||
// candidates.add(rightL);
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
Set<Long> discuss(int other) {
|
||||
|
||||
boolean junior = myId % 2 == 0;
|
||||
|
||||
Set<Long> result;
|
||||
|
||||
if (junior) {
|
||||
serveRequest(other);
|
||||
result = decide(other);
|
||||
} else {
|
||||
result = decide(other);
|
||||
serveRequest(other);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void client() {
|
||||
map = new HashMap<>();
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
map.put((long) i, broken_memory.GetValue(i));
|
||||
}
|
||||
|
||||
int otherClient1;
|
||||
int otherClient2;
|
||||
if (myId % 2 == 0) {
|
||||
otherClient1 = (myId + 1 + nodeCount) % nodeCount;
|
||||
otherClient2 = (myId - 1 + nodeCount) % nodeCount;
|
||||
} else {
|
||||
otherClient1 = (myId - 1 + nodeCount) % nodeCount;
|
||||
otherClient2 = (myId + 1 + nodeCount) % nodeCount;
|
||||
}
|
||||
|
||||
Set<Long> result1 = discuss(otherClient1);
|
||||
Set<Long> result2 = discuss(otherClient2);
|
||||
|
||||
result1.retainAll(result2);
|
||||
long x = result1.iterator().next();
|
||||
// System.out.println(x);
|
||||
|
||||
message.PutLL(0, x);
|
||||
message.Send(0);
|
||||
}
|
||||
|
||||
|
||||
void server() {
|
||||
List<Long> list = new ArrayList<>();
|
||||
for (int i = 0; i < nodeCount; i++) {
|
||||
message.Receive(i);
|
||||
list.add(message.GetLL(i));
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (long i : list) {
|
||||
sb.append(i);
|
||||
sb.append(' ');
|
||||
}
|
||||
System.out.println(sb);
|
||||
}
|
||||
}
|
32
dcj/src/Template.java
Normal file
32
dcj/src/Template.java
Normal file
@@ -0,0 +1,32 @@
|
||||
public class Template {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Template().run();
|
||||
}
|
||||
|
||||
int myId = message.MyNodeId();
|
||||
int nodeCount = message.NumberOfNodes();
|
||||
long itemCount = 0;
|
||||
|
||||
long perNode = itemCount / (nodeCount - 1) + 1;
|
||||
long iL = Math.min((myId - 1) * perNode, itemCount);
|
||||
long iR = Math.min(myId * perNode, itemCount);
|
||||
|
||||
void run() {
|
||||
if (myId == 0) {
|
||||
server();
|
||||
} else {
|
||||
client();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void client() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void server() {
|
||||
|
||||
}
|
||||
}
|
17
dcj/src/broken_memory.java
Normal file
17
dcj/src/broken_memory.java
Normal file
@@ -0,0 +1,17 @@
|
||||
// Sample input 2, in Java.
|
||||
public class broken_memory {
|
||||
public broken_memory() {
|
||||
}
|
||||
|
||||
public static long GetLength() {
|
||||
return 30L;
|
||||
}
|
||||
|
||||
public static long GetValue(long i) {
|
||||
if (i < 0L || i >= GetLength())
|
||||
throw new IllegalArgumentException("Invalid argument");
|
||||
if ((29L - message.MyNodeId()) == i)
|
||||
return (((i % 9L) + 1L) * ((i % 7L) + 1L) ^ (i + message.MyNodeId() + 1)) + 1L;
|
||||
return (((i % 9L) + 1L) * ((i % 7L) + 1L)) + 1L;
|
||||
}
|
||||
}
|
92
dcj/src/message.java
Normal file
92
dcj/src/message.java
Normal file
@@ -0,0 +1,92 @@
|
||||
// A program you submit to Distributed Code Jam will be compiled by Google, and
|
||||
// will run on multiple computers (nodes). This library describes the interface
|
||||
// needed for the nodes to identify themselves and to communicate.
|
||||
//
|
||||
// This is the version of the interface for programs written in Java. Your
|
||||
// program doesn't need to import it and should always use a class name before
|
||||
// accessing the static methods, e.g.:
|
||||
// int n = message.NumberOfNodes();
|
||||
public class message {
|
||||
|
||||
// The number of nodes on which the solution is running.
|
||||
public static int NumberOfNodes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The index (in the range [0 .. NumberOfNodes()-1]) of the node on which this
|
||||
// process is running.
|
||||
public static int MyNodeId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// In all the functions below, if "target" or "source" is not in the valid
|
||||
// range, the behaviour is undefined.
|
||||
|
||||
// The library internally has a message buffer for each of the nodes in
|
||||
// [0 .. NumberOfNodes()-1]. It accumulates the message in such a buffer through
|
||||
// the "Put" methods.
|
||||
|
||||
// Append "value" to the message that is being prepared for the node with id
|
||||
// "target". The "Int" in PutInt is interpreted as 32 bits, regardless of
|
||||
// whether the actual int type will be 32 or 64 bits.
|
||||
public static void PutChar(int target, char value) {
|
||||
|
||||
}
|
||||
|
||||
public static void PutInt(int target, int value) {
|
||||
|
||||
}
|
||||
|
||||
public static void PutLL(int target, long value) {
|
||||
|
||||
}
|
||||
|
||||
// Send the message that was accumulated in the appropriate buffer to the
|
||||
// "target" instance, and clear the buffer for this instance.
|
||||
//
|
||||
// This method is non-blocking - that is, it does not wait for the receiver to
|
||||
// call "Receive", it returns immediately after sending the message.
|
||||
public static void Send(int target) {
|
||||
|
||||
}
|
||||
|
||||
// The library also has a receiving buffer for each instance. When you call
|
||||
// "Receive" and retrieve a message from an instance, the buffer tied to this
|
||||
// instance is overwritten. You can then retrieve individual parts of the
|
||||
// message through the Get* methods. You must retrieve the contents of the
|
||||
// message in the order in which they were appended.
|
||||
//
|
||||
// This method is blocking - if there is no message to receive, it will wait for
|
||||
// the message to arrive.
|
||||
//
|
||||
// You can call Receive(-1) to retrieve a message from any source, or with
|
||||
// source in [0 .. NumberOfNodes()-1] to retrieve a message from a particular
|
||||
// source.
|
||||
//
|
||||
// It returns the number of the instance which sent the message (which is equal
|
||||
// to source, unless source is -1).
|
||||
public static int Receive(int source) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Each of these methods returns and consumes one item from the buffer of the
|
||||
// appropriate instance. You must call these methods in the order in which the
|
||||
// elements were appended to the message (so, for instance, if the message was
|
||||
// created with PutChar, PutChar, PutLL, you must call GetChar, GetChar, GetLL
|
||||
// in this order).
|
||||
// If you call them in different order, or you call a Get* method after
|
||||
// consuming all the contents of the buffer, behaviour is undefined.
|
||||
// The "Int" in GetInt is interpreted as 32 bits, regardless of whether the
|
||||
// actual int type will be 32 or 64 bits.
|
||||
public static char GetChar(int source) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static int GetInt(int source) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static long GetLL(int source) {
|
||||
return 0;
|
||||
}
|
||||
}
|
36
dcj/src/number_bases.java
Normal file
36
dcj/src/number_bases.java
Normal file
@@ -0,0 +1,36 @@
|
||||
// Sample input 1, in Java.
|
||||
public class number_bases {
|
||||
public number_bases() {
|
||||
}
|
||||
|
||||
public static long GetLength() {
|
||||
return 3L;
|
||||
}
|
||||
|
||||
public static long GetDigitX(long i) {
|
||||
switch ((int)i) {
|
||||
case 0: return 3L;
|
||||
case 1: return 2L;
|
||||
case 2: return 1L;
|
||||
default: throw new IllegalArgumentException("Invalid argument");
|
||||
}
|
||||
}
|
||||
|
||||
public static long GetDigitY(long i) {
|
||||
switch ((int)i) {
|
||||
case 0: return 6L;
|
||||
case 1: return 5L;
|
||||
case 2: return 4L;
|
||||
default: throw new IllegalArgumentException("Invalid argument");
|
||||
}
|
||||
}
|
||||
|
||||
public static long GetDigitZ(long i) {
|
||||
switch ((int)i) {
|
||||
case 0: return 0L;
|
||||
case 1: return 8L;
|
||||
case 2: return 5L;
|
||||
default: throw new IllegalArgumentException("Invalid argument");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user