git reimport
This commit is contained in:
87
dcj/other/pancakes_solution.java
Normal file
87
dcj/other/pancakes_solution.java
Normal file
@@ -0,0 +1,87 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().run();
|
||||
}
|
||||
|
||||
int myId = message.MyNodeId();
|
||||
int nodeCount = message.NumberOfNodes();
|
||||
long stackSize = pancakes.GetStackSize();
|
||||
long diners = pancakes.GetNumDiners();
|
||||
long perNode = stackSize / nodeCount + 1;
|
||||
|
||||
long iL = myId * perNode;
|
||||
long iR = Math.min((myId + 1) * perNode, stackSize);
|
||||
|
||||
void run() {
|
||||
client();
|
||||
|
||||
if (myId == 0) {
|
||||
server();
|
||||
}
|
||||
}
|
||||
|
||||
long count(List<Long> xs) {
|
||||
long res = 0;
|
||||
long prev = -1;
|
||||
for (Long x : xs) {
|
||||
if (x < prev) {
|
||||
res++;
|
||||
}
|
||||
prev = x;
|
||||
}
|
||||
return res + 1;
|
||||
}
|
||||
|
||||
void client() {
|
||||
List<Long> xs = new ArrayList<>();
|
||||
for (long i = iL; i < iR; i++) {
|
||||
long x = pancakes.GetStackItem(i);
|
||||
xs.add(x);
|
||||
}
|
||||
|
||||
if (xs.isEmpty()) {
|
||||
message.PutLL(0, -1);
|
||||
message.Send(0);
|
||||
return;
|
||||
}
|
||||
|
||||
long res = count(xs);
|
||||
|
||||
message.PutLL(0, xs.get(0));
|
||||
message.PutLL(0, xs.get(xs.size() - 1));
|
||||
message.PutLL(0, res);
|
||||
message.Send(0);
|
||||
}
|
||||
|
||||
void server() {
|
||||
long res = 0;
|
||||
long prevLast = Long.MAX_VALUE;
|
||||
|
||||
for (int nodeId = 0; nodeId < nodeCount; nodeId++) {
|
||||
message.Receive(nodeId);
|
||||
long first = message.GetLL(nodeId);
|
||||
if (first == -1) {
|
||||
continue;
|
||||
}
|
||||
long last = message.GetLL(nodeId);
|
||||
long iRes = message.GetLL(nodeId);
|
||||
|
||||
// System.out.println("R " + nodeId + ": "+ first + " " + last + " " + iRes);
|
||||
|
||||
res += iRes;
|
||||
|
||||
if (prevLast <= first) {
|
||||
res--;
|
||||
}
|
||||
prevLast = last;
|
||||
|
||||
// System.out.println(res);
|
||||
}
|
||||
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user