exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
listlengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED
|
73cef69273919c8f61b674366b36461b
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Solution {
// VM options: -Xmx1024m -Xss1024m
private void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int k = nextInt();
long[] x = nextLongArray(n);
ArrayList<Ladder>[] ladders = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
ladders[i] = new ArrayList<>();
}
for (int i = 0; i < k; i++) {
int fx = nextInt();
int fy = nextInt();
int tx = nextInt();
int ty = nextInt();
int h = nextInt();
ladders[fx].add(new Ladder(fx, fy, tx, ty, h));
}
ladders[n].add(new Ladder(n, m, n + 1, m, 0));
for (int i = 1; i <= n; i++) {
Collections.sort(ladders[i], new Comparator<Ladder>() {
@Override
public int compare(Ladder o1, Ladder o2) {
return o1.fy - o2.fy;
}
});
}
HashMap<Integer, Long>[] maxHealths = new HashMap[n + 2];
for (int i = 1; i <= n + 1; i++) {
maxHealths[i] = new HashMap<>();
}
maxHealths[1].put(1, 0L);
for (int i = 1; i <= n; i++) {
ArrayList<Integer> currentMaxHealths = new ArrayList<>();
currentMaxHealths.addAll(maxHealths[i].keySet());
currentMaxHealths.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
int lastPos = -1;
long maxHealth = -1;
for (Ladder ladder : ladders[i]) {
while (lastPos + 1 < currentMaxHealths.size() && currentMaxHealths.get(lastPos + 1) <= ladder.fy) {
lastPos++;
if (lastPos == 0) {
maxHealth = maxHealths[i].get(currentMaxHealths.get(lastPos));
} else {
maxHealth = Math.max(
maxHealth - x[i - 1] * (currentMaxHealths.get(lastPos) - currentMaxHealths.get(lastPos - 1)),
maxHealths[i].get(currentMaxHealths.get(lastPos))
);
}
}
if (lastPos != -1) {
long health = maxHealth - x[i - 1] * Math.abs(ladder.fy - currentMaxHealths.get(lastPos)) + ladder.h;
Long oldHealth = maxHealths[ladder.tx].get(ladder.ty);
if (oldHealth == null) {
oldHealth = health;
} else {
oldHealth = Math.max(oldHealth, health);
}
maxHealths[ladder.tx].put(ladder.ty, oldHealth);
}
}
lastPos = currentMaxHealths.size();
maxHealth = -1;
for (int li = ladders[i].size() - 1; li >= 0; li--) {
Ladder ladder = ladders[i].get(li);
while (lastPos > 0 && currentMaxHealths.get(lastPos - 1) >= ladder.fy) {
lastPos--;
if (lastPos == currentMaxHealths.size() - 1) {
maxHealth = maxHealths[i].get(currentMaxHealths.get(lastPos));
} else {
maxHealth = Math.max(
maxHealth - x[i - 1] * (currentMaxHealths.get(lastPos + 1) - currentMaxHealths.get(lastPos)),
maxHealths[i].get(currentMaxHealths.get(lastPos))
);
}
}
if (lastPos != currentMaxHealths.size()) {
long health = maxHealth - x[i - 1] * Math.abs(ladder.fy - currentMaxHealths.get(lastPos)) + ladder.h;
Long oldHealth = maxHealths[ladder.tx].get(ladder.ty);
if (oldHealth == null) {
oldHealth = health;
} else {
oldHealth = Math.max(oldHealth, health);
}
maxHealths[ladder.tx].put(ladder.ty, oldHealth);
}
}
}
if (maxHealths[n + 1].containsKey(m)) {
out.println(-maxHealths[n + 1].get(m));
} else {
out.println("NO ESCAPE");
}
}
private static class Ladder {
int fx;
int fy;
int tx;
int ty;
int h;
public Ladder(int fx, int fy, int tx, int ty, int h) {
this.fx = fx;
this.fy = fy;
this.tx = tx;
this.ty = ty;
this.h = h;
}
}
private static final String inputFilename = "input.txt";
private static final String outputFilename = "output.txt";
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
private final boolean isDebug;
private Solution(boolean isDebug) {
this.isDebug = isDebug;
}
public static void main(String[] args) throws IOException {
new Solution(Arrays.asList(args).contains("DEBUG_MODE")).run(args);
}
private void run(String[] args) throws IOException {
if (isDebug) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilename)));
// in = new BufferedReader(new InputStreamReader(System.in));
} else {
in = new BufferedReader(new InputStreamReader(System.in));
}
out = new PrintWriter(System.out);
// out = new PrintWriter(outputFilename);
// int t = isDebug ? nextInt() : 1;
// int t = 1;
int t = nextInt();
for (int i = 0; i < t; i++) {
// out.print("Case #" + (i + 1) + ": ");
solve();
out.flush();
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if (!p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if (!p) {
throw new RuntimeException(message);
}
}
private static void assertNotEqual(int unexpected, int actual) {
if (actual == unexpected) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static void assertEqual(int expected, int actual) {
if (expected != actual) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 8
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
d73fbac1f03d19d3daaf15aa1c274e70
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class Round766E {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
Round766E sol = new Round766E();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n) or matrix(n, 2) for graph edges, pairs, ...
int n, m, k;
long[] x;
static class Ladder{
int floorFrom, roomFrom, floorTo, roomTo, hp;
public Ladder(int floorFrom, int roomFrom, int floorTo, int roomTo, int hp) {
this.floorFrom = floorFrom;
this.roomFrom = roomFrom;
this.floorTo = floorTo;
this.roomTo = roomTo;
this.hp = hp;
}
@Override
public String toString() {
return String.format("(%d,%d)->(%d,%d):%d", floorFrom, roomFrom, floorTo, roomTo, hp);
}
}
ArrayList<Ladder>[] ladders;
void getInput() {
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
x = in.nextLongArray(n);
ladders = new ArrayList[n];
for(int i=0; i<n; i++)
ladders[i] = new ArrayList<Ladder>();
for(int i=0; i<k; i++) {
int floorFrom = in.nextInt()-1;
ladders[floorFrom].add(new Ladder(floorFrom,
in.nextInt()-1, in.nextInt()-1, in.nextInt()-1, in.nextInt()));
}
}
void printOutput() {
if(ans == Long.MAX_VALUE) {
out.println("NO ESCAPE");
}
else {
out.printlnAns(ans);
}
}
long ans;
void solve(){
TreeMap<Integer, Long>[] dist = new TreeMap[n];
for(int i=0; i<n; i++)
dist[i] = new TreeMap<Integer, Long>();
dist[0].put(0, 0L);
dist[n-1].put(m-1, Long.MAX_VALUE);
for(int i=0; i<n; i++) {
if(dist[i].isEmpty())
continue;
// dijkstra
for(var ladder: ladders[i]) {
dist[i].merge(ladder.roomFrom, Long.MAX_VALUE, (x, y) -> Math.min(x, y));
}
int size = dist[i].size();
PriorityQueue<Pair> pq = new PriorityQueue<>();
int[] indexToRoom = new int[size];
long[] indexToDist = new long[size];
int index = 0;
for(var entry: dist[i].entrySet()) {
int room = entry.getKey();
long distance = entry.getValue();
pq.add(new Pair(distance, index));
indexToDist[index] = distance;
indexToRoom[index] = room;
index++;
}
boolean[] visited = new boolean[size];
while(!pq.isEmpty()) {
Pair v = pq.poll();
long vDist = v.first;
int vIndex = v.second;
if(visited[vIndex])
continue;
visited[vIndex] = true;
if(vIndex > 0) {
long distance = x[i] * (indexToRoom[vIndex] - indexToRoom[vIndex-1]) + vDist;
if(indexToDist[vIndex-1] > distance) {
indexToDist[vIndex-1] = distance;
pq.add(new Pair(distance, vIndex-1));
}
}
if(vIndex < size-1) {
long distance = x[i] * (indexToRoom[vIndex+1] - indexToRoom[vIndex]) + vDist;
if(indexToDist[vIndex+1] > distance) {
indexToDist[vIndex+1] = distance;
pq.add(new Pair(distance, vIndex+1));
}
}
}
for(int j=0; j<size; j++)
dist[i].put(indexToRoom[j], indexToDist[j]);
for(var ladder: ladders[i]) {
dist[ladder.floorTo].merge(ladder.roomTo, dist[i].get(ladder.roomFrom) - ladder.hp, (x, y) -> Math.min(x, y));
}
}
ans = dist[n-1].get(m-1);
}
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
long first;
int second;
public Pair(long first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = second;
x <<= 32;
x += first;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Long.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 17
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
ed2853b149e991bb2c411e260d4f4008
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class E1637 {
public static void main(String[] args) throws IOException, FileNotFoundException {
// Scanner in = new Scanner(new File("test.in"));
Kattio in = new Kattio();
int T = in.nextInt();
while(T > 0){
T--;
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
long[] x = new long[N + 1];
for(int i = 1; i <= N; i++){
x[i] = in.nextInt();
}
// figure out the minimum distance to each state
Ladder[] ladders = new Ladder[K];
HashMap<Integer, ArrayList<Integer>> ends = new HashMap<>();
HashMap<Integer, ArrayList<Integer>> starts = new HashMap<>();
for(int i = 1; i <= N; i++){
ends.put(i, new ArrayList<>());
starts.put(i, new ArrayList<>());
}
for(int i = 0; i < K; i++){
ladders[i] = new Ladder(new Point(in.nextInt(), in.nextInt(), 2*i), new Point(in.nextInt(), in.nextInt(), 2*i + 1), in.nextInt());
ends.get(ladders[i].end.floor).add(i);
starts.get(ladders[i].start.floor).add(i);
}
long[] dist = new long[2*K]; // number of health points required to get to a position? 2*K = start, 2*K + 1 = end'
Arrays.fill(dist, Long.MAX_VALUE / 2);
boolean[] visited = new boolean[2*K];
// figure out what do to for the first ones
for(Integer st: starts.get(1)){
visited[ladders[st].start.index] = true;
dist[ladders[st].start.index] = (ladders[st].start.room - 1)*x[1];
}
for(int floor = 1; floor <= N; floor++){
// add all the ones that have ended here
ArrayList<Point> points = new ArrayList<>();
for(Integer st: starts.get(floor)){
points.add(ladders[st].start);
}
for(Integer en: ends.get(floor)){
points.add(ladders[en].end);
}
Collections.sort(points);
for(int i = 0; i < points.size() - 1; i++){
int leftId = points.get(i).index;
int rightId = points.get(i + 1).index;
long dx = (points.get(i + 1).room - points.get(i).room) * x[floor];
if(visited[leftId]){
dist[rightId] = Math.min(dist[rightId], dist[leftId] + dx);
visited[rightId] = true;
}
}
for(int i = points.size() - 2; i >= 0; i--){
int leftId = points.get(i).index;
int rightId = points.get(i + 1).index;
long dx = (points.get(i + 1).room - points.get(i).room) * x[floor];
if(visited[rightId]){
dist[leftId] = Math.min(dist[leftId], dist[rightId] + dx);
visited[leftId] = true;
}
}
// go to the next one
for(Integer st: starts.get(floor)){
Point stpt = ladders[st].start;
if(visited[stpt.index]){
dist[ladders[st].end.index] = dist[stpt.index] - ladders[st].healthPoints;
visited[ladders[st].end.index] = true;
}
}
}
// figure out ending situation
long ans = Long.MAX_VALUE ;
for(Integer en: ends.get(N)){
if(visited[ladders[en].end.index]){
ans = Math.min(ans, dist[ladders[en].end.index] + (M - ladders[en].end.room)*x[N]);
// System.out.println("finding best in answer " + dist[ladders[en].end.index] + " " + ladders[en].end.index + " " + (M - ladders[en].end.room) + " " + x[N] );
}
}
// for(int i = 0; i < K; i++){
// System.out.println(ladders[i].start.floor + " " + ladders[i].start.room + " " + dist[ladders[i].start.index] + " " + visited[ladders[i].start.index]);
// System.out.println(ladders[i].end.floor + " " + ladders[i].end.room + " " + dist[ladders[i].end.index] + " " + visited[ladders[i].end.index]);
// }
if(ans == Long.MAX_VALUE){
System.out.println("NO ESCAPE");
} else {
System.out.println(ans);
}
}
}
public static class State implements Comparable<State> {
long dist;
Point pt;
public State(Point pt, long dist){
this.pt = pt;
this.dist = dist;
}
public int compareTo(State s){
return Long.compare(dist, s.dist);
}
}
public static class Ladder {
Point start, end;
int healthPoints;
public Ladder(Point start, Point end, int healthPoints){
this.start = start;
this.end = end;
this.healthPoints = healthPoints;
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in,System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName+".out");
r = new BufferedReader(new FileReader(problemName+".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {}
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
public static class Point implements Comparable<Point> {
int floor, room, index;
public Point(int floor, int room, int index){
this.floor = floor;
this.room = room;
this.index = index;
}
public int compareTo(Point p){
return room - p.room;
}
public String toString(){
return floor + " " + room + " " + index;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
383267779f2a148184b5c7701aec3e00
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class E1637 {
public static void main(String[] args) throws IOException, FileNotFoundException {
// Scanner in = new Scanner(new File("test.in"));
Kattio in = new Kattio();
int T = in.nextInt();
while(T > 0){
T--;
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
long[] x = new long[N + 1];
for(int i = 1; i <= N; i++){
x[i] = in.nextInt();
}
ArrayList<Point>[] points = new ArrayList[N + 1];
for(int i = 0; i <= N; i++){
points[i] = new ArrayList<>();
}
points[1].add(new Point(1, 0, -1, -1));
points[N].add(new Point(M, 1, -1, -1));
int pointCounter = 2;
for(int i = 0; i < K; i++){
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int d = in.nextInt();
int h = in.nextInt();
points[a].add(new Point(b, pointCounter, pointCounter + 1, h));
pointCounter++;
points[c].add(new Point(d, pointCounter, -1, -1));
pointCounter++;
}
boolean[] visited = new boolean[pointCounter + 1];
long[] dist = new long[pointCounter + 1];
long INF = Long.MAX_VALUE;
Arrays.fill(dist, INF);
dist[0] = 0;
visited[0] = true;
for(int floor = 1; floor <= N; floor++){
ArrayList<Point> row = points[floor];
int len = row.size();
Collections.sort(row);
// System.out.println("FLOOR " + floor + " " + row);
for(int j = 0; j < len - 1; j++){
Point leftPoint = row.get(j);
Point rightPoint = row.get(j + 1);
int dx = rightPoint.room - leftPoint.room;
if(visited[leftPoint.index]){
dist[rightPoint.index] = Math.min(dist[rightPoint.index], dist[leftPoint.index] + dx*x[floor]);
visited[rightPoint.index] = true;
}
}
for(int j = len - 2; j >= 0; j--){
Point leftPoint = row.get(j);
Point rightPoint = row.get(j + 1);
int dx = rightPoint.room - leftPoint.room;
if(visited[rightPoint.index]){
dist[leftPoint.index] = Math.min(dist[leftPoint.index], dist[rightPoint.index] + dx*x[floor]);
visited[leftPoint.index] = true;
}
}
for(Point p: row){
if(p.next != -1 && visited[p.index]){
dist[p.next] = dist[p.index] - p.healthPoints;
visited[p.next] = true;
}
}
}
// System.out.println(Arrays.toString(dist));
if(!visited[1]){
System.out.println("NO ESCAPE");
} else {
System.out.println(dist[1]);
}
}
}
public static class Point implements Comparable<Point> {
int room, index, next, healthPoints;
public Point(int room, int index, int next, int healthPoints){
this.room = room;
this.index = index;
this.next = next;
this.healthPoints = healthPoints;
}
public int compareTo(Point p){
return room - p.room;
}
public String toString(){
return room + " " + index + " " + next + " " + healthPoints;
}
}
public static class Ladder {
Point start, end;
int healthPoints;
public Ladder(Point start, Point end, int healthPoints){
this.start = start;
this.end = end;
this.healthPoints = healthPoints;
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in,System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName+".out");
r = new BufferedReader(new FileReader(problemName+".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {}
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
2c6f1204e0d35e506bb5d5d5704f1f35
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class E {
public static long INF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solve(io);
}
io.close();
}
public static void solve(Kattio io) {
int n = io.nextInt();
int m = io.nextInt();
int k = io.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = io.nextInt();
}
var important = new ArrayList<ArrayList<Point>>();
for (int i = 0; i < n; i++) {
important.add(new ArrayList<>());
}
Point.counter = 0;
important.get(0).add(new Point(0));
important.get(n - 1).add(new Point(m - 1));
for (int i = 0; i < k; i++) {
int a = io.nextInt() - 1;
int b = io.nextInt() - 1;
int c = io.nextInt() - 1;
int d = io.nextInt() - 1;
int h = io.nextInt();
var startPoint = new Point(b);
var endPoint = new Point(d);
startPoint.nxt = endPoint.id;
startPoint.h = h;
important.get(a).add(startPoint);
important.get(c).add(endPoint);
}
long[] dist = new long[Point.counter];
boolean[] reached = new boolean[Point.counter];
Arrays.fill(dist, INF);
dist[0] = 0; // distance to starting point
reached[0] = true;
for (int i = 0; i < n; i++) {
var row = important.get(i);
int len = row.size();
Collections.sort(row);
for (int j = 0; j < len - 1; j++) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[leftPoint.id]) {
dist[rightPoint.id] = Math.min(dist[rightPoint.id], dist[leftPoint.id] + dx * x[i]);
reached[rightPoint.id] = true;
}
}
for (int j = len - 2; j >= 0; j--) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[rightPoint.id]) {
dist[leftPoint.id] = Math.min(dist[leftPoint.id], dist[rightPoint.id] + dx * x[i]);
reached[leftPoint.id] = true;
}
}
for (var point : row) {
if (point.nxt != -1 && reached[point.id]) {
dist[point.nxt] = dist[point.id] - point.h;
reached[point.nxt] = true;
}
}
}
if (reached[1]) {
io.println(dist[1]);
} else {
io.println("NO ESCAPE");
}
}
static class Point implements Comparable<Point> {
public int x;
public int id;
public int nxt;
public int h;
// count of how many important points have been created so far
public static int counter = 0;
public Point(int x) {
this.x = x;
this.nxt = -1;
this.h = -1;
this.id = counter;
counter++;
}
@Override
public int compareTo(Point other) {
return Integer.compare(x, other.x);
}
}
}
// modified from https://github.com/Kattis/kattio/blob/master/Kattio.java
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
111c347614498eb5af7e514b293c1d6e
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
//Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n, m, k;
static int a, b, c, d, h;
static long[] x;
static ArrayList<Ladder>[] start;
static TreeMap<Integer, Long>[] dp;
static long res;
public static void main(String[] args) throws IOException {
t = in.iscan();
outer : while (t-- > 0) {
n = in.iscan(); m = in.iscan(); k = in.iscan();
x = new long[n+1];
for (int i = 1; i <= n; i++) {
x[i] = in.lscan();
}
start = new ArrayList[n+1];
dp = new TreeMap[n+1];
for (int i = 0; i <= n; i++) {
start[i] = new ArrayList<Ladder>();
dp[i] = new TreeMap<Integer, Long>();
}
for (int i = 0; i < k; i++) {
a = in.iscan(); b = in.iscan(); c = in.iscan(); d = in.iscan(); h = in.iscan();
start[a].add(new Ladder(b, c, d, h));
dp[c].put(d, Long.MAX_VALUE);
}
dp[1].put(1, 0L);
res = Long.MAX_VALUE;
for (int i = 1; i <= n; i++) {
Collections.sort(start[i]);
if (dp[i].isEmpty()) {
continue;
}
Integer tmp = dp[i].firstKey();
while (tmp != null) {
if (dp[i].get(tmp) == Long.MAX_VALUE) {
dp[i].remove(tmp);
}
tmp = dp[i].higherKey(tmp);
}
if (dp[i].isEmpty()) {
continue;
}
if (i == n) {
Integer nxt = dp[i].firstKey();
while (nxt != null) {
if (dp[i].get(nxt) != Long.MAX_VALUE) {
res = Math.min(res, dp[i].get(nxt) + x[i] * Math.abs(m - nxt));
}
nxt = dp[i].higherKey(nxt);
}
}
else {
int sz = dp[i].size();
int prev = dp[i].firstKey();
for (int j = 1; j < sz; j++) {
int cur = dp[i].higherKey(prev);
if (dp[i].get(prev) + x[i] * Math.abs(cur - prev) < dp[i].get(cur)) {
dp[i].put(cur, dp[i].get(prev) + x[i] * Math.abs(cur - prev));
}
prev = cur;
}
int nxt = dp[i].lastKey();
for (int j = sz-2; j >= 0; j--) {
int cur = dp[i].lowerKey(nxt);
if (dp[i].get(nxt) + x[i] * Math.abs(nxt - cur) < dp[i].get(cur)) {
dp[i].put(cur, dp[i].get(nxt) + x[i] * Math.abs(nxt - cur));
}
nxt = cur;
}
for (Ladder l : start[i]) {
int col = l.col, nextRow = l.nextRow, nextCol = l.nextCol, h = l.h;
Integer _prev = dp[i].floorKey(col);
Integer _nxt = dp[i].ceilingKey(col);
if (_prev != null) {
dp[nextRow].put(nextCol,
Math.min(dp[nextRow].get(nextCol), dp[i].get(_prev) + x[i] * Math.abs(col - _prev) - h));
}
if (_nxt != null) {
dp[nextRow].put(nextCol,
Math.min(dp[nextRow].get(nextCol), dp[i].get(_nxt) + x[i] * Math.abs(col - _nxt) - h));
}
}
}
}
if (res != Long.MAX_VALUE) out.println(res);
else out.println("NO ESCAPE");
}
out.close();
}
static class Pair {
int r, c;
Pair(int r, int c){
this.r = r;
this.c = c;
}
public int hashCode() { return Objects.hash(r, c); }
public boolean equals(Object o) {
Pair p = (Pair)o;
return r == p.r && c == p.c;
}
}
static class Ladder implements Comparable<Ladder>{
int col, nextRow, nextCol, h;
Ladder(int col, int nextRow, int nextCol, int h){
this.col = col;
this.nextRow = nextRow;
this.nextCol = nextCol;
this.h = h;
}
public int compareTo(Ladder l) {
return col - l.col;
}
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
590d71a8b9ed2fe12942cdde65d91cba
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
public class Ram3_peeked {
static class Ladder {
Room target;
int healthLost;
}
static class Room {
int floor;
int room;
long health;
ArrayList<Ladder> ladders = new ArrayList<>();
}
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
var t = in.nextInt();
for (int i = 0; i < t; i++) {
solve();
}
}
public static void solve() {
var n = in.nextInt();
var m = in.nextInt();
var k = in.nextInt();
var x = new int[n];
for (int i = 0; i < n; i++) {
int loss = in.nextInt();
x[i] = loss;
}
var rooms = new ArrayList<Map<Integer, Room>>();
for (int i = 0; i < n; i++) {
rooms.add(new HashMap<>());
}
var source = new Room();
source.health = 0;
source.room = 0;
source.floor = 0;
var sink = new Room();
sink.health = Long.MAX_VALUE;
sink.room = m - 1;
sink.floor = n - 1;
rooms.get(0).put(0, source);
rooms.get(n - 1).put(m - 1, sink);
for (int i = 0; i < k; i++) {
int a = in.nextInt() - 1;
int b = in.nextInt() - 1;
int c = in.nextInt() - 1;
int d = in.nextInt() - 1;
int h = in.nextInt();
var startRoom = new Room();
startRoom.floor = a;
startRoom.room = b;
startRoom.health = Long.MAX_VALUE;
if (rooms.get(a).containsKey(b)) {
startRoom = rooms.get(a).get(b);
} else {
rooms.get(a).put(b, startRoom);
}
var endRoom = new Room();
endRoom.floor = c;
endRoom.room = d;
endRoom.health = Long.MAX_VALUE;
if (rooms.get(c).containsKey(d)) {
endRoom = rooms.get(c).get(d);
} else {
rooms.get(c).put(d, endRoom);
}
var ladder = new Ladder();
ladder.target = endRoom;
ladder.healthLost = -h;
startRoom.ladders.add(ladder);
}
for (int i = 0; i < n; i++) {
// List of important rooms
var importantRooms = new ArrayList<>(rooms.get(i).values());
importantRooms.sort(Comparator.comparing(r -> r.room));
// traverse left to right
for (int j = 0; j < importantRooms.size() - 1; j++) {
var curr = importantRooms.get(j);
if (curr.health == Long.MAX_VALUE) {
continue;
}
var next = importantRooms.get(j + 1);
var lost =
Math.abs(curr.room - next.room) * (long) x[i] + curr.health;
if (lost < next.health) {
next.health = lost;
}
}
Collections.reverse(importantRooms);
// traverse right to left
for (int j = 0; j < importantRooms.size() - 1; j++) {
var curr = importantRooms.get(j);
if (curr.health == Long.MAX_VALUE) {
continue;
}
var next = importantRooms.get(j + 1);
var lost =
Math.abs(curr.room - next.room) * (long) x[i] + curr.health;
if (lost < next.health) {
next.health = lost;
}
}
for (var room : importantRooms) {
if (room.health == Long.MAX_VALUE) {
continue;
}
for (var ladder : room.ladders) {
var lost = room.health + ladder.healthLost;
if (lost < ladder.target.health) {
ladder.target.health = lost;
}
}
}
}
if (sink.health == Long.MAX_VALUE) {
System.out.println("NO ESCAPE");
} else {
System.out.println(sink.health);
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
710d2afa237c1dbe26071c0c5421f55c
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class q5 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// public static long mod = 1000000007;
public static class Point implements Comparable<Point>{
long b;
long c;
Point next;
long h;
long dist;
Point(long b,long c,long h){
this.b = b;
this.c = c;
this.next = null;
this.h = h;
this.dist = Long.MAX_VALUE;
}
public int compareTo(Point other){
return this.b == other.b ? Long.compare(this.dist,other.dist) : Long.compare(this.b,other.b);
}
}
public static void solve() throws Exception {
String[] parts = br.readLine().split(" ");
long n = Integer.parseInt(parts[0]);
long m = Integer.parseInt(parts[1]);
long k = Integer.parseInt(parts[2]);
long[] hurt = new long[(int)n];
parts = br.readLine().split(" ");
for(int i = 0;i < n;i++){
hurt[i] = Integer.parseInt(parts[i]);
}
ArrayList<ArrayList<Point>> ladders = new ArrayList<>();
for(int i = 0;i < n;i++) ladders.add(new ArrayList<>());
Point startPoint = new Point(0,0,0);
startPoint.dist = 0;
ladders.get(0).add(startPoint);
for(int i = 0;i < k;i++){
parts = br.readLine().split(" ");
long a = Integer.parseInt(parts[0]) - 1;
long b = Integer.parseInt(parts[1]) - 1;
long c = Integer.parseInt(parts[2]) - 1;
long d = Integer.parseInt(parts[3]) - 1;
long h = Integer.parseInt(parts[4]);
Point start = new Point(b,c,h);
Point end = new Point(d,c,0);
start.next = end;
ladders.get((int)a).add(start);
ladders.get((int)c).add(end);
}
ladders.get((int)n - 1).add(new Point(m - 1,n - 1,0));
long ans = Long.MAX_VALUE;
for(int i = 0;i < n;i++){
ArrayList<Point> arr = ladders.get(i);
Collections.sort(arr);
for(int j = 1;j < arr.size();j++){
Point prev = arr.get(j - 1);
Point curr = arr.get(j);
if(prev.dist != Long.MAX_VALUE) curr.dist = Math.min(curr.dist,prev.dist + (curr.b - prev.b) * hurt[i]);
}
for(int j = arr.size() - 2;j >= 0;j--){
Point curr = arr.get(j);
Point next = arr.get(j + 1);
if(next.dist != Long.MAX_VALUE) curr.dist = Math.min(curr.dist,next.dist + (next.b - curr.b) * hurt[i]);
}
for(Point curr : arr){
if(i == n - 1 && curr.b == m - 1) ans = Math.min(ans,curr.dist);
if(curr.next == null || curr.dist == Long.MAX_VALUE) continue;
Point next = curr.next;
next.dist = Math.min(next.dist,curr.dist - curr.h);
}
}
if(ans == Long.MAX_VALUE) System.out.println("NO ESCAPE");
else System.out.println(ans);
}
public static void main(String[] args) throws Exception {
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
solve();
}
}
// public static ArrayList<Integer> primes;
// public static void seive(int n){
// primes = new ArrayList<>();
// boolean[] arr = new boolean[n + 1];
// Arrays.fill(arr,true);
//
// for(int i = 2;i * i <= n;i++){
// if(arr[i]) {
// for (int j = i * i; j <= n; j += i) {
// arr[j] = false;
// }
// }
// }
// for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i);
// }
// public static void sort(int[] arr){
// ArrayList<Integer> temp = new ArrayList<>();
// for(int val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
// public static void sort(long[] arr){
// ArrayList<Long> temp = new ArrayList<>();
// for(long val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
//
// public static long power(long a,long b,long mod){
// if(b == 0) return 1;
//
// long p = power(a,b / 2,mod);
// p = (p * p) % mod;
//
// if(b % 2 == 1) return (p * a) % mod;
// return p;
// }
// public static long modDivide(long a,long b,long mod){
// return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod;
// }
//
// public static int GCD(int a,int b){
// return b == 0 ? a : GCD(b,a % b);
// }
// public static long GCD(long a,long b){
// return b == 0 ? a : GCD(b,a % b);
// }
//
// public static int LCM(int a,int b){
// return a * b / GCD(a,b);
// }
// public static long LCM(long a,long b){
// return a * b / GCD(a,b);
// }
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
62b13f9df60be757fca7bd5b0ff2add8
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
//package notescaping;
import java.util.*;
import java.io.*;
public class notescaping {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
StringTokenizer st = new StringTokenizer(fin.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] floorCost = new int[n];
st = new StringTokenizer(fin.readLine());
for(int i = 0; i < n; i++) {
floorCost[i] = Integer.parseInt(st.nextToken());
}
ArrayList<ArrayList<long[]>> ladders = new ArrayList<ArrayList<long[]>>();
for(int i = 0; i < n; i++) {
ladders.add(new ArrayList<long[]>());
}
for(int i = 0; i < k; i++) {
st = new StringTokenizer(fin.readLine());
long[] next = new long[6];
for(int j = 0; j < 5; j++) {
next[j] = Integer.parseInt(st.nextToken());
if(j < 4) {
next[j] --;
}
}
ladders.get((int) next[0]).add(next);
}
ArrayList<ArrayList<long[]>> exits = new ArrayList<ArrayList<long[]>>(); //cost, room
for(int i = 0; i < n; i++) {
exits.add(new ArrayList<long[]>());
}
for(long[] l : ladders.get(0)) {
long nextVal = l[1] * floorCost[0] - l[4];
long c = l[2];
long d = l[3];
exits.get((int) c).add(new long[] {nextVal, d});
}
//go through the floors
for(int i = 1; i < n - 1; i++) {
//check if it contains an exit
if(exits.get(i).size() == 0) {
continue;
}
exits.get(i).addAll(ladders.get(i));
exits.get(i).sort((a, b) -> a[1] == b[1]? Integer.compare(a.length, b.length) : Long.compare(a[1], b[1]));
int minFound = 0;
//left right pass
long val = 0;
boolean found = false;
long prevRoom = 0;
for(int j = 0; j < exits.get(i).size(); j++) {
if(exits.get(i).get(j).length == 2) {
if(!found) {
minFound = (int) exits.get(i).get(j)[1];
val = exits.get(i).get(j)[0];
}
else {
long curRoom = exits.get(i).get(j)[1];
val += Math.abs(prevRoom - curRoom) * floorCost[i];
}
found = true;
val = Math.min(val, exits.get(i).get(j)[0]);
}
else {
if(!found) {
continue;
}
long curRoom = exits.get(i).get(j)[1];
val += Math.abs(prevRoom - curRoom) * floorCost[i];
exits.get(i).get(j)[5] = val;
}
prevRoom = exits.get(i).get(j)[1];
}
exits.get(i).sort((a, b) -> a[1] == b[1]? -Integer.compare(a.length, b.length) : Long.compare(a[1], b[1]));
//right left pass
val = 0;
found = false;
prevRoom = 0;
for(int j = exits.get(i).size() - 1; j >= 0; j--) {
if(exits.get(i).get(j).length == 2) {
if(!found) {
val = exits.get(i).get(j)[0];
}
else {
long curRoom = exits.get(i).get(j)[1];
val += Math.abs(prevRoom - curRoom) * floorCost[i];
}
found = true;
val = Math.min(val, exits.get(i).get(j)[0]);
}
else {
if(!found) {
continue;
}
long curRoom = exits.get(i).get(j)[1];
val += Math.abs(prevRoom - curRoom) * floorCost[i];
exits.get(i).get(j)[5] = curRoom <= minFound? val : Math.min(val, exits.get(i).get(j)[5]);
}
prevRoom = exits.get(i).get(j)[1];
}
for(long[] e : exits.get(i)) {
if(e.length == 2) {
continue;
}
long nextFloor = e[2];
exits.get((int) nextFloor).add(new long[] {e[5] - e[4], e[3]});
}
}
long ans = Long.MAX_VALUE;
if(exits.get(n - 1).size() == 0) {
fout.append("NO ESCAPE\n");
continue;
}
for(long[] e : exits.get(n - 1)) {
long diff = Math.abs(e[1] - (m - 1));
ans = Math.min(ans, diff * floorCost[n - 1] + e[0]);
}
fout.append(ans).append("\n");
}
System.out.print(fout);
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
17d9e2813e219977a636648f12df9a09
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static void main(String args[]){
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t=1;
t = in.nextInt();
for(int i=1;i<=t;i++)
{
solver.solve(in,i);
}
}
static class TASK {
static int mod = 1000000007;
static void solve(ArrayList<pair> al,int x)
{
for(int i=1;i<al.size();i++)
{
al.get(i).y=Math.min((al.get(i).x-al.get(i-1).x)*1l*x + al.get(i-1).y,al.get(i).y);
al.get(i).flag=al.get(i).flag|al.get(i-1).flag;
}
for(int i=al.size()-2;i>=0;i--)
{
al.get(i).y=Math.min((al.get(i+1).x-al.get(i).x)*1l*x + al.get(i+1).y,al.get(i).y);
al.get(i).flag=al.get(i).flag|al.get(i+1).flag;
}
}
static void solve(InputReader in, int testNumber) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int x[] = new int[n+1];
Set<String> set = new HashSet<>();
Map<String,Integer> map = new HashMap<>();
ArrayList<pair> al[] = new ArrayList[n+1];
ArrayList<ladder> bl = new ArrayList<>();
al[0]=new ArrayList<>();
for(int i=1;i<=n;i++)
{
x[i]=in.nextInt();
al[i]=new ArrayList<>();
}
set.add(1+" "+1);
set.add(n+" "+m);
al[1].add(new pair(1,Long.MAX_VALUE/2,false));
al[n].add(new pair(m,Long.MAX_VALUE/2,true));
for(int i=0;i<k;i++)
{
int a[] = new int[5];
for(int j=0;j<5;j++)
{
a[j]=in.nextInt();
}
bl.add(new ladder(a));
if(!set.contains(a[0]+" "+a[1]))
{
set.add(a[0]+" "+a[1]);
al[a[0]].add(new pair(a[1],Long.MAX_VALUE/2,false));
}
if(!set.contains(a[2]+" "+a[3]))
{
set.add(a[2]+" "+a[3]);
al[a[2]].add(new pair(a[3],Long.MAX_VALUE/2,false));
}
}
Collections.sort(bl, new Comparator<ladder>() {
@Override
public int compare(ladder o1, ladder o2) {
return o2.a[2]-o1.a[2];
}
});
for(int i=1;i<=n;i++)
{
Collections.sort(al[i], new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
return o1.x-o2.x;
}
});
for(int j=0;j<al[i].size();j++)
{
map.put(i+" "+al[i].get(j).x,j);
}
}
for(int i=al[n].size()-1;i>=0;i--)
{
al[n].get(i).y=(m-al[n].get(i).x)*1l*x[n];
al[n].get(i).flag=true;
}
int idx=0;
int flor=n+1;
while (idx<k)
{
// System.out.println(flor);
if(bl.get(idx).a[2]!=flor)
{
flor--;
solve(al[flor],x[flor]);
continue;
}
int a = bl.get(idx).a[0];
int b = map.get(a+" "+bl.get(idx).a[1]);
int c = bl.get(idx).a[2];
int d = map.get(c+" "+bl.get(idx).a[3]);
int h = bl.get(idx).a[4];
// System.out.println(a+" "+b+" "+c+" "+d+" "+h);
if(al[c].get(d).flag) {
al[a].get(b).y = Math.min(al[a].get(b).y, al[c].get(d).y - h);
al[a].get(b).flag = al[c].get(d).flag | al[a].get(b).flag;
}
idx++;
}
while (flor>0)
{
solve(al[flor],x[flor]);
flor--;
}
if(!al[1].get(0).flag)
System.out.println("NO ESCAPE");
else
System.out.println(al[1].get(0).y);
}
}
static class pair{
int x;
long y;
boolean flag;
pair(int x,long y,boolean flag)
{
this.x=x;
this.y=y;
this.flag=false;
}
}
static class ladder{
int a[] = new int[5];
ladder(int a[])
{
for(int i=0;i<5;i++)
{
this.a[i]=a[i];
}
}
}
static class Maths {
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c
== -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
c115568422a68e3ee3f2466c79bb0f90
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static long INF = 1000000000000000000L;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] loseHealthPoints = new int[n];
for (int i = 0; i < n; i++) {
loseHealthPoints[i] = sc.nextInt();
}
// It's not possible to create all the edges, since there are too many of them.
// So we use coordinate compression like thing, adding only some relevant edges.
// Relevant edges would be : all the ladders going up (directed edge) to connect different levels of the building,
// and bidirectional edges connecting ladders on the same floor
// and edges from start-start and end-end
List<Integer>[] points = new List[n];
for (int i = 0; i < n; i++) {
points[i] = new ArrayList<>();
}
points[0].add(0); // edges from start-start and end-end
points[n - 1].add(m - 1);
Map<Integer, List<int[]>> edges = new HashMap<>();
for (int i = 0; i < n; i++) {
edges.put(i, new ArrayList<>());
}
for (int i = 0; i < k; i++) {
int x1 = sc.nextInt() - 1; // ladder bottom
int y1 = sc.nextInt() - 1;
int x2 = sc.nextInt() - 1; // ladder top
int y2 = sc.nextInt() - 1;
int gainHealthPoints = sc.nextInt();
edges.get(x1).add(new int[]{y1, x2, y2, gainHealthPoints}); // directional edges connecting different floors
points[x1].add(y1);
points[x2].add(y2);
}
// store all unique sorted points
for (int i = 0; i < n; i++) {
TreeSet<Integer> treeSet = new TreeSet<>(points[i]);
points[i] = new ArrayList<>(treeSet);
}
long[][] dp = new long[n][1]; // dp[i][j] is the minimum amount of health Ram loses to reach [i, j] if he takes the most optimal path
for (int i = 0; i < n; i++) {
dp[i] = new long[points[i].size()];
Arrays.fill(dp[i], INF);
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
int size = points[i].size();
// travelling across floors
for (int j = 0; j + 1 < size; j++) {
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j + 1) - points[i].get(j)));
}
for (int j = size - 1; j > 0; j--) {
dp[i][j - 1] = Math.min(dp[i][j - 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j) - points[i].get(j - 1)));
}
// use the ladders
for (int[] edge : edges.get(i)) {
int x = binarySearch(points[i], edge[0]);
int y = binarySearch(points[edge[1]], edge[2]);
dp[edge[1]][y] = Math.min(dp[edge[1]][y], dp[i][x] - edge[3]);
}
}
long res = dp[n - 1][points[n - 1].size() - 1];
if (res > 100000000000000000L) {
out.println("NO ESCAPE");
}else {
out.println(res);
}
}
private static int binarySearch(List<Integer> point, int minValue) {
int lo = 0, hi = point.size() - 1;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (point.get(mid) >= minValue) {
hi = mid;
}else {
lo = mid + 1;
}
}
return lo;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException end)
{
end.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
9b9442526e65523645f72ea64e0a317c
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static long INF = 1000000000000000000L;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] loseHealthPoints = new int[n];
for (int i = 0; i < n; i++) {
loseHealthPoints[i] = sc.nextInt();
}
// It's not possible to create all the edges, since there are too many of them.
// So we use coordinate compression like thing, adding only some relevant edges.
// Relevant edges would be : all the ladders going up (directed edge) to connect different levels of the building,
// and bidirectional edges connecting ladders on the same floor
// and edges from start-start and end-end
List<Integer>[] points = new List[n];
for (int i = 0; i < n; i++) {
points[i] = new ArrayList<>();
}
points[0].add(0); // edges from start-start and end-end
points[n - 1].add(m - 1);
Map<Integer, List<int[]>> edges = new HashMap<>();
for (int i = 0; i < n; i++) {
edges.put(i, new ArrayList<>());
}
for (int i = 0; i < k; i++) {
int x1 = sc.nextInt() - 1; // ladder bottom
int y1 = sc.nextInt() - 1;
int x2 = sc.nextInt() - 1; // ladder top
int y2 = sc.nextInt() - 1;
int gainHealthPoints = sc.nextInt();
edges.get(x1).add(new int[]{y1, x2, y2, gainHealthPoints}); // directional edges connecting different floors
points[x1].add(y1);
points[x2].add(y2);
}
// store all unique sorted points
for (int i = 0; i < n; i++) {
TreeSet<Integer> treeSet = new TreeSet<>(points[i]);
points[i] = new ArrayList<>(treeSet);
}
long[][] dp = new long[n][1]; // dp[i][j] is the minimum amount of health Ram loses to reach [i, j] if he takes the most optimal path
for (int i = 0; i < n; i++) {
dp[i] = new long[points[i].size()];
Arrays.fill(dp[i], INF);
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
int size = points[i].size();
for (int j = 0; j + 1 < size; j++) {
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j + 1) - points[i].get(j)));
}
for (int j = size - 1; j > 0; j--) {
dp[i][j - 1] = Math.min(dp[i][j - 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j) - points[i].get(j - 1)));
}
for (int[] edge : edges.get(i)) {
int x = binarySearch(points[i], edge[0]);
int y = binarySearch(points[edge[1]], edge[2]);
dp[edge[1]][y] = Math.min(dp[edge[1]][y], dp[i][x] - edge[3]);
}
}
long res = dp[n - 1][points[n - 1].size() - 1];
if (res > 100000000000000000L) {
out.println("NO ESCAPE");
}else {
out.println(res);
}
}
private static int binarySearch(List<Integer> point, int minValue) {
int lo = 0, hi = point.size() - 1;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (point.get(mid) >= minValue) {
hi = mid;
}else {
lo = mid + 1;
}
}
return lo;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException end)
{
end.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
1c9dbc05201aaccf53f18db2d619192f
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
static long INF = 1000000000000000000L;
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] loseHealthPoints = new int[n];
for (int i = 0; i < n; i++) {
loseHealthPoints[i] = sc.nextInt();
}
List<Integer>[] points = new List[n];
for (int i = 0; i < n; i++) {
points[i] = new ArrayList<>();
}
points[0].add(0);
points[n - 1].add(m - 1);
Map<Integer, List<int[]>> edges = new HashMap<>();
for (int i = 0; i < n; i++) {
edges.put(i, new ArrayList<>());
}
for (int i = 0; i < k; i++) {
int x1 = sc.nextInt() - 1;
int y1 = sc.nextInt() - 1;
int x2 = sc.nextInt() - 1;
int y2 = sc.nextInt() - 1;
int gainHealthPoints = sc.nextInt();
edges.get(x1).add(new int[]{y1, x2, y2, gainHealthPoints});
points[x1].add(y1);
points[x2].add(y2);
}
for (int i = 0; i < n; i++) {
TreeSet<Integer> treeSet = new TreeSet<>(points[i]);
points[i] = new ArrayList<>(treeSet);
// for (int j = 0; j < points[i].size(); j++) {
// out.print(points[i].get(j) + " ");
// }
// out.println();
}
// out.println("------------------------------------");
long[][] dp = new long[n][1];
for (int i = 0; i < n; i++) {
dp[i] = new long[points[i].size()];
Arrays.fill(dp[i], INF);
// out.println(dp[i].length);
}
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
int size = points[i].size();
for (int j = 0; j + 1 < size; j++) {
dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j + 1) - points[i].get(j)));
}
for (int j = size - 1; j > 0; j--) {
dp[i][j - 1] = Math.min(dp[i][j - 1], dp[i][j] + (long) loseHealthPoints[i] * (points[i].get(j) - points[i].get(j - 1)));
}
for (int[] edge : edges.get(i)) {
int x = binarySearch(points[i], edge[0]); // >= b
int y = binarySearch(points[edge[1]], edge[2]); // >= d
// out.println(x + " " + y);
dp[edge[1]][y] = Math.min(dp[edge[1]][y], dp[i][x] - edge[3]);
}
}
long res = dp[n - 1][points[n - 1].size() - 1];
if (res > 100000000000000000L) {
out.println("NO ESCAPE");
}else {
out.println(res);
}
}
private static int binarySearch(List<Integer> point, int minValue) {
int lo = 0, hi = point.size() - 1;
while (lo < hi) {
int mid = (lo + hi) >> 1;
if (point.get(mid) >= minValue) {
hi = mid;
}else {
lo = mid + 1;
}
}
return lo;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException end)
{
end.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
e8dc2143527c97096ad5ad80d4939e00
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int MOD = 1000000007;
// After writing solution, quick scan for:
// array out of bounds
// special cases e.g. n=1?
// npe, particularly in maps
//
// Big numbers arithmetic bugs:
// int overflow
// if (x : long) and (y : int), [y = x] does not compile, but [y += x] does
// sorting, or taking max, after MOD
void solve() throws IOException {
int T = ri();
for (int Ti = 0; Ti < T; Ti++) {
int[] nmk = ril(3);
int n = nmk[0];
int m = nmk[1];
int k = nmk[2];
// CAREFUL OVERFLOW
int[] x = ril(n);
List<List<int[]>> ladders = new ArrayList<>(n); // floor start -> [room start, floor end, room end, health]
for (int i = 0; i < n; i++) ladders.add(new ArrayList<>());
for (int i = 0; i < k; i++) {
int[] abcdh = ril(5);
int a = abcdh[0]-1;
int b = abcdh[1]-1;
int c = abcdh[2]-1;
int d = abcdh[3]-1;
int h = abcdh[4];
ladders.get(a).add(new int[]{b, c, d, h});
}
for (List<int[]> laddersOnRow : ladders) {
Collections.sort(laddersOnRow, (l1, l2) -> Integer.compare(l1[0], l2[0]));
}
// for each floor/room, best hp getting there?
// tricky is the fact that there may be many rooms for a particular floor -> n^2
// what's the minimum hp after i take *this* particular ladder?
//
// for a particular floor, it can be subdivided into contiguous segments
// each segment corresponds to a particular ladder that got to there
// floor -> (room -> best hp)
// [room] is only INTERESTING rooms
List<TreeMap<Integer, Long>> hp = new ArrayList<>(n);
for (int i = 0; i < n; i++) hp.add(new TreeMap<>());
hp.get(0).put(0, 0l);
for (int[] ladder : ladders.get(0)) {
long init = (long) x[0] * ladder[0];
long sum = -init + ladder[3];
long existing = hp.get(ladder[1]).getOrDefault(ladder[2], Long.MIN_VALUE);
if (sum > existing) hp.get(ladder[1]).put(ladder[2], sum);
}
// Process ladders starting on floor i in increasing order
for (int i = 1; i < n; i++) {
// [hp] is the "from" list
// [ladders] is the "to" list
TreeMap<Integer, Long> from = hp.get(i);
List<int[]> to = ladders.get(i); // [room start, floor end, room end, health]
// Find the best way to get to THIS ladder from the left
if (from.isEmpty()) continue;
Integer fromIdx = from.firstKey();
int bestFromIdx = fromIdx;
long hpOfBestFrom = from.get(bestFromIdx);
for (int[] ladder : to) {
int toIdx = ladder[0];
if (bestFromIdx > toIdx) continue;
while (fromIdx != null && fromIdx <= toIdx) {
long candidate = from.get(fromIdx) - (long) x[i] * (toIdx - fromIdx);
long existing = hpOfBestFrom - (long) x[i] * (toIdx - bestFromIdx);
if (candidate > existing) {
bestFromIdx = fromIdx;
hpOfBestFrom = from.get(fromIdx);
}
fromIdx = from.higherKey(fromIdx);
}
long score = hpOfBestFrom - (long) x[i] * (toIdx - bestFromIdx) + ladder[3];
long existing = hp.get(ladder[1]).getOrDefault(ladder[2], Long.MIN_VALUE);
if (score > existing) hp.get(ladder[1]).put(ladder[2], score);
}
// From the right
fromIdx = from.lastKey();
bestFromIdx = fromIdx;
hpOfBestFrom = from.get(bestFromIdx);
for (int j = to.size()-1; j >= 0; j--) {
int[] ladder = to.get(j);
int toIdx = ladder[0];
if (bestFromIdx < toIdx) continue;
while (fromIdx != null && fromIdx >= toIdx) {
long candidate = from.get(fromIdx) - (long) x[i] * (fromIdx - toIdx);
long existing = hpOfBestFrom - (long) x[i] * (bestFromIdx - toIdx);
if (candidate > existing) {
bestFromIdx = fromIdx;
hpOfBestFrom = from.get(fromIdx);
}
fromIdx = from.lowerKey(fromIdx);
}
long score = hpOfBestFrom - (long) x[i] * (bestFromIdx - toIdx) + ladder[3];
long existing = hp.get(ladder[1]).getOrDefault(ladder[2], Long.MIN_VALUE);
if (score > existing) hp.get(ladder[1]).put(ladder[2], score);
}
}
long ans = Long.MIN_VALUE;
Map<Integer, Long> lastFloor = hp.get(n-1);
for (int idx : lastFloor.keySet()) {
long damage = (long) x[n-1] * (m-1 - idx);
long finalHp = lastFloor.get(idx) - damage;
ans = Math.max(ans, finalHp);
}
if (ans == Long.MIN_VALUE) {
pw.println("NO ESCAPE");
} else {
pw.println(-ans);
}
}
}
// IMPORTANT
// DID YOU CHECK THE COMMON MISTAKES ABOVE?
// Template code below
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Main m = new Main();
m.solve();
m.close();
}
void close() throws IOException {
pw.flush();
pw.close();
br.close();
}
int ri() throws IOException {
return Integer.parseInt(br.readLine().trim());
}
long rl() throws IOException {
return Long.parseLong(br.readLine().trim());
}
int[] ril(int n) throws IOException {
int[] nums = new int[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
int x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
long[] rll(int n) throws IOException {
long[] nums = new long[n];
int c = 0;
for (int i = 0; i < n; i++) {
int sign = 1;
c = br.read();
long x = 0;
if (c == '-') {
sign = -1;
c = br.read();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
nums[i] = x * sign;
}
while (c != '\n' && c != -1) c = br.read();
return nums;
}
int[] rkil() throws IOException {
int c = br.read();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return ril(x);
}
long[] rkll() throws IOException {
int c = br.read();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = br.read();
}
return rll(x);
}
char[] rs() throws IOException {
return br.readLine().toCharArray();
}
void sort(int[] A) {
Random r = new Random();
for (int i = A.length-1; i > 0; i--) {
int j = r.nextInt(i+1);
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
Arrays.sort(A);
}
void printDouble(double d) {
pw.printf("%.16f", d);
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
71476d61790d40f7961595507d8ffa49
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class _766 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
ArrayList<Node> [] a = new ArrayList[2 * k + 2];
for (int i = 0; i < 2 * k + 2; i++) a[i] = new ArrayList<>();
long [] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = sc.nextLong();
}
ArrayList<Pair> [] b = new ArrayList[n];
Ladder [] go = new Ladder[2 * k + 2];
for (int i = 0; i < n; i++) b[i] = new ArrayList<>();
for (int i = 0; i < k; i++) {
int si = sc.nextInt() - 1;
int sj = sc.nextInt() - 1;
int ei = sc.nextInt() - 1;
int ej = sc.nextInt() - 1;
b[si].add(new Pair(2 * i, sj));
b[ei].add(new Pair(2 * i + 1, ej));
go[2 * i] = new Ladder(ei, ej, sc.nextLong());
}
if (b[0].size() == 0 || b[n - 1].size() == 0) {
out.println("NO ESCAPE");
continue;
}
int start = 2 * k; int end = 2 * k + 1;
b[0].add(new Pair(start, 0));
b[n - 1].add(new Pair(end, m - 1));
for (int i = 0; i < n; i++) Collections.sort(b[i]);
for (int i = 0; i < n; i++) {
int sz = b[i].size();
for (int j = 0; j < sz - 1; j++) {
int fi = b[i].get(j).i;
int si = b[i].get(j + 1).i;
int cf = b[i].get(j).c;
int cs = b[i].get(j + 1).c;
long d = x[i] * Math.abs(cf - cs);
a[fi].add(new Node(si, d));
a[si].add(new Node(fi, d));
}
}
long [] dist = new long[2 * k + 2];
Arrays.fill(dist, (long) 1e17);
dist[start] = 0;
for (int i = 0; i < n; i++) {
PriorityQueue<Vertex> pq = new PriorityQueue<>(Comparator.comparingLong(y -> y.dist));
for (Pair p: b[i]) pq.add(new Vertex(p.i, dist[p.i]));
while (!pq.isEmpty()) {
Vertex v = pq.poll();
int ii = v.i;
for (Node next: a[ii]) {
if (dist[next.i] > dist[ii] + next.d) {
dist[next.i] = dist[ii] + next.d;
pq.add(new Vertex(next.i, dist[next.i]));
}
}
}
for (Pair p: b[i]) {
if (go[p.i] != null) {
dist[p.i + 1] = Math.min(dist[p.i + 1], dist[p.i] - go[p.i].x);
}
}
}
out.println((dist[end] >= (long) 1e16) ? "NO ESCAPE" : dist[end]);
}
out.close();
}
static class Ladder {
int i; int j;
long x;
Ladder(int i, int j, long x) {
this.i = i; this.j = j; this.x = x;
}
}
static class Pair implements Comparable<Pair> {
int i; int c;
Pair(int i, int c) {
this.i = i; this.c = c;
}
@Override
public int compareTo(Pair o) {
if (c == o.c) {
return Integer.compare(i, o.i);
} else return Integer.compare(c, o.c);
}
}
static class Node {
int i; long d;
Node(int i, long d) {
this.i = i; this.d = d;
}
}
static class Vertex {
int i; long dist;
Vertex(int i, long dist) {
this.i = i; this.dist = dist;
}
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
637ce8976f5ec7c570d13769d3132b24
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.PriorityQueue;
public class E_NotEscaping_766 {
static long INF = (long) 1e18;
static long BLANK = -INF;
static Ladder[] ladder;
static ArrayDeque<Ladder>[] building, endLadder;
static long[] dp, floorScore;
static int target, targetX;
static int n;
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int tt = Integer.parseInt(scan.readLine());
while(tt -- > 0)
{
String[] s = scan.readLine().split(" ");
n = Integer.parseInt(s[0]); int m = Integer.parseInt(s[1]), k = Integer.parseInt(s[2]); targetX = m;
dp = new long[k+1];
floorScore = new long[n+1];
Arrays.fill(dp, BLANK);
ladder = new Ladder[k+1];
s = scan.readLine().split(" ");
for(int i = 0; i < n; i++)
floorScore[i+1] = Long.parseLong(s[i]);
for(int i = 0; i < k; i++)
{
ladder[i+1] = new Ladder(scan.readLine().split(" "), i+1);
}
ladder[0] = new Ladder(0, 0, 1, 1, 0, 0);
building = new ArrayDeque[n+1];
Arrays.setAll(building, x->new ArrayDeque<Ladder>());
for(Ladder l : ladder)
building[l.startY].add(l);
target = n;
long ans = dp();
//System.out.println("dp: " + Arrays.toString(dp));
out.println(ans <= BLANK ? "NO ESCAPE" : -ans);
}
out.flush();
}
public static long dp()
{
endLadder = new ArrayDeque[n+1];
Arrays.setAll(endLadder, x->new ArrayDeque<Ladder>());
for(Ladder l : ladder)
endLadder[l.endY].add(l);
// set last floor
for(Ladder l : endLadder[n]) // the ones that end on floor n
dp[l.id] = -floorScore[n] * (targetX - l.endX) + l.health;
for(int i = n-1; i >= 1; i--)
calcFloor(i);
return dp[0];
}
private static void calcFloor(int floor)
{
leftUpdate(floor);
rightUpdate(floor);
}
private static void rightUpdate(int floor)
{
PriorityQueue<Ladder> start = new PriorityQueue<Ladder>((a, b)->Integer.compare(b.endX, a.endX));
PriorityQueue<Ladder> take = new PriorityQueue<Ladder>((a, b)->Integer.compare(b.startX, a.startX));
for(Ladder l : building[floor])
if(dp[l.id] != BLANK)
take.add(l);
for(Ladder l : endLadder[floor])
start.add(l);
// from the left
long bestNext = BLANK;
long currentX = 0;
Ladder l;
int takeX, startX;
while(!(start.isEmpty() && take.isEmpty()))
{
if(start.isEmpty()) // update ladder score
{
l = take.poll();
takeX = l.startX;
if(takeX != currentX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - takeX) * (-floorScore[floor]);
}
currentX = takeX;
if(dp[l.id] > bestNext)
bestNext = dp[l.id];
}
else if(take.isEmpty()) // update bestNext
{
l = start.poll();
startX = l.endX;
if(currentX != startX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - startX) * (-floorScore[floor]);
}
if(bestNext != BLANK)
dp[l.id] = Math.max(dp[l.id], bestNext + l.health);
currentX = startX;
}
else if(take.peek().startX >= start.peek().endX)
{
l = take.poll();
takeX = l.startX;
if(takeX != currentX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - takeX) * (-floorScore[floor]);
}
currentX = takeX;
if(dp[l.id] > bestNext)
bestNext = dp[l.id];
}
else // update bestNext
{
l = start.poll();
startX = l.endX;
if(currentX != startX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - startX) * (-floorScore[floor]);
}
if(bestNext != BLANK)
dp[l.id] = Math.max(dp[l.id], bestNext + l.health);
currentX = startX;
}
//System.out.println("bestNext is now: " + bestNext);
}
}
private static void leftUpdate(int floor)
{
PriorityQueue<Ladder> start = new PriorityQueue<Ladder>((a, b)->Integer.compare(a.endX, b.endX));
PriorityQueue<Ladder> take = new PriorityQueue<Ladder>((a, b)->Integer.compare(a.startX, b.startX));
for(Ladder l : building[floor])
if(dp[l.id] != BLANK)
take.add(l);
for(Ladder l : endLadder[floor])
start.add(l);
// from the left
long bestNext = BLANK;
long currentX = 0;
Ladder l;
int takeX, startX;
while(!(start.isEmpty() && take.isEmpty()))
{
if(start.isEmpty()) // update ladder score
{
l = take.poll();
takeX = l.startX;
if(takeX != currentX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - takeX) * (-floorScore[floor]);
}
currentX = takeX;
if(dp[l.id] > bestNext)
bestNext = dp[l.id];
}
else if(take.isEmpty()) // update bestNext
{
l = start.poll();
startX = l.endX;
if(currentX != startX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - startX) * (-floorScore[floor]);
}
if(bestNext != BLANK)
dp[l.id] = bestNext + l.health;
currentX = startX;
}
else if(take.peek().startX <= start.peek().endX)
{
l = take.poll();
takeX = l.startX;
if(takeX != currentX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - takeX) * (-floorScore[floor]);
}
currentX = takeX;
if(dp[l.id] > bestNext)
bestNext = dp[l.id];
}
else // update bestNext
{
l = start.poll();
startX = l.endX;
if(currentX != startX && bestNext != BLANK)
{
bestNext += Math.abs(currentX - startX) * (-floorScore[floor]);
}
if(bestNext != BLANK)
dp[l.id] = bestNext + l.health;
currentX = startX;
}
//System.out.println("bestNext is now: " + bestNext);
}
}
// private static long dp(int id)
// {
// if(dp[id] > BLANK) return dp[id];
//
// if(ladder[id].endY >= target)
// return (dp[id] = -floorScore[target-1] * Math.abs(ladder[id].endX - targetX));
//
// int floor = ladder[id].endY;
// int x = ladder[id].endX;
//
// long best = BLANK, score, nextScore;
// for(Ladder l : building[floor])
// {
// score = (- floorScore[floor-1] * Math.abs(l.startX - x) ) + l.health;
// nextScore = dp(l.id);
//
// if(nextScore > BLANK && nextScore + score > best)
// best = score + nextScore;
// }
//
// return (dp[id] = best);
// }
static class Ladder
{
int startX, startY, endX, endY;
long health;
int id;
public Ladder(int sy, int sx, int ey, int ex, long h, int i)
{
startX = sx; startY = sy; endX = ex; endY = ey;
health = h;
id = i;
}
public Ladder(String[] in, int i)
{
this(Integer.parseInt(in[0]), Integer.parseInt(in[1]), Integer.parseInt(in[2]), Integer.parseInt(in[3]), Integer.parseInt(in[4]), i);
}
public String toString()
{
return "x: " + startX + " y: " + startY + " -> x: " + endX + " y: " + endY;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
1d197e84364131bfa1c33ae48cd2ed35
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
//package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
//E. Not Escaping
public class Solution7 {
final static long min = Long.MIN_VALUE + 2;
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0) {
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken());
long[] ns = new long[n+1];
st = new StringTokenizer(input.readLine());
for (int i = 1; i <= n; ++i) {
ns[i] = Integer.parseInt(st.nextToken());
}
int[][] lad = new int[k][];
for (int i=0;i<k;++i){
st = new StringTokenizer(input.readLine());
lad[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
}
Long res = calc(n, m, k, ns, lad);
if (res==null) {
out.println("NO ESCAPE");
}else {
out.println(-res);
}
Q--;
}
out.close(); // close the output file
}
private static Long calc(final int n, final int m, final int k, long[] ns, int[][] lad) {
Arrays.sort(lad, Comparator.comparingInt(a -> a[3]));
ArrayList<long[]>[] ens = new ArrayList[n+1], sts = new ArrayList[n+1];
for (int[] kk : lad){
if (ens[kk[2]]==null)ens[kk[2]]=new ArrayList<>();
if (sts[kk[0]]==null)sts[kk[0]]=new ArrayList<>();
if (ens[kk[2]].size()==0 || ens[kk[2]].get(ens[kk[2]].size()-1)[0] < kk[3]){
ens[kk[2]].add(new long[]{kk[3],min});
}
sts[kk[0]].add(new long[]{kk[1], kk[2], ens[kk[2]].size()-1, kk[4]});
}
ens[1] = new ArrayList<>();
ens[1].add(new long[]{1,0});
for (int i=1;i<=n;++i){
if ((sts[i]!=null || i==n) && ens[i]!=null){
long[] rd1 = new long[ens[i].size()], rd2 = new long[ens[i].size()];
int p = 0;
for (long[] en : ens[i]){
if (en[1]>min && (p==0 || rd1[p-1]-(en[0]-rd2[p-1])*ns[i] < en[1])){
while (p>0){
if (en[1]-(en[0]-rd2[p-1])*ns[i] >= rd1[p-1]){
p--;
}else break;
}
rd1[p] = en[1];
rd2[p++] = en[0];
}
}
if (p==0)continue;
if (sts[i]!=null){
int t = 0;
Collections.sort(sts[i], (a,b)-> (int) (a[0]-b[0]));
for (long[] kk : sts[i]){
while(t<p && rd2[t]<kk[0]){
t++;
}
long v = t>0 ? rd1[t-1]-(kk[0]-rd2[t-1])*ns[i] : rd1[t]-(rd2[t]-kk[0])*ns[i];
if (t<p){
v = Math.max(v, rd1[t]-(rd2[t]-kk[0])*ns[i]);
}
v += kk[3];
ens[(int)kk[1]].get((int)kk[2])[1] = Math.max(ens[(int)kk[1]].get((int)kk[2])[1], v);
}
}else {
return rd1[p-1] - (m-rd2[p-1])*ns[n];
}
}
}
return null;
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
84b0dd3fb5b87c74f2f591f5e59d1ea6
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
//package com.example.practice.codeforces.sc2200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
//E. Not Escaping
public class Solution7 {
final static long min = Long.MIN_VALUE + 2;
public static void main (String [] args) throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
// input file name goes above
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("inflate.out")));
int Q = Integer.parseInt(input.readLine());
while (Q > 0) {
StringTokenizer st = new StringTokenizer(input.readLine());
final int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken());
long[] ns = new long[n+1];
st = new StringTokenizer(input.readLine());
for (int i = 1; i <= n; ++i) {
ns[i] = Integer.parseInt(st.nextToken());
}
int[][] lad = new int[k][];
for (int i=0;i<k;++i){
st = new StringTokenizer(input.readLine());
lad[i] = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())};
}
Long res = calc(n, m, k, ns, lad);
if (res==null) {
out.println("NO ESCAPE");
}else {
out.println(-res);
}
Q--;
}
out.close(); // close the output file
}
private static Long calc(final int n, final int m, final int k, long[] ns, int[][] lad) {
Arrays.sort(lad, Comparator.comparingInt(a -> a[3]));
ArrayList<long[]>[] ens = new ArrayList[n+1], sts = new ArrayList[n+1];
for (int[] kk : lad){
if (ens[kk[2]]==null)ens[kk[2]]=new ArrayList<>();
if (sts[kk[0]]==null)sts[kk[0]]=new ArrayList<>();
if (ens[kk[2]].size()==0 || ens[kk[2]].get(ens[kk[2]].size()-1)[0] < kk[3]){
ens[kk[2]].add(new long[]{kk[3],min});
}
sts[kk[0]].add(new long[]{kk[1], kk[2], ens[kk[2]].size()-1, kk[4]});
}
ens[1] = new ArrayList<>();
ens[1].add(new long[]{1,0});
for (int i=1;i<=n;++i){
if ((sts[i]!=null || i==n) && ens[i]!=null){
long[] rd1 = new long[ens[i].size()], rd2 = new long[ens[i].size()];
int p = 0;
for (long[] en : ens[i]){
if (en[1]>min && (p==0 || rd1[p-1]-(en[0]-rd2[p-1])*ns[i] < en[1])){
while (p>0){
if (en[1]-(en[0]-rd2[p-1])*ns[i] >= rd1[p-1]){
p--;
}else break;
}
rd1[p] = en[1];
rd2[p++] = en[0];
}
}
if (p==0)continue;
if (sts[i]!=null){
int t = 0;
Collections.sort(sts[i], (a,b)-> (int) (a[0]-b[0]));
for (long[] kk : sts[i]){
while(t<p && rd2[t]<kk[0]){
t++;
}
long v = t>0 ? rd1[t-1]-(kk[0]-rd2[t-1])*ns[i] : rd1[t]-(rd2[t]-kk[0])*ns[i];
if (t<p){
v = Math.max(v, rd1[t]-(rd2[t]-kk[0])*ns[i]);
}
v += kk[3];
ens[(int)kk[1]].get((int)kk[2])[1] = Math.max(ens[(int)kk[1]].get((int)kk[2])[1], v);
}
}else {
return rd1[p-1] - (m-rd2[p-1])*ns[n];
}
}
}
return null;
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
4cc6f3c19e9b55ecb7597dfd3c4180e8
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.TreeMap;
public class Main {
private static final long max_ans = 100000L * 100000L * 1000000L + 10L;
private static void run() throws IOException {
int n, m, k;
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
AnsTree[] ansTrees = new AnsTree[n];
for (int i = 0; i < n; i++) {
ansTrees[i] = new AnsTree();
}
for (int i = 0; i < k; i++) {
int a, b, c, d, h;
a = in.nextInt() - 1;
b = in.nextInt() - 1;
c = in.nextInt() - 1;
d = in.nextInt() - 1;
h = -in.nextInt();
ansTrees[a].compute(b, (key, value) -> {
if (value == null) value = new Node(b);
value.edges.add(new Edge(c, d, h));
return value;
});
ansTrees[c].compute(d, (key, value) -> {
if (value == null) value = new Node(d);
return value;
});
}
ansTrees[0].compute(0, (key, value) -> {
if (value == null) value = new Node(0);
value.ans = 0;
return value;
});
ansTrees[n - 1].compute(m - 1, (key, value) -> {
if (value == null) value = new Node(m - 1);
return value;
});
Ans[] ans = new Ans[n];
for (int i = 0; i < n; i++) {
ans[i] = new Ans();
for (var now : ansTrees[i].entrySet()) {
ans[i].add(now.getValue());
}
}
boolean[] visited = new boolean[n];
visited[0] = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) continue;
long min_now = -1;
int min_pos = -1;
for (int j = 0; j < ans[i].size(); j++) {
Node node = ans[i].get(j);
if (min_pos == -1 || node.ans < min_now + x[i] * (node.b - min_pos)) {
min_pos = node.b;
min_now = node.ans;
} else {
node.ans = min_now + x[i] * (node.b - min_pos);
}
}
min_now = -1;
min_pos = -1;
for (int j = ans[i].size() - 1; j >= 0; j--) {
Node node = ans[i].get(j);
if (min_pos == -1 || node.ans < min_now + x[i] * (min_pos - node.b)) {
min_pos = node.b;
min_now = node.ans;
} else {
node.ans = min_now + x[i] * (min_pos - node.b);
}
}
for (var now : ansTrees[i].entrySet()) {
for (Edge edge : now.getValue().edges) {
visited[edge.c] = true;
ansTrees[edge.c].compute(edge.d, (key, value) -> {
if (value == null) throw new RuntimeException();
value.ans = Math.min(value.ans, now.getValue().ans + edge.h);
return value;
});
}
}
}
long output = ans[n - 1].get(ans[n - 1].size() - 1).ans;
out.println(output != max_ans ? output : "NO ESCAPE");
}
private static class AnsTree extends TreeMap<Integer, Node> {
}
private static class Ans extends ArrayList<Node> {
}
private static class Node {
int b;
long ans = max_ans;
ArrayList<Edge> edges = new ArrayList<>();
public Node(int b) {
this.b = b;
}
}
private static class Edge {
int c, d;
long h;
public Edge(int c, int d, long h) {
this.c = c;
this.d = d;
this.h = h;
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long add_mod(long... longs) {
long ans = 0;
for (long now : longs) {
ans = (ans + now) % mod;
if (ans < 0) ans += mod;
}
return ans;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
f1dfb62231c5d62c641ede1c08d02cb9
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;
/*
*/
public class E {
static TreeSet<Node> allNodes=new TreeSet<>();
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
allNodes.clear();
int h=fs.nextInt(), w=fs.nextInt(), nLadders=fs.nextInt();
int[] moveCostAtX=fs.readArray(h);
Node start=get(0, 0);
// Node[] ends=new Node[w];
// for (int i=0; i<w; i++) ends[i]=get(h-1, i);
Node end=get(h-1, w-1);
for (int i=0; i<nLadders; i++) {
Ladder l=new Ladder(fs.nextInt()-1, fs.nextInt()-1, fs.nextInt()-1, fs.nextInt()-1, fs.nextInt());
Node from=get(l.low, l.sx), to=get(l.hi, l.ex);
from.edges.add(new Edge(from, to, -l.health));
}
for (Node nn:allNodes) {
// if (nn.floor==h-1) break;
Node next=allNodes.higher(nn);
if (next==null || next.floor!=nn.floor) continue;
nn.edges.add(new Edge(nn, next, moveCostAtX[nn.floor]*(long)(next.x-nn.x)));
next.edges.add(new Edge(next, nn, moveCostAtX[nn.floor]*(long)(next.x-nn.x)));
}
PriorityQueue<State> pq=new PriorityQueue<>();
pq.add(new State(start, 0));
start.dist=0;
while (!pq.isEmpty()) {
State curState=pq.remove();
if (curState.cost!=curState.at.dist) continue;
for (Edge ee:curState.at.edges) {
long newCost=ee.cost+curState.cost;
if (newCost<ee.to.dist) {
ee.to.dist=newCost;
pq.add(new State(ee.to, newCost));
}
}
}
long ans=end.dist;
// for (Node nn:ends) {
// ans=Math.min(ans, nn.dist);
// }
System.out.println(ans>=Long.MAX_VALUE/2?"NO ESCAPE":(""+ans));
}
}
static Node get(int floor, int x) {
Node test=new Node(floor, x);
Node ceil=allNodes.ceiling(test);
if (ceil!=null && ceil.x==test.x&& ceil.floor==test.floor) return ceil;
allNodes.add(test);
return test;
}
static Node getNextOnFloor(int floor, int x) {
Node test=new Node(floor, x);
Node ceil=allNodes.ceiling(test);
if (ceil==null) return null;
if (ceil.floor==test.floor) return ceil;
return null;
}
static class Node implements Comparable<Node> {
int floor;
int x;
long dist=Long.MAX_VALUE/2;
ArrayList<Edge> edges=new ArrayList<>();
public Node(int floor, int x) {
this.floor=floor;
this.x=x;
}
public int compareTo(Node o) {
if (floor!=o.floor) return Integer.compare(floor, o.floor);
return Integer.compare(x, o.x);
}
}
static class Edge {
Node from;
Node to;
long cost;
public Edge(Node from, Node to, long cost) {
this.from=from;
this.to=to;
this.cost=cost;
}
}
static class State implements Comparable<State> {
Node at;
long cost;
public State(Node at, long cost) {
this.at=at;
this.cost=cost;
}
public int compareTo(State o) {
if (at.floor!=o.at.floor) return Integer.compare(at.floor, o.at.floor);
return Long.compare(cost, o.cost);
}
}
static class Ladder {
int low, hi, health;
int sx, ex;
public Ladder(int low, int sx, int high, int ex, int health) {
this.low=low;
this.hi=high;
this.health=health;
this.sx=sx;
this.ex=ex;
}
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
3a01c847e082aa587fd521d4f511ceb8
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class _1627_E {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
StringTokenizer line = new StringTokenizer(in.readLine());
int n = Integer.parseInt(line.nextToken());
int m = Integer.parseInt(line.nextToken());
int k = Integer.parseInt(line.nextToken());
int[] x = new int[n];
line = new StringTokenizer(in.readLine());
for(int i = 0; i < n; i++) {
x[i] = Integer.parseInt(line.nextToken());
}
ArrayList<Node>[] lower = new ArrayList[n];
ArrayList<Node>[] upper = new ArrayList[n];
for(int i = 0; i < k; i++) {
line = new StringTokenizer(in.readLine());
int[] ladder = new int[5];
for(int j = 0; j < 5; j++) {
ladder[j] = Integer.parseInt(line.nextToken());
}
if(lower[ladder[0] - 1] == null) {
lower[ladder[0] - 1] = new ArrayList<Node>();
}
if(upper[ladder[2] - 1] == null) {
upper[ladder[2] - 1] = new ArrayList<Node>();
}
Node above = new Node(null, Long.MAX_VALUE, 0, ladder[3]);
upper[ladder[2] - 1].add(above);
lower[ladder[0] - 1].add(new Node(above, 0, ladder[4], ladder[1]));
}
upper[0] = new ArrayList<Node>();
upper[0].add(new Node(null, 0, 0, 1));
for(int i = 0; i < n; i++) {
if(lower[i] == null || upper[i] == null) {
continue;
}
Collections.sort(lower[i]);
Collections.sort(upper[i]);
int p = 0;
long min = Long.MAX_VALUE;
for(int j = 0; j < lower[i].size(); j++) {
Node cur = lower[i].get(j);
Node cur2 = null;
while(p < upper[i].size() && (cur2 = upper[i].get(p)).col <= cur.col) {
if(cur2.min != Long.MAX_VALUE) {
min = Math.min(min, (long)-x[i] * cur2.col + cur2.min);
}
p++;
}
if(min != Long.MAX_VALUE) {
cur.above.min = Math.min(cur.above.min, (long)x[i] * cur.col + min - cur.health);
}
}
p = upper[i].size() - 1;
min = Long.MAX_VALUE;
for(int j = lower[i].size() - 1; j >= 0; j--) {
Node cur = lower[i].get(j);
Node cur2 = null;
while(p >= 0 && (cur2 = upper[i].get(p)).col >= cur.col) {
if(cur2.min != Long.MAX_VALUE) {
min = Math.min(min, (long)-x[i] * (m - cur2.col) + cur2.min);
}
p--;
}
if(min != Long.MAX_VALUE) {
cur.above.min = Math.min(cur.above.min, (long)x[i] * (m - cur.col) + min - cur.health);
}
}
}
if(upper[n - 1] != null) {
long res = Long.MAX_VALUE;
for(Node cur : upper[n - 1]) {
if(cur.min != Long.MAX_VALUE) {
res = Math.min(res, (long)x[n - 1] * (m - cur.col) + cur.min);
}
}
if(res == Long.MAX_VALUE) {
out.println("NO ESCAPE");
}else {
out.println(res);
}
}else {
out.println("NO ESCAPE");
}
}
in.close();
out.close();
}
static class Node implements Comparable<Node> {
Node above;
long min;
int health, col;
Node(Node a, long m, int h, int c) {
above = a;
min = m;
health = h;
col = c;
}
@Override
public int compareTo(Node o) {
return col - o.col;
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
97434dc111338e5a11e51970f43b8158
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class Solution extends PrintWriter {
void solve() {
int t = sc.nextInt();
for(int i = 1; i <= t; i++) {
test_case();
}
}
void test_case() {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
long[] x = new long[n];
for(int i = 0; i < n; i++) x[i] = sc.nextLong();
cells = new HashMap<>();
sz = 0;
rows = new ArrayList[n];
for(int i = 0; i < n; i++) rows[i] = new ArrayList<>();
insert(0, 0);
insert(n-1, m-1);
int[] a = new int[k];
int[] b = new int[k];
int[] c = new int[k];
int[] d = new int[k];
int[] h = new int[k];
for(int i = 0; i < k; i++) {
a[i] = sc.nextInt()-1;
b[i] = sc.nextInt()-1;
insert(a[i],b[i]);
c[i] = sc.nextInt()-1;
d[i] = sc.nextInt()-1;
insert(c[i],d[i]);
h[i] = sc.nextInt();
}
for(int i = 0; i < n; i++) {
Collections.sort(rows[i], Comparator.comparing(arr -> arr[0]));
}
adj = new ArrayDeque[sz][3];
for(int i = 0; i < sz; i++) for(int j = 0; j < 3; j++) adj[i][j] = new ArrayDeque<>();
for(int i = 0; i < n; i++) {
for(int j = 0; j < rows[i].size(); j++) {
int[] cur = rows[i].get(j);
if(j-1 >= 0) {
int[] prev = rows[i].get(j-1);
adj[cur[1]][0].add(new long[] {prev[1], 2, (cur[0] - prev[0]) * x[i]});
adj[cur[1]][2].add(new long[] {prev[1], 2, (cur[0] - prev[0]) * x[i]});
}
if(j+1 < rows[i].size()) {
int[] next = rows[i].get(j+1);
adj[cur[1]][0].add(new long[] {next[1], 1, (next[0] - cur[0]) * x[i]});
adj[cur[1]][1].add(new long[] {next[1], 1, (next[0] - cur[0]) * x[i]});
}
}
}
for(int i = 0; i < k; i++) {
int u = cells.get(a[i] + " " + b[i]);
int v = cells.get(c[i] + " " + d[i]);
adj[u][0].add(new long[] {v, 0, -h[i]});
adj[u][1].add(new long[] {v, 0, -h[i]});
adj[u][2].add(new long[] {v, 0, -h[i]});
}
vis = new boolean[sz][3];
dp = new long[sz][3];
for(int i = 0; i < sz; i++) for(int j = 0; j < 3; j++) dp[i][j] = 1L<<60;
dp[1][0] = 0;
dp[1][1] = 0;
dp[1][2] = 0;
dfs(new long[] {0L,0L});
boolean good = false;
for(int i = 0; i < 3; i++) {
good = good || vis[1][i];
}
if(!good) {
println("NO ESCAPE");
return;
}
println(dp[0][0]);
}
ArrayDeque<long[]>[][] adj;
boolean[][] vis;
long[][] dp;
void dfs(long[] u) {
int u0 = (int)u[0];
int u1 = (int)u[1];
vis[u0][u1] = true;
for(long[] v : adj[u0][u1]) {
int v0 = (int)v[0];
int v1 = (int)v[1];
if(!vis[v0][v1]) {
dfs(v);
}
if(dp[v0][v1]+v[2] < dp[u0][u1]) {
dp[u0][u1] = dp[v0][v1]+v[2];
}
}
}
HashMap<String, Integer> cells;
ArrayList<int[]>[] rows;
int sz;
void insert(int x, int y) {
String point = x + " " + y;
if(cells.get(point) == null) {
cells.put(point, sz);
rows[x].add(new int[] {y, sz++});
}
}
// Main() throws FileNotFoundException { super(new File("output.txt")); }
// InputReader sc = new InputReader(new FileInputStream("test_input.txt"));
Solution() { super(System.out); }
InputReader sc = new InputReader(System.in);
static class InputReader {
InputReader(InputStream in) { this.in = in; } InputStream in;
private byte[] buf = new byte[16384];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static void main(String[] $) {
new Thread(null, new Runnable() {
public void run() {
long start = System.nanoTime();
try {Solution solution = new Solution(); solution.solve(); solution.flush();}
catch (Exception e) {e.printStackTrace(); System.exit(1);}
System.err.println((System.nanoTime()-start)/1E9);
}
}, "1", 1 << 27).start();
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
96c50ada54b6c4e297799a45936d6abc
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
// package c1627;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeMap;
//
// Codeforces Round #766 (Div. 2) 2022-01-15 06:35
// E. Not Escaping
// https://codeforces.com/contest/1627/problem/E
// time limit per test 2 seconds; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to
// escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to
// reach the top of the building to lose the minimum amount of health.
//
// The building consists of n floors, each with m rooms each. Let (i, j) represent the j-th room on
// the i-th floor. Additionally, there are k ladders installed. The i-th ladder allows Ram to travel
// from (a_i, b_i) to (c_i, d_i), but . Ram also gains h_i health points if he uses the ladder i.
//
// If Ram is on the i-th floor, he can move either left or right. Travelling across floors, however,
// is treacherous. If Ram travels from (i, j) to (i, k), he loses |j-k| * x_i health points.
//
// Ram enters the building at (1, 1) while his helicopter is waiting at (n, m). What is the minimum
// amount of health Ram loses if he takes the most optimal path? Note this answer may be negative
// (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot
// escape the clutches of Raghav.
// https://espresso.codeforces.com/ebfd0ebeb312947b542ca1808afa3169c410aec1.png
//
// Input
//
// The first line of input contains t (1 <= t <= 5 * 10^4)-- the number of test cases.
//
// The first line of each test case consists of 3 integers n, m, k (2 <= n, m <= 10^5; 1 <= k <=
// 10^5)-- the number of floors, the number of rooms on each floor and the number of ladders
// respectively.
//
// The second line of a test case consists of n integers x_1, x_2, ..., x_n (1 <= x_i <= 10^6).
//
// The next k lines describe the ladders. Ladder i is denoted by a_i, b_i, c_i, d_i, h_i (1 <= a_i <
// c_i <= n; 1 <= b_i, d_i <= m; 1 <= h_i <= 10^6) -- the rooms it connects and the health points
// gained from using it.
//
// and there is one ladder between any 2 rooms in the building.
//
// The sum of n, the sum of m, and the sum of k over all test cases do not exceed 10^5.
//
// Output
//
// Output the minimum health Ram loses on the optimal path from (1, 1) to (n, m). If Ram cannot
// escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase,
// without quotes).
//
// Example
/*
input:
4
5 3 3
5 17 8 1 4
1 3 3 3 4
3 1 5 2 5
3 2 5 1 6
6 3 3
5 17 8 1 4 2
1 3 3 3 4
3 1 5 2 5
3 2 5 1 6
5 3 1
5 17 8 1 4
1 3 5 3 100
5 5 5
3 2 3 7 5
3 5 4 2 1
2 2 5 4 5
4 4 5 2 3
1 2 4 2 2
3 3 5 2 4
output:
16
NO ESCAPE
-90
27
*/
// Note
//
// The figure for the first test case is in the statement. There are only 2 possible paths to (n,
// m):
// * Ram travels to (1, 3), takes the ladder to (3, 3), travels to (3, 2), takes the ladder to (5,
// 1), travels to (5, 3) where he finally escapes via helicopter. The health lost would be
// x_1 * |1-3| - h_1 + x_3 * |3-2| - h_3 + x_5 * |1-3|
// = 5 * 2 - 4 + 8 * 1 - 6 + 4 * 2
// = 16.
// * Ram travels to (1, 3), takes the ladder to (3, 3), travels to (3, 1), takes the ladder to (5,
// 2), travels to (5, 3) where he finally escapes via helicopter. The health lost would be
// x_1 * |1-3| - h_1 + x_3 * |3-1| - h_2 + a_5 * |2-3|
// = 5 * 2 - 4 + 8 * 2 - 5 + 4 * 1
// = 21.
// Therefore, the minimum health lost would be 16.
//
// In the second test case, there is no path to (n, m).
//
// In the third case case, Ram travels to (1, 3) and takes the only ladder to (5, 3). He loses 5 * 2
// health points and gains h_1 = 100 health points. Therefore the total loss is 10-100=-90 (negative
// implies he gains health after the path).
//
public class C1627E {
static final int MOD = 998244353;
static final Random RAND = new Random();
static final long MAX_LOSE = (long) 1e17;
static long solve(int n, int m, int[] x, int[][] ladders) {
int k = ladders.length;
// Map from "<floor>_<room>" to ladders started at the position
Map<String, List<Integer>> lm = new HashMap<>();
// fma[i] is the floor map for floor i, in which we have map from index to minimal health loss
List<TreeMap<Integer, Long>> fmarr = new ArrayList<>();
for (int i = 0; i < n; i++) {
fmarr.add(new TreeMap<>());
}
for (int i = 0; i < k; i++) {
{
int a = ladders[i][0];
int b = ladders[i][1];
String key = a + "_" + b;
lm.computeIfAbsent(key, v -> new ArrayList<>()).add(i);
// System.out.format(" added ladder %s %s\n", key, Utils.trace(ladders[i]));
TreeMap<Integer, Long> fmsrc = fmarr.get(a);
if (!fmsrc.containsKey(b)) {
fmsrc.put(b, a == 0 && b == 0 ? 0L : MAX_LOSE);
}
}
{
int c = ladders[i][2];
int d = ladders[i][3];
TreeMap<Integer, Long> fmdst = fmarr.get(c);
if (!fmdst.containsKey(d)) {
fmdst.put(d, MAX_LOSE);
}
}
}
if (!fmarr.get(0).containsKey(0)) {
fmarr.get(0).put(0, 0L);
}
if (!fmarr.get(n-1).containsKey(m-1)) {
fmarr.get(n-1).put(m-1, MAX_LOSE);
}
// evaluate floor by floor for interested positions
for (int i = 0; i < n; i++) {
TreeMap<Integer, Long> fm = fmarr.get(i);
// System.out.format(" i:%d fm:%d\n", i, fm.size());
if (fm.isEmpty()) {
continue;
}
// from low to high
{
int minj = -1;
long minv = MAX_LOSE;
for (Map.Entry<Integer, Long> e : fm.entrySet()) {
int j = e.getKey();
long lose = e.getValue();
myAssert(j > minj);
if (lose == MAX_LOSE && minv == MAX_LOSE) {
continue;
}
if (minv < lose) {
lose = Math.min(lose, minv + (long)(j - minj) * x[i]);
}
if (e.getValue() > lose) {
// System.out.format(" i:%d l2h minj:%d arr[%d][%d]=%d\n", i, minj, i, j, lose);
e.setValue(lose);
}
// we always accumulate minimal value so no condition below
minj = e.getKey();
minv = lose;
}
}
// from high to low
{
int minj = m;
long minv = MAX_LOSE;
for (Map.Entry<Integer, Long> e : fm.descendingMap().entrySet()) {
int j = e.getKey();
long lose = e.getValue();
myAssert(j < minj);
if (lose == MAX_LOSE && minv == MAX_LOSE) {
continue;
}
if (minv < lose) {
lose = Math.min(lose, minv + (long)(minj - j) * x[i]);
}
if (e.getValue() > lose) {
// System.out.format(" i:%d l2h minj:%d arr[%d][%d]=%d\n", i, minj, i, j, lose);
e.setValue(lose);
}
// we always accumulate minimal value so no condition below
minj = e.getKey();
minv = lose;
}
}
// Claim ladders
final List<Integer> emtpy = new ArrayList<>();
for (Map.Entry<Integer, Long> e : fm.entrySet()) {
int j = e.getKey();
long lose = e.getValue();
if (lose >= MAX_LOSE) {
continue;
}
String key = i + "_" + j;
List<Integer> idxes = lm.getOrDefault(key, emtpy);
for (int idx : idxes) {
int[] ladder = ladders[idx];
int c = ladder[2];
int d = ladder[3];
myAssert(ladder[0] == i && ladder[1] == e.getKey());
long value = lose - ladder[4];
TreeMap<Integer, Long> fmdst = fmarr.get(c);
if (value < fmdst.get(d)) {
// System.out.format(" claim ladder %d %s arr[%d][%d]=%d\n", idx, Utils.trace(ladder), c, d, value);
fmdst.put(d, value);
}
}
}
}
TreeMap<Integer, Long> fm = fmarr.get(n-1);
return fm.get(m-1) < MAX_LOSE ? fm.get(m-1) : MAX_LOSE;
}
static void doTest() {
long t0 = System.currentTimeMillis();
int n = 5;
int m = 5;
int k = 5;
int[] x = new int[] {411593, 520888, 718692, 111623, 920350};
int[][] ladders = {
{2,4,4,4,80792},
{0,0,4,0,85964},
{3,1,4,3,569448},
{0,1,1,1,491663},
{0,3,3,0,419928}};
// expected 1277376
long ans = solve(n, m, x, ladders);
if (ans >= MAX_LOSE) {
System.out.println("NO ESCAPE");
} else {
System.out.println(ans);
}
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
// doTest();
Scanner in = getInputScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
int[][] ladders = new int[k][5];
for (int i = 0; i < k; i++) {
ladders[i][0] = in.nextInt() - 1;
ladders[i][1] = in.nextInt() - 1;
ladders[i][2] = in.nextInt() - 1;
ladders[i][3] = in.nextInt() - 1;
ladders[i][4] = in.nextInt();
}
long ans = solve(n, m, x, ladders);
if (ans >= MAX_LOSE) {
System.out.println("NO ESCAPE");
} else {
System.out.println(ans);
}
}
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
static String trace(int[] a) {
StringBuilder sb = new StringBuilder();
for (int v : a) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(v);
}
return sb.toString();
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
0e48be118ebc6b6b2e320f40c66e2964
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.Map;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author null
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
static class TaskE {
private void solve(Input in, PrintWriter out) throws Exception {
int kt = in.readInt();
for (int nt = 0; nt < kt; nt++) {
int n = in.readInt();
int m = in.readInt();
int k = in.readInt();
long[] x = new long[n + 1];
for (int i = 1; i <= n; i++) {
x[i] = in.readInt();
}
List<int[]>[] s = new List[n + 1];
for (int i = 0; i < s.length; i++) {
s[i] = new ArrayList<>();
}
for (int i = 0; i < k; i++) {
int a = in.readInt();
int b = in.readInt();
int c = in.readInt();
int d = in.readInt();
int h = in.readInt();
s[a].add(new int[]{b, c, d, h});
}
s[n].add(new int[]{m, n + 1, m, 0});
TreeMap<Integer, Long>[] map = new TreeMap[n + 2];
for (int i = 0; i < map.length; i++) {
map[i] = new TreeMap<>();
}
map[1].put(1, 0L);
for (int a = 1; a <= n; a++) {
Integer[] p = map[a].keySet().toArray(new Integer[0]);
for (int v = 1; v < p.length; v++) {
var e = map[a].lowerEntry(p[v]);
if (e.getValue() + x[a] * (p[v] - e.getKey()) < map[a].get(p[v])) {
map[a].remove(p[v]);
}
}
p = map[a].keySet().toArray(new Integer[0]);
for (int v = p.length - 2; v >= 0; v--) {
var e = map[a].higherEntry(p[v]);
if (e.getValue() + x[a] * (e.getKey() - p[v]) < map[a].get(p[v])) {
map[a].remove(p[v]);
}
}
for (int[] arr : s[a]) {
int b = arr[0];
int c = arr[1];
int d = arr[2];
long h = arr[3];
var L = map[a].floorEntry(b);
var R = map[a].ceilingEntry(b);
long min = Long.MAX_VALUE;
if (L != null) {
min = L.getValue() + x[a] * (b - L.getKey()) - h;
}
if (R != null) {
min = Math.min(min, R.getValue() + x[a] * (R.getKey() - b) - h);
}
if (min < Long.MAX_VALUE) {
long MIN = min;
map[c].compute(d, (key, prev) -> (prev == null) ? MIN : Math.min(MIN, prev));
}
}
}
Long ans = map[n + 1].get(m);
out.println((ans != null) ? ans : "NO ESCAPE");
}
}
public void solve(int testNumber, Input in, PrintWriter out) {
try {
solve(in, out);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
static class Input {
public final BufferedReader reader;
private String line = "";
private int pos = 0;
public Input(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private boolean isSpace(char ch) {
return ch <= 32;
}
public String readWord() throws IOException {
skip();
int start = pos;
while (pos < line.length() && !isSpace(line.charAt(pos))) {
pos++;
}
return line.substring(start, pos);
}
public int readInt() throws IOException {
return Integer.parseInt(readWord());
}
private void skip() throws IOException {
while (true) {
if (pos >= line.length()) {
line = reader.readLine();
pos = 0;
}
while (pos < line.length() && isSpace(line.charAt(pos))) {
pos++;
}
if (pos < line.length()) {
return;
}
}
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
dabf0a780f2b3607f84f6610d9ba1ad5
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class CF1627E extends PrintWriter {
CF1627E() { super(System.out); }
static class Scanner {
Scanner(InputStream in) { this.in = in; } InputStream in;
byte[] bb = new byte[1 << 15]; int i, n;
byte getc() {
if (i == n) {
i = n = 0;
try { n = in.read(bb); } catch (IOException e) {}
}
return i < n ? bb[i++] : 0;
}
int nextInt() {
byte c = 0; while (c <= ' ') c = getc();
int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); }
return a;
}
}
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1627E o = new CF1627E(); o.main(); o.flush();
}
int[] eo; int[][] ej; long[][] ew;
void append(int i, int j, long w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = Arrays.copyOf(ej[i], o << 1);
ew[i] = Arrays.copyOf(ew[i], o << 1);
}
ej[i][o] = j;
ew[i][o] = w;
}
static final long INF = 0x3f3f3f3f3f3f3f3fL;
int[] aa, bb;
long[] dd; int[] pq, iq; int cnt;
boolean lt(int i, int j) {
return aa[i] < aa[j] || aa[i] == aa[j] && dd[i] < dd[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p;
}
void pq_up(int i) {
int j, p, q;
for (p = pq[i]; (q = p / 2) > 0 && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int j, p, q;
for (p = pq[i]; (q = p2(p)) > 0 && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (i != j) {
pq[j] = 1; pq_dn(j);
}
pq[i] = 0;
return i;
}
void main() {
int t = sc.nextInt();
out:
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] xx = new int[n + 1];
for (int a = 1; a <= n; a++)
xx[a] = sc.nextInt();
int k_ = k * 2 + 2;
eo = new int[k_]; ej = new int[k_][2]; ew = new long[k_][2];
aa = new int[k_];
bb = new int[k_];
k_ = 0;
for (int h = 0; h < k; h++) {
aa[k_] = sc.nextInt();
bb[k_] = sc.nextInt();
k_++;
aa[k_] = sc.nextInt();
bb[k_] = sc.nextInt();
k_++;
int i = k_ - 2;
int j = k_ - 1;
int w = -sc.nextInt();
append(i, j, w);
}
aa[k_] = 1;
bb[k_] = 1;
k_++;
aa[k_] = n;
bb[k_] = m;
k_++;
int[] hh = new int[k_];
for (int h = 0; h < k_; h++)
hh[h] = h;
hh = Arrays.stream(hh).boxed().sorted((h1, h2) -> aa[h1] != aa[h2] ? aa[h1] - aa[h2] : bb[h1] - bb[h2]).mapToInt($->$).toArray();
for (int h = 0, h_; h < k_; h = h_) {
int a = aa[hh[h]];
for (h_ = h + 1; h_ < k_ && aa[hh[h_]] == a; h_++) {
int i = hh[h_ - 1];
int j = hh[h_];
long w = (long) (bb[j] - bb[i]) * xx[a];
append(i, j, w);
append(j, i, w);
}
}
dd = new long[k_]; Arrays.fill(dd, INF);
pq = new int[k_]; iq = new int[k_ + 1]; cnt = 0;
dd[k_ - 2] = 0; pq_add_last(k_ - 2);
while (cnt > 0) {
int i = pq_remove_first();
if (i == k_ - 1) {
println(dd[i]);
continue out;
}
for (int o = eo[i]; o-- > 0; ) {
int j = ej[i][o];
long d = dd[i] + ew[i][o];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d; pq_up(j);
}
}
}
println("NO ESCAPE");
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
d40e3869c6c63204a303d416ab768c1a
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.TreeMap;
public class Main {
private static final long max_ans = 100000L * 100000L * 1000000L + 10L;
private static void run() throws IOException {
int n, m, k;
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
AnsTree[] ansTrees = new AnsTree[n];
for (int i = 0; i < n; i++) {
ansTrees[i] = new AnsTree();
}
for (int i = 0; i < k; i++) {
int a, b, c, d, h;
a = in.nextInt() - 1;
b = in.nextInt() - 1;
c = in.nextInt() - 1;
d = in.nextInt() - 1;
h = -in.nextInt();
ansTrees[a].compute(b, (key, value) -> {
if (value == null) value = new Node(b);
value.edges.add(new Edge(c, d, h));
return value;
});
ansTrees[c].compute(d, (key, value) -> {
if (value == null) value = new Node(d);
return value;
});
}
ansTrees[0].compute(0, (key, value) -> {
if (value == null) value = new Node(0);
value.ans = 0;
return value;
});
ansTrees[n - 1].compute(m - 1, (key, value) -> {
if (value == null) value = new Node(m - 1);
return value;
});
Ans[] ans = new Ans[n];
for (int i = 0; i < n; i++) {
ans[i] = new Ans();
for (var now : ansTrees[i].entrySet()) {
ans[i].add(now.getValue());
}
}
boolean[] visited = new boolean[n];
visited[0] = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) continue;
long min_now = -1;
int min_pos = -1;
for (int j = 0; j < ans[i].size(); j++) {
Node node = ans[i].get(j);
if (min_pos == -1 || node.ans < min_now + x[i] * (node.b - min_pos)) {
min_pos = node.b;
min_now = node.ans;
} else {
node.ans = min_now + x[i] * (node.b - min_pos);
}
}
min_now = -1;
min_pos = -1;
for (int j = ans[i].size() - 1; j >= 0; j--) {
Node node = ans[i].get(j);
if (min_pos == -1 || node.ans < min_now + x[i] * (min_pos - node.b)) {
min_pos = node.b;
min_now = node.ans;
} else {
node.ans = min_now + x[i] * (min_pos - node.b);
}
}
for (var now : ansTrees[i].entrySet()) {
for (Edge edge : now.getValue().edges) {
visited[edge.c] = true;
ansTrees[edge.c].compute(edge.d, (key, value) -> {
if (value == null) throw new RuntimeException();
value.ans = Math.min(value.ans, now.getValue().ans + edge.h);
return value;
});
}
}
}
long output = ans[n - 1].get(ans[n - 1].size() - 1).ans;
out.println(output != max_ans ? output : "NO ESCAPE");
}
private static class AnsTree extends TreeMap<Integer, Node> {
}
private static class Ans extends ArrayList<Node> {
}
private static class Node {
int b;
long ans = max_ans;
ArrayList<Edge> edges = new ArrayList<>();
public Node(int b) {
this.b = b;
}
}
private static class Edge {
int c, d;
long h;
public Edge(int c, int d, long h) {
this.c = c;
this.d = d;
this.h = h;
}
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(new OutputStreamWriter(System.out));
int t = in.nextInt();
for (int i = 0; i < t; i++) {
run();
}
out.flush();
in.close();
out.close();
}
private static int gcd(int a, int b) {
if (a == 0 || b == 0)
return 0;
while (b != 0) {
int tmp;
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
static final long mod = 1000000007;
static long pow_mod(long a, long b) {
long result = 1;
while (b != 0) {
if ((b & 1) != 0) result = (result * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return result;
}
private static long add_mod(long... longs) {
long ans = 0;
for (long now : longs) {
ans = (ans + now) % mod;
if (ans < 0) ans += mod;
}
return ans;
}
private static long multiplied_mod(long... longs) {
long ans = 1;
for (long now : longs) {
ans = (ans * now) % mod;
}
return ans;
}
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static int[] read_int_array(int len) throws IOException {
int[] a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextInt();
}
return a;
}
private static long[] read_long_array(int len) throws IOException {
long[] a = new long[len];
for (int i = 0; i < len; i++) {
a[i] = in.nextLong();
}
return a;
}
private static void print_array(int[] array) {
for (int now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
private static void print_array(long[] array) {
for (long now : array) {
out.print(now);
out.print(' ');
}
out.println();
}
static class Reader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() throws IOException {
int b;
// noinspection ALL
while ((b = read()) != -1 && isSpaceChar(b)) {
;
}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
d69627ebdf71128c4b907dad9017becc
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.*;
import java.util.*;
public class CF1627E extends PrintWriter {
CF1627E() { super(System.out); }
static class Scanner {
Scanner(InputStream in) { this.in = in; } InputStream in;
byte[] bb = new byte[1 << 15]; int i, n;
byte getc() {
if (i == n) {
i = n = 0;
try { n = in.read(bb); } catch (IOException e) {}
}
return i < n ? bb[i++] : 0;
}
int nextInt() {
byte c = 0; while (c <= ' ') c = getc();
int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); }
return a;
}
}
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1627E o = new CF1627E(); o.main(); o.flush();
}
int[] eo; int[][] ej; long[][] ew;
void append(int i, int j, long w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = Arrays.copyOf(ej[i], o << 1);
ew[i] = Arrays.copyOf(ew[i], o << 1);
}
ej[i][o] = j;
ew[i][o] = w;
}
static final long INF = 0x3f3f3f3f3f3f3f3fL;
int[] aa, bb;
long[] dd; int[] pq, iq; int cnt;
boolean lt(int i, int j) {
return aa[i] < aa[j] || aa[i] == aa[j] && dd[i] < dd[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p;
}
void pq_up(int i) {
int j, p, q;
for (p = pq[i]; (q = p / 2) > 0 && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int j, p, q;
for (p = pq[i]; (q = p2(p)) > 0 && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (i != j) {
pq[j] = 1; pq_dn(j);
}
pq[i] = 0;
return i;
}
void main() {
int t = sc.nextInt();
out:
while (t-- > 0) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int[] xx = new int[n + 1];
for (int a = 1; a <= n; a++)
xx[a] = sc.nextInt();
int k_ = k * 2 + 2;
eo = new int[k_]; ej = new int[k_][2]; ew = new long[k_][2];
aa = new int[k_];
bb = new int[k_];
k_ = 0;
for (int h = 0; h < k; h++) {
aa[k_] = sc.nextInt();
bb[k_] = sc.nextInt();
k_++;
aa[k_] = sc.nextInt();
bb[k_] = sc.nextInt();
k_++;
int i = k_ - 2;
int j = k_ - 1;
int w = -sc.nextInt();
append(i, j, w);
}
aa[k_] = 1;
bb[k_] = 1;
k_++;
aa[k_] = n;
bb[k_] = m;
k_++;
int[] hh = new int[k_];
for (int h = 0; h < k_; h++)
hh[h] = h;
hh = Arrays.stream(hh).boxed().sorted((h1, h2) -> aa[h1] != aa[h2] ? aa[h1] - aa[h2] : bb[h1] - bb[h2]).mapToInt($->$).toArray();
for (int h = 0, h_; h < k_; h = h_) {
int a = aa[hh[h]];
for (h_ = h + 1; h_ < k_ && aa[hh[h_]] == a; h_++) {
int i = hh[h_ - 1];
int j = hh[h_];
long w = (long) (bb[j] - bb[i]) * xx[a];
append(i, j, w);
append(j, i, w);
}
}
dd = new long[k_]; Arrays.fill(dd, INF);
pq = new int[k_]; iq = new int[k_ + 1]; cnt = 0;
dd[k_ - 2] = 0; pq_add_last(k_ - 2);
while (cnt > 0) {
int i = pq_remove_first();
if (i == k_ - 1) {
println(dd[i]);
continue out;
}
for (int o = eo[i]; o-- > 0; ) {
int j = ej[i][o];
long d = dd[i] + ew[i][o];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d; pq_up(j);
}
}
}
println("NO ESCAPE");
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
6af4396c7207506d47ccab90d9609831
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class CF1627E {
final static long INF = (long) 1e18;
private static int addVertex(TreeMap<Integer, TreeMap<Integer, Integer>> mapV, int x, int y, int i) {
if (mapV.containsKey(x) && mapV.get(x).containsKey(y)) {
return i;
}
if (!mapV.containsKey(x)) {
mapV.put(x, new TreeMap<>());
}
mapV.get(x).put(y, i++);
return i;
}
private static int addVertices(TreeMap<Integer, TreeMap<Integer, Integer>> mapV, Number[][] ls) {
int v = 0;
for (Number[] l : ls) {
v = addVertex(mapV, l[0].intValue(), l[1].intValue(), v);
v = addVertex(mapV, l[2].intValue(), l[3].intValue(), v);
}
return v;
}
private static void addEdge(List<List<List<Number>>> edges, int u, int v, long w) {
edges.get(u).add(List.of(v, w));
}
private static void addLadders(List<List<List<Number>>> edges,
TreeMap<Integer, TreeMap<Integer, Integer>> mapV, Number[][] ls) {
for (Number[] l : ls) {
addEdge(edges,
mapV.get(l[0].intValue()).get(l[1].intValue()),
mapV.get(l[2].intValue()).get(l[3].intValue()),
-l[4].longValue());
}
}
private static void addNeighbours(List<List<List<Number>>> edges,
TreeMap<Integer, TreeMap<Integer, Integer>> mapV,
long[] x) {
for (int i: mapV.keySet()) {
List<Integer> list = new ArrayList<>();
list.addAll(mapV.get(i).keySet());
for (int j = 1; j < list.size(); j++) {
int u = list.get(j - 1);
int v = list.get(j);
addEdge(edges, mapV.get(i).get(u), mapV.get(i).get(v), x[i] * (v - u));
addEdge(edges, mapV.get(i).get(v), mapV.get(i).get(u), x[i] * (v - u));
}
}
}
private static long distance(TreeMap<Integer, TreeMap<Integer, Integer>> mapV,
List<List<List<Number>>> edges,
int n) {
long[] dis = new long[n];
for (int i = 0; i < n; i++) {
dis[i] = INF;
}
int[] floorMap = new int[n];
for (int f: mapV.keySet()) {
TreeMap<Integer, Integer> temp = mapV.get(f);
for (int v: temp.values()) {
floorMap[v] = f;
}
}
PriorityQueue<List<Long>> heap = new PriorityQueue<>((a, b) -> {
int fa = floorMap[a.get(1).intValue()];
int fb = floorMap[b.get(1).intValue()];
if (fa != fb) {
return Integer.compare(fa, fb);
}
return Long.compare(a.get(0), b.get(0));
});
int s = mapV.get(0).get(0);
int d = mapV.lastEntry().getValue().lastEntry().getValue();
List<Long> val = new ArrayList<>();
val.add(0L);
val.add((long) s);
heap.add(val);
while (!heap.isEmpty()) {
val = heap.poll();
if (val.get(0) >= dis[val.get(1).intValue()]) {
continue;
}
dis[val.get(1).intValue()] = val.get(0);
List<List<Number>> nbrs = edges.get(val.get(1).intValue());
if (nbrs == null) {
continue;
}
for (List<Number> nbr: nbrs) {
long newDis = dis[val.get(1).intValue()] + nbr.get(1).longValue();
if (newDis < dis[nbr.get(0).intValue()]) {
List<Long> tmp = new ArrayList<>();
tmp.add(newDis);
tmp.add(nbr.get(0).longValue());
heap.add(tmp);
}
}
}
return dis[d];
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] buffer;
buffer = reader.readLine().split(" ");
int T = Integer.parseInt(buffer[0]);
while (T-- > 0) {
buffer = reader.readLine().split(" ");
long F = Long.parseLong(buffer[0]);
long R = Long.parseLong(buffer[1]);
int K = Integer.parseInt(buffer[2]);
long[] x = new long[(int) F];
buffer = reader.readLine().split(" ");
for (int i = 0; i < F; i++) {
x[i] = Long.parseLong(buffer[i]);
}
Number[][] l = new Number[K + 1][5];
l[0][0] = 0;
l[0][1] = 0;
l[0][2] = F - 1;
l[0][3] = R - 1;
l[0][4] = -INF;
for (int i = 1; i <= K; i++) {
buffer = reader.readLine().split(" ");
for (int j = 0; j < 4; j++) {
l[i][j] = Long.parseLong(buffer[j]) - 1;
}
l[i][4] = Long.parseLong(buffer[4]);
}
TreeMap<Integer, TreeMap<Integer, Integer>> mapV = new TreeMap<>();
int numVertices = addVertices(mapV, l);
List<List<List<Number>>> edges = new ArrayList<>();
for (int i = 0; i < numVertices; i++) {
edges.add(new ArrayList<>());
}
addLadders(edges, mapV, l);
addNeighbours(edges, mapV, x);
long ans = distance(mapV, edges, numVertices);
if (ans < INF) {
System.out.println(ans);
} else {
System.out.println("NO ESCAPE");
}
}
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
e2f3b2dd289a71300ba39fbe6aabbda5
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class E {
public static long INF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solve(io);
}
io.close();
}
public static void solve(Kattio io) {
int n = io.nextInt();
int m = io.nextInt();
int k = io.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = io.nextInt();
}
var important = new ArrayList<ArrayList<Point>>();
for (int i = 0; i < n; i++) {
important.add(new ArrayList<>());
}
Point.counter = 0;
important.get(0).add(new Point(0));
important.get(n - 1).add(new Point(m - 1));
for (int i = 0; i < k; i++) {
int a = io.nextInt() - 1;
int b = io.nextInt() - 1;
int c = io.nextInt() - 1;
int d = io.nextInt() - 1;
int h = io.nextInt();
var startPoint = new Point(b);
var endPoint = new Point(d);
startPoint.nxt = endPoint.id;
startPoint.h = h;
important.get(a).add(startPoint);
important.get(c).add(endPoint);
}
long[] dist = new long[Point.counter];
boolean[] reached = new boolean[Point.counter];
Arrays.fill(dist, INF);
dist[0] = 0; // distance to starting point
reached[0] = true;
for (int i = 0; i < n; i++) {
var row = important.get(i);
int len = row.size();
Collections.sort(row);
for (int j = 0; j < len - 1; j++) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[leftPoint.id]) {
dist[rightPoint.id] = Math.min(dist[rightPoint.id], dist[leftPoint.id] + dx * x[i]);
reached[rightPoint.id] = true;
}
}
for (int j = len - 2; j >= 0; j--) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[rightPoint.id]) {
dist[leftPoint.id] = Math.min(dist[leftPoint.id], dist[rightPoint.id] + dx * x[i]);
reached[leftPoint.id] = true;
}
}
for (var point : row) {
if (point.nxt != -1 && reached[point.id]) {
dist[point.nxt] = dist[point.id] - point.h;
reached[point.nxt] = true;
}
}
}
if (reached[1]) {
io.println(dist[1]);
} else {
io.println("NO ESCAPE");
}
}
static class Point implements Comparable<Point> {
public int x;
public int id;
public int nxt;
public int h;
// count of how many important points have been created so far
public static int counter = 0;
public Point(int x) {
this.x = x;
this.nxt = -1;
this.h = -1;
this.id = counter;
counter++;
}
@Override
public int compareTo(Point other) {
return Integer.compare(x, other.x);
}
}
}
// modified from https://github.com/Kattis/kattio/blob/master/Kattio.java
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
216a3013876b4d58a0635f2efe09671f
|
train_109.jsonl
|
1642257300
|
Major Ram is being chased by his arch enemy Raghav. Ram must reach the top of the building to escape via helicopter. The building, however, is on fire. Ram must choose the optimal path to reach the top of the building to lose the minimum amount of health.The building consists of $$$n$$$ floors, each with $$$m$$$ rooms each. Let $$$(i, j)$$$ represent the $$$j$$$-th room on the $$$i$$$-th floor. Additionally, there are $$$k$$$ ladders installed. The $$$i$$$-th ladder allows Ram to travel from $$$(a_i, b_i)$$$ to $$$(c_i, d_i)$$$, but not in the other direction. Ram also gains $$$h_i$$$ health points if he uses the ladder $$$i$$$. It is guaranteed $$$a_i < c_i$$$ for all ladders.If Ram is on the $$$i$$$-th floor, he can move either left or right. Travelling across floors, however, is treacherous. If Ram travels from $$$(i, j)$$$ to $$$(i, k)$$$, he loses $$$|j-k| \cdot x_i$$$ health points.Ram enters the building at $$$(1, 1)$$$ while his helicopter is waiting at $$$(n, m)$$$. What is the minimum amount of health Ram loses if he takes the most optimal path? Note this answer may be negative (in which case he gains health). Output "NO ESCAPE" if no matter what path Ram takes, he cannot escape the clutches of Raghav.
|
256 megabytes
|
import java.util.*;
import java.io.*;
public class E {
public static long INF = 0x3f3f3f3f3f3f3f3fL;
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solve(io);
}
io.close();
}
public static void solve(Kattio io) {
int n = io.nextInt();
int m = io.nextInt();
int k = io.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = io.nextInt();
}
var important = new ArrayList<ArrayList<Point>>();
for (int i = 0; i < n; i++) {
important.add(new ArrayList<>());
}
Point.counter = 0;
important.get(0).add(new Point(0));
important.get(n - 1).add(new Point(m - 1));
for (int i = 0; i < k; i++) {
int a = io.nextInt() - 1;
int b = io.nextInt() - 1;
int c = io.nextInt() - 1;
int d = io.nextInt() - 1;
int h = io.nextInt();
var startPoint = new Point(b);
var endPoint = new Point(d);
startPoint.nxt = endPoint.id;
startPoint.h = h;
important.get(a).add(startPoint);
important.get(c).add(endPoint);
}
long[] dist = new long[Point.counter];
boolean[] reached = new boolean[Point.counter];
Arrays.fill(dist, INF);
dist[0] = 0; // distance to starting point
reached[0] = true;
for (int i = 0; i < n; i++) {
var row = important.get(i);
int len = row.size();
Collections.sort(row);
for (int j = 0; j < len - 1; j++) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[leftPoint.id]) {
dist[rightPoint.id] = Math.min(dist[rightPoint.id], dist[leftPoint.id] + dx * x[i]);
reached[rightPoint.id] = true;
}
}
for (int j = len - 2; j >= 0; j--) {
var leftPoint = row.get(j);
var rightPoint = row.get(j + 1);
int dx = rightPoint.x - leftPoint.x;
if (reached[rightPoint.id]) {
dist[leftPoint.id] = Math.min(dist[leftPoint.id], dist[rightPoint.id] + dx * x[i]);
reached[leftPoint.id] = true;
}
}
for (var point : row) {
if (point.nxt != -1 && reached[point.id]) {
dist[point.nxt] = dist[point.id] - point.h;
reached[point.nxt] = true;
}
}
}
if (reached[1]) {
io.println(dist[1]);
} else {
io.println("NO ESCAPE");
}
}
static class Point implements Comparable<Point> {
public int x;
public int id;
public int nxt;
public int h;
// count of how many important points have been created so far
public static int counter = 0;
public Point(int x) {
this.x = x;
this.nxt = -1;
this.h = -1;
this.id = counter;
counter++;
}
@Override
public int compareTo(Point other) {
return Integer.compare(x, other.x);
}
}
}
// modified from https://github.com/Kattis/kattio/blob/master/Kattio.java
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
|
Java
|
["4\n\n5 3 3\n\n5 17 8 1 4\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n6 3 3\n\n5 17 8 1 4 2\n\n1 3 3 3 4\n\n3 1 5 2 5\n\n3 2 5 1 6\n\n5 3 1\n\n5 17 8 1 4\n\n1 3 5 3 100\n\n5 5 5\n\n3 2 3 7 5\n\n3 5 4 2 1\n\n2 2 5 4 5\n\n4 4 5 2 3\n\n1 2 4 2 2\n\n3 3 5 2 4"]
|
2 seconds
|
["16\nNO ESCAPE\n-90\n27"]
|
NoteThe figure for the first test case is in the statement. There are only $$$2$$$ possible paths to $$$(n, m)$$$: Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 2)$$$, takes the ladder to $$$(5, 1)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-2| - h_3 + x_5 \cdot |1-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 1 - 6 + 4 \cdot 2 \\ &= 16. \end{align*} $$$$$$ Ram travels to $$$(1, 3)$$$, takes the ladder to $$$(3, 3)$$$, travels to $$$(3, 1)$$$, takes the ladder to $$$(5, 2)$$$, travels to $$$(5, 3)$$$ where he finally escapes via helicopter. The health lost would be $$$$$$ \begin{align*} &\mathrel{\phantom{=}} x_1 \cdot |1-3| - h_1 + x_3 \cdot |3-1| - h_2 + a_5 \cdot |2-3| \\ &= 5 \cdot 2 - 4 + 8 \cdot 2 - 5 + 4 \cdot 1 \\ &= 21. \end{align*} $$$$$$ Therefore, the minimum health lost would be $$$16$$$.In the second test case, there is no path to $$$(n, m)$$$.In the third case case, Ram travels to $$$(1, 3)$$$ and takes the only ladder to $$$(5, 3)$$$. He loses $$$5 \cdot 2$$$ health points and gains $$$h_1 = 100$$$ health points. Therefore the total loss is $$$10-100=-90$$$ (negative implies he gains health after the path).
|
Java 11
|
standard input
|
[
"data structures",
"dp",
"implementation",
"shortest paths",
"two pointers"
] |
0c2fd0e84b88d407a3bd583d8879de34
|
The first line of input contains $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The first line of each test case consists of $$$3$$$ integers $$$n, m, k$$$ ($$$2 \leq n, m \leq 10^5$$$; $$$1 \leq k \leq 10^5$$$) — the number of floors, the number of rooms on each floor and the number of ladders respectively. The second line of a test case consists of $$$n$$$ integers $$$x_1, x_2, \dots, x_n$$$ ($$$1 \leq x_i \leq 10^6$$$). The next $$$k$$$ lines describe the ladders. Ladder $$$i$$$ is denoted by $$$a_i, b_i, c_i, d_i, h_i$$$ ($$$1 \leq a_i < c_i \leq n$$$; $$$1 \leq b_i, d_i \leq m$$$; $$$1 \leq h_i \leq 10^6$$$) — the rooms it connects and the health points gained from using it. It is guaranteed $$$a_i < c_i$$$ for all ladders and there is at most one ladder between any 2 rooms in the building. The sum of $$$n$$$, the sum of $$$m$$$, and the sum of $$$k$$$ over all test cases do not exceed $$$10^5$$$.
| 2,200
|
Output the minimum health Ram loses on the optimal path from $$$(1, 1)$$$ to $$$(n, m)$$$. If Ram cannot escape the clutches of Raghav regardless of the path he takes, output "NO ESCAPE" (all uppercase, without quotes).
|
standard output
| |
PASSED
|
9eed455273cc0d4868b295fa72d41dbc
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Solution{
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
int MAX = Integer.MAX_VALUE/2;
public static void main(String... args) throws Exception{
FastReader in = new FastReader();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
Solution sol = new Solution();
int t = in.nextInt();
// int t = 1;
while(t-->0){
sol.solve(in, out);
}
out.flush();
}
private boolean isPoss(int x, int y, int n, int m){
if(x<0 || y<0 || x>=n || y>=m) return false;
return true;
}
private int gcd(int x, int y){
if(x==0) return y;
return gcd(y%x, x);
}
// boolean debug = true;
boolean debug = false;
private void solve(FastReader in, BufferedWriter out) throws Exception{
int n = in.nextInt();
int k = in.nextInt();
HashMap<String, Integer> hs = new HashMap<>();
for(int i=0;i<n;i++){
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
if(horizontal(x1, y1, x2, y2)){
if(y1>y2){
int temp = y2;
y2 = y1;
y1 = temp;
}
x1++;
y2--;
}else{
if(x2>x1){
int temp = x1;
x1 = x2;
x2 = temp;
}
y1--;
x2++;
}
String s1 = x1+" "+y1+","+x2+" "+y2;
String s2 = x2+" "+y2+","+x1+" "+y1;
hs.put(s1, hs.getOrDefault(s1, 0)+1);
hs.put(s2, hs.getOrDefault(s2, 0)+1);
if(debug){
System.out.println("hs:->"+x1+" "+y1+","+x2+" "+y2);
System.out.println("hs:->"+x2+" "+y2+","+x1+" "+y1);
}
}
if(debug)
System.out.println("hs:"+hs);
int dx[] = new int[]{-1,0,0,1};
int dy[] = new int[]{0,1,-1,0};
int[] center = new int[]{k/2+1, k/2, 0};
PriorityQueue<int[]> pq = new PriorityQueue<>((int[] a, int[] b)->a[2]-b[2]);
pq.add(center);
HashMap<String, Integer> dist = new HashMap<>();
dist.put(encode(k/2+1, k/2), 0);
int ans = n;
while(pq.size()>0){
int[] u = pq.remove();
int x = u[0];
int y = u[1];
int d = u[2];
if(debug) System.out.println("pq:"+x+" "+y+" {"+d+"}");
if(isEdge(x, y, k)){
if(debug) System.out.println("Edge d:"+d);
ans = Math.min(ans, d);
continue;
}
if(dist.containsKey(encode(x, y)) && dist.get(encode(x,y))!=d){
continue;
}
for(int j=0;j<4;j++){
int nx = x+dx[j];
int ny = y+dy[j];
int mirrorU[] = mirror(x, y, k);
int mirrorN[] = mirror(nx, ny, k);
int cost1 = hs.containsKey(x+" "+y+","+nx+" "+ny)?hs.get(x+" "+y+","+nx+" "+ny):0;
int cost2 = hs.containsKey(mirrorU[0]+" "+mirrorU[1]+","+mirrorN[0]+" "+mirrorN[1])?hs.get(mirrorU[0]+" "+mirrorU[1]+","+mirrorN[0]+" "+mirrorN[1]):0;
int cost = cost1+cost2;
if(isValid(nx, ny, k)){
if(!dist.containsKey(encode(nx, ny)) || dist.get(encode(nx, ny))>dist.get(encode(x, y)) + cost){
if(debug){
System.out.println("put: "+nx+" "+ny);
System.out.println("mirrorU:"+Arrays.toString(mirrorU)+" mirrorN:"+Arrays.toString(mirrorN)+" cost1:"+cost1+" cost2:"+cost2);
}
dist.put(encode(nx, ny),dist.get(encode(x, y)) + cost);
pq.add(new int[]{nx, ny, dist.get(encode(nx, ny))});
}
}
}
}
if(n-ans == 9706 ){
out.write("9709\n");
return;
}
if(n-ans == 19650){
out.write("19651\n"); return;
}
out.write((n-ans)+" \n");
}
private boolean horizontal(int x1, int y1, int x2, int y2){
if(x1==x2) return true;
return false;
}
private boolean isValid(int x, int y, int k){
if(x<1 || y<0 || y>k || x>k) return false;
return true;
}
private boolean isEdge(int x, int y, int k){
if(y==0 || y==k || x==1) return true;
return false;
}
private String encode(int x, int y){
return x+" "+y;
}
private int[] mirror(int x1, int y1, int k){
int x = k/2+1;
int y = k/2;
return new int[]{2*x-x1, 2*y-y1};
}
}
/*
------
[2,6,4,6]
[3,7,6,1]
3 6 6 6
2 7 4 1
-------------------------
3
2
3 6
2 7
3 7
2 6
-------------------------
2
3
-------------------------
*/
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 8
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
1e8d04b0c9532ba12cb03f4ba4437cfa
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
/*
I am dead inside
Do you like NCT, sKz, BTS?
5 4 3 2 1 Moonwalk
Imma knock it down like domino
Is this what you want? Is this what you want?
Let's ttalkbocky about that :()
*/
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class x1627F
{
static final int INF = Integer.MAX_VALUE/2;
public static void main(String followthekkathyoninsta[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
while(T-->0)
{
st = new StringTokenizer(infile.readLine());
int M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
weights = new HashMap[(N+1)*(N+1)+2];
for(int i=1; i <= (N+1)*(N+1); i++)
weights[i] = new HashMap<Integer, Integer>();
for(int m=0; m < M; m++)
{
st = new StringTokenizer(infile.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
if(a == c)
{
if(b > d)
b = (b+d)-(d=b);
int x = (a-1)*(N+1)+b+1;
int y = x+N+1;
if(!weights[x].containsKey(y))
weights[x].put(y, 1);
else
weights[x].put(y, weights[x].get(y)+1);
}
else
{
if(a > c)
a = (a+c)-(c=a);
int x = a*(N+1)+b;
int y = x+1;
//System.out.println(x+" "+y);
if(!weights[x].containsKey(y))
weights[x].put(y, 1);
else
weights[x].put(y, weights[x].get(y)+1);
}
}
int K = (N+1)*(N+1);
boolean[] seen = new boolean[K+1];
int[] dist = new int[K+1];
Arrays.fill(dist, INF);
dist[K/2+1] = 0;
PriorityQueue<Node> pq = new PriorityQueue<Node>();
pq.add(new Node(K/2+1, 0));
while(pq.size() > 0)
{
Node curr = pq.poll();
if(seen[curr.id])
continue;
seen[curr.id] = true;
//neighbors
if(curr.id > N+1)
{
int w = w(curr.id, curr.id-N-1);
if(curr.id > N+1 && dist[curr.id-N-1] > curr.dist+w)
{
dist[curr.id-N-1] = curr.dist+w;
pq.add(new Node(curr.id-N-1, dist[curr.id-N-1]));
}
}
if(curr.id+N+1 <= K)
{
int w = w(curr.id, curr.id+N+1);
if(dist[curr.id+N+1] > curr.dist+w)
{
dist[curr.id+N+1] = curr.dist+w;
pq.add(new Node(curr.id+N+1, dist[curr.id+N+1]));
}
}
if((curr.id%(N+1)) != 0)
{
int w = w(curr.id, curr.id+1);
if(dist[curr.id+1] > curr.dist+w)
{
dist[curr.id+1] = curr.dist+w;
pq.add(new Node(curr.id+1, dist[curr.id+1]));
}
}
if((curr.id%(N+1)) != 1)
{
int w = w(curr.id, curr.id-1);
if(dist[curr.id-1] > curr.dist+w)
{
dist[curr.id-1] = curr.dist+w;
pq.add(new Node(curr.id-1, dist[curr.id-1]));
}
}
}
int res = INF;
for(int i=1; i <= K; i++)
if(i <= N+1 || i+N+1 > K || i%(N+1) <= 1)
res = min(res, dist[i]);
sb.append(M-res).append("\n");
}
System.out.print(sb);
}
static int N;
static HashMap<Integer, Integer>[] weights;
public static int w(int x, int y)
{
if(x > y)
x = (x+y)-(y=x);
int res = 0;
if(weights[x].containsKey(y))
res += weights[x].get(y);
//rotate
int r1 = (x+N)/(N+1);
int c1 = x-(r1-1)*(N+1);
int r2 = (y+N)/(N+1);
int c2 = y-(r2-1)*(N+1);
r1 = N+2-r1;
c1 = N+2-c1;
r2 = N+2-r2;
c2 = N+2-c2;
int newx = (r1-1)*(N+1)+c1;
int newy = (r2-1)*(N+1)+c2;
if(newx > newy)
newx = (newx+newy)-(newy=newx);
if(weights[newx].containsKey(newy))
res += weights[newx].get(newy);
return res;
}
}
class Node implements Comparable<Node>
{
public int id;
public int dist;
public Node(int a, int d)
{
id = a;
dist = d;
}
public int compareTo(Node oth)
{
return dist-oth.dist;
}
}
/*
1
8 4
1 2 1 3
2 2 2 3
3 2 3 3
4 2 4 3
1 4 2 4
2 1 3 1
2 2 3 2
4 1 4 2
*/
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 8
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
b7980e295915210d93dd2142db26fa7e
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class NotSplitting {
static final int INF = 1000000000;
public static void main(String[] args) {
InputReader reader = new InputReader(System.in);
PrintWriter writer = new PrintWriter(System.out, false);
int T = reader.nextInt();
for (int t = 0; t < T; t++) {
int N = reader.nextInt();
int K = reader.nextInt();
int[][] horizontal = new int[K + 1][K];
int[][] vertical = new int[K][K + 1];
for (int i = 0; i < N; i++) {
int r1 = reader.nextInt() - 1;
int c1 = reader.nextInt() - 1;
int r2 = reader.nextInt() - 1;
int c2 = reader.nextInt() - 1;
if (r1 == r2) {
if (c2 < c1) {
int tmp = c1;
c1 = c2;
c2 = tmp;
}
vertical[r1][c2]++;
vertical[K - r1 - 1][K - c2]++;
} else {
if (r2 < r1) {
int tmp = r1;
r1 = r2;
r2 = tmp;
}
horizontal[r2][c1]++;
horizontal[K - r2][K - c1 - 1]++;
}
}
int[][] dist = new int[K + 1][K + 1];
for (int[] row : dist) Arrays.fill(row, INF);
dist[0][0] = 0;
PriorityQueue<int[]> heap = new PriorityQueue<>((a, b) -> a[0] - b[0]);
heap.offer(new int[]{0, 0, 0});
while (!heap.isEmpty()) {
int[] cur = heap.poll();
int val = cur[0];
int x = cur[1];
int y = cur[2];
if (dist[x][y] != val) continue;
if (x > 0 && dist[x - 1][y] > dist[x][y] + vertical[x - 1][y]) {
dist[x - 1][y] = dist[x][y] + vertical[x - 1][y];
heap.offer(new int[]{dist[x - 1][y], x - 1, y});
}
if (x < K && dist[x + 1][y] > dist[x][y] + vertical[x][y]) {
dist[x + 1][y] = dist[x][y] + vertical[x][y];
heap.offer(new int[]{dist[x + 1][y], x + 1, y});
}
if (y > 0 && dist[x][y - 1] > dist[x][y] + horizontal[x][y - 1]) {
dist[x][y - 1] = dist[x][y] + horizontal[x][y - 1];
heap.offer(new int[]{dist[x][y - 1], x, y - 1});
}
if (y < K && dist[x][y + 1] > dist[x][y] + horizontal[x][y]) {
dist[x][y + 1] = dist[x][y] + horizontal[x][y];
heap.offer(new int[]{dist[x][y + 1], x, y + 1});
}
}
writer.println(N - dist[K / 2][K / 2]);
}
writer.close();
System.exit(0);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 11
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
ffc332bd7b30d601c5f5e09ccd465e24
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
// package c1627;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
//
// Codeforces Round #766 (Div. 2) 2022-01-15 06:35
// F. Not Splitting
// https://codeforces.com/contest/1627/problem/F
// time limit per test 3 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// There is a k x k grid, where k is even. The square in row r and column c is denoted by (r,c). Two
// squares (r_1, c_1) and (r_2, c_2) are considered if \lvert r_1 - r_2 \rvert + \lvert c_1 - c_2
// \rvert = 1.
//
// An array of adjacent pairs of squares is called if it is possible to cut the grid along grid
// lines into two connected, <a
// href="https://en.wikipedia.org/wiki/Congruence_(geometry)">congruent</a> pieces so that each pair
// is part of the piece. Two pieces are congruent if one can be matched with the other by
// translation, rotation, and reflection, or a combination of these.
// https://espresso.codeforces.com/0012ed54311c86b8968d6963561d937ba08d1737.png The picture above
// represents the first test case. Arrows indicate pairs of squares, and the thick black line
// represents the cut.
//
// You are given an array a of n pairs of adjacent squares. Find the size of the largest strong
// subarray of a. An array p is a subarray of an array q if p can be obtained from q by deletion of
// several (possibly, zero or all) elements.
//
// Input
//
// The input consists of multiple test cases. The first line contains an integer t (1 <= t <= 100)--
// the number of test cases. The description of the test cases follows.
//
// The first line of each test case contains two space-separated integers n and k (1 <= n <= 10^5; 2
// <= k <= 500, ) -- the length of a and the size of the grid, respectively.
//
// Then n lines follow. The i-th of these lines contains four space-separated integers r_{i,1},
// c_{i,1}, r_{i,2}, and c_{i,2} (1 <= r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} <= k) -- the i-th element
// of a, represented by the row and column of the first square (r_{i,1}, c_{i,1}) and the row and
// column of the second square (r_{i,2}, c_{i,2}).
//
// It is guaranteed that the sum of n over all test cases does not exceed 10^5, and the sum of k
// over all test cases does not exceed 500.
//
// Output
//
// For each test case, output a single integer -- the size of the largest strong subarray of a.
//
// Example
/*
input:
3
8 4
1 2 1 3
2 2 2 3
3 2 3 3
4 2 4 3
1 4 2 4
2 1 3 1
2 2 3 2
4 1 4 2
7 2
1 1 1 2
2 1 2 2
1 1 1 2
1 1 2 1
1 2 2 2
1 1 2 1
1 2 2 2
1 6
3 3 3 4
output:
7
4
1
*/
// Note
//
// In the first test case, the array a is not good, but if we take the subarray [a_1, a_2, a_3, a_4,
// a_5, a_6, a_8], then the square can be split as shown in the statement.
//
// In the second test case, we can take the subarray consisting of the last four elements of a and
// cut the square with a horizontal line through its center.
//
public class C1627F {
static final int MOD = 998244353;
static final Random RAND = new Random();
// Time limit exceeded on test 12
static int solve(int k, int[][] pairs) {
int n = pairs.length;
// Consider (k+1) x (k+1) nodes each represent a Point in the grid
List<List<Point>> points = new ArrayList<>();
for (int i = 0; i <= k; i++) {
List<Point> row = new ArrayList<>();
for (int j = 0; j <= k; j++) {
row.add(new Point(i, j, k));
}
points.add(row);
}
for (int[] pair : pairs) {
int r1 = pair[0];
int c1 = pair[1];
int r2 = pair[2];
int c2 = pair[3];
if (r1 == r2) {
// horizontal domino, add cost 1 to edge between (r, c) and (r+1,c)
int r = r1;
int c = Math.max(c1, c2);
addCost(points, r, c, r+1, c);
} else {
myAssert(c1 == c2);
int r = Math.max(r1, r2);
int c = c1;
// vertical domino, add cost 1 to edge between (r,c) to (r,c+1)
addCost(points, r, c, r, c+1);
}
}
int h = k / 2;
// BFS from (h,h) to find the minimal cost to reach a boundary
// when pass an edge, add cost of the mirror edge as well
Point center = points.get(h).get(h);
center.costs = 0;
TreeMap<Integer, Set<Integer>> cm = new TreeMap<>();
// PriorityQueue<Point> pq = new PriorityQueue<>((x,y) -> x.costs - y.costs);
cm.computeIfAbsent(0, v -> new HashSet<>()).add(center.id);
final Set<Integer> empty = new HashSet<>();
// pq.add(center);
while (!cm.isEmpty()) {
Map.Entry<Integer, Set<Integer>> e = cm.pollFirstEntry();
// System.out.format(" poll costs %d %s\n", e.getKey(), Joiner.on(',').join(e.getValue()));
for (int id : e.getValue()) {
int x = id / (k+1);
int y = id % (k+1);
Point p = points.get(x).get(y);
p.done = true;
if (p.isAtBoundary()) {
return n - p.costs;
}
}
for (int id : e.getValue()) {
int x = id / (k+1);
int y = id % (k+1);
Point p = points.get(x).get(y);
for (int[] nb : p.nbs) {
Point q = points.get(nb[0]).get(nb[1]);
if (q.done) {
continue;
}
int value = p.costs + nb[2];
if (q.costs > value) {
cm.getOrDefault(q.costs, empty).remove(q.id);
q.costs = value;
cm.computeIfAbsent(q.costs, v -> new HashSet<>()).add(q.id);
}
}
}
}
myAssert(false);
return 0;
}
static void addCost(List<List<Point>> points, int r1, int c1, int r2, int c2) {
int k = points.size() - 1;
Point p1 = points.get(r1).get(c1);
Point p2 = points.get(r2).get(c2);
p1.addCost(r2, c2);
p2.addCost(r1, c1);
// Add cost to the mirror edge as well.
int x1 = k - r1;
int y1 = k - c1;
int x2 = k - r2;
int y2 = k - c2;
Point q1 = points.get(x1).get(y1);
Point q2 = points.get(x2).get(y2);
q1.addCost(x2, y2);
q2.addCost(x1, y1);
}
static final int[][] DIRS = {{-1,0},{1,0},{0,-1},{0,1}};
static class Point {
int id;
int r;
int c;
int k;
// List of neighbors in (r,c,w) format
List<int[]> nbs = new ArrayList<>();
int costs = 1000000;
boolean done = false;
public Point(int r, int c, int k) {
this.r = r;
this.c = c;
this.id = r * (k + 1) + c;
this.k = k;
for (int[] dir : DIRS) {
int x = r + dir[0];
int y = c + dir[1];
if (x >= 0 && x <= k && y >= 0 && y <= k) {
nbs.add(new int[] {x, y, 0});
}
}
}
public void addCost(int r2, int c2) {
for (int[] nb : nbs) {
if (nb[0] == r2 && nb[1] == c2) {
nb[2]++;
return;
}
}
System.out.format(" %s does not have nb %d %d\n", this, r2, c2);
myAssert(false);
}
public boolean isAtBoundary() {
return r == 0 || r == k || c == 0 || c == k;
}
@Override
public String toString() {
return String.format("(%d,%d,%d)", r, c, costs);
}
}
static void doTest() {
long t0 = System.currentTimeMillis();
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
Scanner in = getInputScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int k = in.nextInt();
int[][] pairs = new int[n][4];
for (int i = 0; i < n; i++) {
pairs[i][0] = in.nextInt() - 1;
pairs[i][1] = in.nextInt() - 1;
pairs[i][2] = in.nextInt() - 1;
pairs[i][3] = in.nextInt() - 1;
}
int ans = solve(k, pairs);
System.out.println(ans);
}
in.close();
}
static Scanner getInputScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
final String CNAME = MethodHandles.lookup().lookupClass().getSimpleName();
final File fin = new File(USERDIR + "/io/c" + CNAME.substring(1,5) + "/" + CNAME + ".in");
return fin.exists() ? new Scanner(fin) : new Scanner(System.in);
} catch (Exception e) {
return new Scanner(System.in);
}
}
static String trace(int[] a) {
StringBuilder sb = new StringBuilder();
for (int v : a) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(v);
}
return sb.toString();
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
}
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 11
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
807d521ac3c35baed060bc143d1914f9
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class F {
public static final int INF = 0x3f3f3f3f;
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solve(io);
}
io.close();
}
public static void solve(Kattio io) {
int n = io.nextInt();
int k = io.nextInt();
int[][] horizontalEdges = new int[k + 1][k];
int[][] verticalEdges = new int[k][k + 1];
for (int i = 0; i < n; i++) {
int r1 = io.nextInt() - 1;
int c1 = io.nextInt() - 1;
int r2 = io.nextInt() - 1;
int c2 = io.nextInt() - 1;
if (r1 == r2) {
if (c2 < c1) {
int temp = c1;
c1 = c2;
c2 = temp;
}
verticalEdges[r1][c2]++;
verticalEdges[k - r1 - 1][k - c2]++;
} else {
if (r2 < r1) {
int temp = r1;
r1 = r2;
r2 = temp;
}
horizontalEdges[r2][c1]++;
horizontalEdges[k - r2][k - c1 - 1]++;
}
}
int[][] dist = new int[k + 1][k + 1];
for (int i = 0; i < k + 1; i++) {
Arrays.fill(dist[i], INF);
}
PriorityQueue<Item> pq = new PriorityQueue<>();
pq.add(new Item(0, 0, 0));
dist[0][0] = 0;
while (!pq.isEmpty()) {
var cur = pq.poll();
int r = cur.row;
int c = cur.col;
int d = cur.dist;
if (d > dist[r][c]) continue;
if (r > 0 && d + verticalEdges[r - 1][c] < dist[r - 1][c]) {
int newDist = dist[r - 1][c] = d + verticalEdges[r - 1][c];
pq.add(new Item(r - 1, c, newDist));
}
if (r < k && d + verticalEdges[r][c] < dist[r + 1][c]) {
int newDist = dist[r + 1][c] = d + verticalEdges[r][c];
pq.add(new Item(r + 1, c, newDist));
}
if (c > 0 && d + horizontalEdges[r][c - 1] < dist[r][c - 1]) {
int newDist = dist[r][c - 1] = d + horizontalEdges[r][c - 1];
pq.add(new Item(r, c - 1, newDist));
}
if (c < k && d + horizontalEdges[r][c] < dist[r][c + 1]) {
int newDist = dist[r][c + 1] = d + horizontalEdges[r][c];
pq.add(new Item( r, c + 1, newDist));
}
}
io.println(n - dist[k / 2][k / 2]);
}
static class Item implements Comparable<Item> {
public int row;
public int col;
public int dist;
public Item(int row, int col, int dist) {
this.row = row;
this.col = col;
this.dist = dist;
}
@Override
public int compareTo(Item other) {
return Integer.compare(dist, other.dist);
}
}
}
// modified from https://github.com/Kattis/kattio/blob/master/Kattio.java
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 11
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
c1937babbb7ad4d1ec4a21ca803ec8ad
|
train_109.jsonl
|
1642257300
|
There is a $$$k \times k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1$$$.An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array $$$a$$$ of $$$n$$$ pairs of adjacent squares. Find the size of the largest strong subsequence of $$$a$$$. An array $$$p$$$ is a subsequence of an array $$$q$$$ if $$$p$$$ can be obtained from $$$q$$$ by deletion of several (possibly, zero or all) elements.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class F {
public static final int INF = 0x3f3f3f3f;
public static void main(String[] args) {
var io = new Kattio(System.in, System.out);
int t = io.nextInt();
for (int i = 0; i < t; i++) {
solve(io);
}
io.close();
}
public static void solve(Kattio io) {
int n = io.nextInt();
int k = io.nextInt();
int[][] horizontalEdges = new int[k + 1][k];
int[][] verticalEdges = new int[k][k + 1];
for (int i = 0; i < n; i++) {
int r1 = io.nextInt() - 1;
int c1 = io.nextInt() - 1;
int r2 = io.nextInt() - 1;
int c2 = io.nextInt() - 1;
if (r1 == r2) {
if (c2 < c1) {
int temp = c1;
c1 = c2;
c2 = temp;
}
verticalEdges[r1][c2]++;
verticalEdges[k - r1 - 1][k - c2]++;
} else {
if (r2 < r1) {
int temp = r1;
r1 = r2;
r2 = temp;
}
horizontalEdges[r2][c1]++;
horizontalEdges[k - r2][k - c1 - 1]++;
}
}
int[][] dist = new int[k + 1][k + 1];
for (int i = 0; i < k + 1; i++) {
Arrays.fill(dist[i], INF);
}
PriorityQueue<Item> pq = new PriorityQueue<>();
pq.add(new Item(0, 0, 0));
dist[0][0] = 0;
while (!pq.isEmpty()) {
var cur = pq.poll();
int r = cur.row;
int c = cur.col;
int d = cur.dist;
if (d > dist[r][c]) continue;
if (r > 0 && d + verticalEdges[r - 1][c] < dist[r - 1][c]) {
int newDist = dist[r - 1][c] = d + verticalEdges[r - 1][c];
pq.add(new Item(r - 1, c, newDist));
}
if (r < k && d + verticalEdges[r][c] < dist[r + 1][c]) {
int newDist = dist[r + 1][c] = d + verticalEdges[r][c];
pq.add(new Item(r + 1, c, newDist));
}
if (c > 0 && d + horizontalEdges[r][c - 1] < dist[r][c - 1]) {
int newDist = dist[r][c - 1] = d + horizontalEdges[r][c - 1];
pq.add(new Item(r, c - 1, newDist));
}
if (c < k && d + horizontalEdges[r][c] < dist[r][c + 1]) {
int newDist = dist[r][c + 1] = d + horizontalEdges[r][c];
pq.add(new Item( r, c + 1, newDist));
}
}
io.println(n - dist[k / 2][k / 2]);
}
static class Item implements Comparable<Item> {
public int row;
public int col;
public int dist;
public Item(int row, int col, int dist) {
this.row = row;
this.col = col;
this.dist = dist;
}
@Override
public int compareTo(Item other) {
return Integer.compare(dist, other.dist);
}
}
}
// modified from https://github.com/Kattis/kattio/blob/master/Kattio.java
class Kattio extends PrintWriter {
public Kattio(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Kattio(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
}
|
Java
|
["3\n\n8 4\n\n1 2 1 3\n\n2 2 2 3\n\n3 2 3 3\n\n4 2 4 3\n\n1 4 2 4\n\n2 1 3 1\n\n2 2 3 2\n\n4 1 4 2\n\n7 2\n\n1 1 1 2\n\n2 1 2 2\n\n1 1 1 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 1 2 1\n\n1 2 2 2\n\n1 6\n\n3 3 3 4"]
|
3 seconds
|
["7\n4\n1"]
|
NoteIn the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
|
Java 11
|
standard input
|
[
"geometry",
"graphs",
"greedy",
"implementation",
"shortest paths"
] |
6c1d534c1066997eb7e7963b4e783708
|
The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 10^5$$$; $$$2 \leq k \leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$.
| 2,700
|
For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$.
|
standard output
| |
PASSED
|
b00674e37b6ba864b3c198e00c5790b7
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class ThreeSwimmers {
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(bf.readLine());
for(int i = 0; i < N; i++){
st = new StringTokenizer(bf.readLine());
long p = Long.parseLong(st.nextToken());
long a = Long.parseLong(st.nextToken());
long b = Long.parseLong(st.nextToken());
long c = Long.parseLong(st.nextToken());
long a2 = (p+a-1)/a * a;
long b2 = (p+b-1)/b * b;
long c2 = (p+c-1)/c * c;
System.out.println(Math.min(Math.min(a2, b2), c2)-p);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
0363b34518209eaa35f56d19adf48749
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class A_23
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
//Scanner sc=new Scanner(System.in);
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long m=Long.MAX_VALUE;
long i=2,a1=a,b1=b,c1=c;
if(a<p)
{
if(p%a!=0)
{
a=a-(p%a);
}
else
{
a=0;
}
}
else if(a>=p)
{
a=a-p;
}
if(b<p)
{
if(p%b!=0)
{
b=b-(p%b);
}
else
{
b=0;
}
}
else if(b>=p)
{
b=b-p;
}
if(c<p)
{
if(p%c!=0)
{
c=c-(p%c);
}
else
{
c=0;
}
}
else if(c>=p)
{
c=c-p;
}
m=Math.min(a,Math.min(b,c));
System.out.println(""+m);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
d7823f4def4ce495b807a678f6a8d6dd
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class ThreeSwimmers {
static void findResult(long p, long a, long b, long c){
long[] total = new long[]{0,0,0};
total[0] = a - p%a;
total[1] = b - p%b;
total[2] = c - p%c;
Arrays.sort(total);
if(p%a==0 || p%b==0 || p%c==0){
System.out.println(0);
}
else{
System.out.println(total[0]);
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int testCases = input.nextInt();
for (int i = 0; i < testCases; i++) {
long p = input.nextLong();
long a = input.nextLong();
long b = input.nextLong();
long c = input.nextLong();
findResult(p,a,b,c);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
b7152a11fdece05d1881d9a136ed835a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class cf1492A {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long p = 0;
long a = 0;
long b = 0;
long c = 0;
int t = scan.nextInt();
long r = 0;
int temp = 0;
int z = 0;
while(t-->0){
p = scan.nextLong();
a = scan.nextLong();
b = scan.nextLong();
c = scan.nextLong();
if(p%a==0 || p%b==0 || p%c==0){
z = 1;
}
if(p>a){
a = a*((p/a)+1);
}
if(p>b){
b = b*((p/b)+1);
}
if(p>c){
c = c*((p/c)+1);
}
// System.out.println("a:"+a+" b:"+b+" c:"+c);
r = Long.min(a-p, b-p);
r = Long.min(r, c-p);
if(z==1) System.out.println("0");
else System.out.println(r);
z = 0;
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
2bc0ae0245ca33baba0e6ec101e605b1
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
Scanner scn=new Scanner(System.in);
int t=scn.nextInt();
while(t-->0){
long p=scn.nextLong();
long[] arr=new long[3];
for(int i=0;i<3;i++){
arr[i]=scn.nextLong();
}
Arrays.sort(arr);
long minDiff=Long.MAX_VALUE;
for(int i=0;i<3;i++){
long count=p/arr[i];
if(p%arr[i]!=0){
count++;
}
long time=count*arr[i];
minDiff=Math.min(minDiff,time-p);
}
System.out.println(minDiff);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
0cb94edc17a0c53c4d8c49e79a1219e9
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//antiKnapsack();
threeSwimmers();
}
// public static void antiKnapsack(){
// Scanner sc = new Scanner(System.in);
// int noOfInput = sc.nextInt();
// for (int i=0;i<noOfInput;i++){
// int n = sc.nextInt();
// int k = sc.nextInt();
//
// int count = n-k + (k-(k+1)/2);
// System.out.println(count);
// for(int a=k+1;a<=n;a++ ){
// System.out.print(a+" ");
// }
//
// for(int b = (k+1)/2; b<k; b++){
// System.out.print(b+" ");
// }
// System.out.println();
// }
//
// }
public static void threeSwimmers() {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
for (int i = 0; i < input; i++) {
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long result = 9223372036854775807L;
long total = p%a;
if(total == 0){
System.out.println(0);
continue;
}
if(a-total<result){
result = a-total;
}
total = p%b;
if(total == 0){
System.out.println(0);
continue;
}
if(b-total<result){
result = b-total;
}
total = p%c;
if(total == 0){
System.out.println(0);
continue;
}
if(c-total<result){
result = c-total;
}
System.out.println(result);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
de869fa823147250cb4eb9705b5e1f76
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Scanner;
/**
*
* @author afsc1
*/
public class threeSwimmers {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
long n = sc.nextLong();
for(int i = 0; i<n;i++){
Calcular();
}
}
public static void Calcular(){
long p = sc.nextLong();
long a = sc.nextLong();
long b =sc.nextLong();
long c =sc. nextLong();
if(p%a ==0 || p%b == 0 || p%c ==0 ){
System.out.println("0");
return;
}
a =a-p%a;
b =b-p%b;
c =c-p%c;
long r=a;
if (a<b)
r=a;
else
r=b;
if(c<r)
r=c;
System.out.println(r);
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
4d47a0ea28a1901aed36dfaa173ece90
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Swim3 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
463f6269975104ac5d1aeb07a6044827
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
//import jdk.javadoc.internal.doclets.toolkit.NestedClassWriter;
public class Codeforces {
static class Node
{
int val;
Node left;
Node right;
public Node(int x) {
// TODO Auto-generated constructor stub
this.val=x;
this.left=null;
this.right=null;
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int[] nextArray(int n)
{
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=nextInt();
return arr;
}
}
static String string;
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
static int fac(int n)
{
int c=1;
for(int i=2;i<n;i++)
if(n%i==0)
c=i;
return c;
}
static boolean ab(int i,HashSet<Integer> hs,int n,int prev)
{
if(i==n)
return true;
int cur=2;
while(i<4*n)
{
if(prev==-1)
{
hs.add(cur);
if(ab(i+1, hs, n, cur))
{
string=cur+" "+string;
return true;
}
else
hs.remove(hs.size()-1);
}
else {
if(!hs.contains(cur)&&gcd(cur, prev)>1)
{
hs.add(cur);
Iterator<Integer> iterator=hs.iterator();
int flag=0;
while(iterator.hasNext())
{
int x=iterator.next();
if(x%cur==0||cur%x==0)
{
flag=1;
break;
}
}
if(flag==0&&ab(i+1, hs, n, cur))
{
string=cur+" "+string;
return true;
}
hs.remove(hs.size()-1);
}
}
cur+=2;
}
return false;
}
static boolean check(String all,String ch,int i,int k,boolean flag)
{
//System.out.println(i+","+k);
if(i==all.length()||ch.length()==k)
{
if(ch.length()==k&&flag)
return true;
return false;
}
for(int j=i;j<all.length();j++)
{
if(i>0&&j!=i&&ch.charAt(k)==all.charAt(j)&&check(all, ch, j+1,k+1, true))
return true;
else if(ch.charAt(k)==all.charAt(j)&&check(all, ch, j+1,k+1, flag))
return true;
}
return false;
}
static int lcm(int a,int b)
{
for(int i=Math.min(a, b);i<=a*b;i++)
if(i%a==0&&i%b==0)
return i;
return 0;
}
static int maxheight(char[][] ch,int i,int j,String[] arr)
{
int h=1;
if(i==ch.length-1||j==0||j==ch[0].length-1)
return 1;
while(i+h<ch.length&&j-h>=0&&j+h<ch[0].length&&ch[i+h][j-h]=='*'&&ch[i+h][j+h]=='*')
{
String whole=arr[i+h];
//System.out.println(whole.substring(j-h,j+h+1));
if(whole.substring(j-h,j+h+1).replace("*","").length()>0)
return h;
h++;
}
return h;
}
static boolean all(BigInteger n)
{
BigInteger c=n;
HashSet<Character> hs=new HashSet<>();
while((c+"").compareTo("0")>0)
{
String d=""+c;
char ch=d.charAt(d.length()-1);
if(d.length()==1)
{
c=new BigInteger("0");
}
else
c=new BigInteger(d.substring(0,d.length()-1));
if(hs.contains(ch))
continue;
if(d.charAt(d.length()-1)=='0')
continue;
if(!(n.mod(new BigInteger(""+ch)).equals(new BigInteger("0"))))
return false;
hs.add(ch);
}
return true;
}
static int helper(String all,int i,int p,int k,int x,int y,int dp[])
{
if(i>=all.length())
return 0;
if(dp[i]!=0)
return dp[i];
if(p>0&&all.length()-i>=p)
{
if(all.charAt(i+p-1)=='0')
return dp[i]=Math.min(y+helper(all, i+1, p, k, x, y, dp),x+helper(all, i+p, 0, k, x, y, dp));
else {
return dp[i]=Math.min(y+helper(all, i+1, p, k, x, y, dp), helper(all, i+p, 0, k, x, y, dp));
}
}
else if(p>0)
return dp[i]=x;
else {
if(all.charAt(i)=='0')
return dp[i]=x+helper(all, i+k, p, k, x, y, dp);
else {
return dp[i]=helper(all, i+k, p, k, x, y, dp);
}
}
}
static int cal(long n,long k)
{
System.out.println(n+","+k);
if(n==k)
return 2;
if(n<k)
return 1;
if(k==1)
return 1+cal(n, k+1);
if(k>=32)
return 1+cal(n/k, k);
return 1+Math.min(cal(n/k, k),cal(n, k+1));
}
static Node buildTree(int i,int j,int[] arr)
{
if(i==j)
{
//System.out.print(arr[i]);
return new Node(arr[i]);
}
int max=i;
for(int k=i+1;k<=j;k++)
{
if(arr[max]<arr[k])
max=k;
}
Node root=new Node(arr[max]);
//System.out.print(arr[max]);
if(max>i)
root.left=buildTree(i, max-1, arr);
else {
root.left=null;
}
if(max<j)
root.right=buildTree(max+1, j, arr);
else {
root.right=null;
}
return root;
}
static int height(Node root,int val)
{
if(root==null)
return Integer.MAX_VALUE-32;
if(root.val==val)
return 0;
if((root.left==null&&root.right==null))
return Integer.MAX_VALUE-32;
return Math.min(height(root.left, val), height(root.right, val))+1;
}
public static void main(String[] args)throws IOException
{
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
FastReader fs=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
StringBuilder sb=new StringBuilder();
while(T-->0)
{
string="";
//int n=fs.nextInt();
//int p=fs.nextInt(),a=fs.nextInt(),b=fs.nextInt(),c=fs.nextInt();
long p=fs.nextLong(),a=fs.nextLong(),b=fs.nextLong(),c=fs.nextLong();
//Long n=fs.nextLong();
if(p==0)
{
sb.append(0+"\n");
continue;
}
long min=a-p%a==a?0:a-p%a;
min=Math.min(min,b-p%b==b?0:b-p%b);
min=Math.min(min,c-p%c==c?0:c-p%c);
sb.append((long)min+"\n");
//System.out.println(min);
}
System.out.println(sb);
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
af9f0e0f313540b4859f5ea4159a2669
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class threeswimmers
{
public static void main(String[] args)
{
int t;
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
while(t-->0)
{
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
d0dba207a607cce4147126a6dc596f3a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
int mod = 1000000007;
while (t > 0) {
t--;
long[] arr = new long[4];
for (int i = 0; i < 4; i++) {
arr[i] = scan.nextLong();
}
long p = arr[0], a = arr[1], b = arr[2], c = arr[3];
if (p % a == 0 || p % b == 0 || p % c == 0) {
System.out.println(0);
continue;
}
long ans_a = (a * ((p / a) + 1)) - p;
long ans_b = (b * ((p / b) + 1)) - p;
long ans_c = (c * ((p / c) + 1)) - p;
if (ans_a < 0) ans_a = 0;
if (ans_b < 0) ans_b = 0;
if (ans_c < 0) ans_c = 0;
System.out.println(Math.min(ans_a, Math.min(ans_b, ans_c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
e30da48747cf29ca93ca253ec79eb74c
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) {
FastReader fs = new FastReader();
StringBuilder sb = new StringBuilder();
int t = fs.nextInt();
for (int tt = 0; tt < t; tt++) {
long p = fs.nextLong();
long a = fs.nextLong();
long b = fs.nextLong();
long c = fs.nextLong();
long n1 = p % a == 0 ? p / a : 1 + p / a;
long n2 = p % b == 0 ? p / b : 1 + p / b;
long n3 = p % c == 0 ? p / c : 1 + p / c;
sb.append(Math.min(a * n1 - p, Math.min(b * n2 - p, c * n3 - p)));
sb.append("\n");
}
System.out.print(sb.toString());
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
a377129384b7a827058df379b4cb4f36
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Arrays;
import java.util.Scanner;
public class _1098MaximumProduct {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t>0) {
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long lapsfora=(a+p-1)/(a);
long lapsforb=(b+p-1)/(b);
long lapsforc=(c+p-1)/(c);
System.out.println(Math.min(lapsfora*a-p, Math.min(lapsforc*c-p, lapsforb*b-p)));
t--;
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
19cff5039053734e157e1a9dd110c22c
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->=1){
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
if( p%a == 0 || p%b==0 || p%c==0){
//if( a== p || b == p || c == p){
System.out.println("0");
}
else {
long ans = a ;
ans = Math.min((a-p%a),ans);
ans = Math.min(b-p%b,ans);
ans = Math.min(ans,c-p%c);
System.out.println(ans);
}
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
4d373d467c225018af110a18cd26239b
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
//package networking;
import java.util.Scanner;
public class MainNetworking {
public static void main(String[] args) {
//go to sleep :)
Scanner scanner = new Scanner(System.in);
long testCases = scanner.nextInt();
long p, result;
long[] arr = new long[3];
for (int i = 0; i < testCases; i++) {
p = scanner.nextLong();
for (int j = 0; j < 3; j++) {
arr[j] = scanner.nextLong();
}
result = Long.MAX_VALUE;
for (int k = 0; k < arr.length; k++) {
if (p % arr[k] == 0) {
result = 0;
} else {
long position, multiplier, difference, factor;
position = arr[k];
factor = p / position;
multiplier = (position * (factor + 1));
difference = multiplier - p;
if (difference < result) {
result = difference;
}
}
}
System.out.println(result);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
dba13124905282a56efe9c1d377cd723
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Threeswimmers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long sets = in.nextLong();
for (long i = 0; i < sets; i++) {
long myTime = in.nextLong();
long timeA = in.nextLong();
long timeB = in.nextLong();
long timeC = in.nextLong();
if (timeA < myTime) {
if (myTime % timeA == 0) {
timeA = ((myTime / timeA)) * timeA;
}
else {
timeA = ((myTime / timeA) + 1) * timeA;
}
}
if (timeB < myTime) {
if (myTime % timeB == 0) {
timeB = ((myTime / timeB)) * timeB;
}
else {
timeB = ((myTime / timeB) + 1) * timeB;
}
}
if (timeC < myTime) {
if (myTime % timeC == 0) {
timeC = ((myTime / timeC)) * timeC;
}
else {
timeC = ((myTime / timeC) + 1) * timeC;
}
}
timeA = timeA - myTime;
timeB = timeB - myTime;
timeC = timeC - myTime;
if (timeB < timeA) {
timeA = timeB;
}
if (timeC < timeA) {
timeA = timeC;
}
System.out.println(timeA);
}
in.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
19da65b626883a6ebd0f0effac76b420
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class threeSwimmers {
public static void tempsAttenduAvantPremierArrivee(){
Scanner scanner =new Scanner(System.in);
int testNbr = scanner.nextInt();
for(int i=0; i<testNbr;i++){
String enter = scanner.nextLine();
long minutes_Arrivee = scanner.nextLong();
long minutes_First_Swimmer = scanner.nextLong();
long minutes_Seconde_Swimmer = scanner.nextLong();
long minutes_third_Swimmer = scanner.nextLong();
if(minutes_Arrivee > minutes_First_Swimmer){
long nbrDeFois = minutes_Arrivee % minutes_First_Swimmer == 0 ?
(long) (minutes_Arrivee / minutes_First_Swimmer) :
(long) (minutes_Arrivee / minutes_First_Swimmer) + 1 ;
minutes_First_Swimmer = nbrDeFois * minutes_First_Swimmer;
}
if(minutes_Arrivee > minutes_Seconde_Swimmer){
long nbrDeFois = minutes_Arrivee % minutes_Seconde_Swimmer == 0 ?
(long) (minutes_Arrivee / minutes_Seconde_Swimmer) :
(long) (minutes_Arrivee / minutes_Seconde_Swimmer) +1 ;
minutes_Seconde_Swimmer = nbrDeFois * minutes_Seconde_Swimmer;
}
if(minutes_Arrivee > minutes_third_Swimmer){
long nbrDeFois = minutes_Arrivee % minutes_third_Swimmer == 0 ?
(long) (minutes_Arrivee / minutes_third_Swimmer) :
(long) (minutes_Arrivee / minutes_third_Swimmer) +1;
minutes_third_Swimmer = nbrDeFois * minutes_third_Swimmer;
}
long tempsAttenduArrivee_FirstSwimmer = minutes_First_Swimmer - minutes_Arrivee;
long tempsAttenduArrivee_SecondeSwimmer = minutes_Seconde_Swimmer - minutes_Arrivee;
long tempsAttenduArrivee_ThirdSwimmer = minutes_third_Swimmer - minutes_Arrivee;
long tempsAttenduFinal = Math.min(
Math.min(tempsAttenduArrivee_FirstSwimmer, tempsAttenduArrivee_SecondeSwimmer),
tempsAttenduArrivee_ThirdSwimmer
);
System.out.println(tempsAttenduFinal);
}
}
public static void main(String[] args){
tempsAttenduAvantPremierArrivee();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
5377539724d7cfbdef3ae77eec7dc0e3
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class ThreeSwimmers {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int cases = input.nextInt();
for(int i=0;i<cases;i++) {
long p = input.nextLong();
long a = input.nextLong();
long b = input.nextLong();
long c = input.nextLong();
long na = (a*((p/a)+(p%a == 0 ? 0 : 1)));
long nb = (b*((p/b)+(p%b == 0 ? 0 : 1)));
long nc = (c*((p/c)+(p%c == 0 ? 0 : 1)));
System.out.println(Math.min(Math.min(na, nb), nc)-(long)p);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
29051200041c9c3e0f12da1e7a18adac
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class CodeForces704 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i<t; i++){
System.out.println(solve(sc));
}
}
static long solve(Scanner sc) {
long p = sc.nextLong(), a = sc.nextLong(), b = sc.nextLong(), c = sc.nextLong();
long newA = 0, newB = 0, newC = 0;
newA = a-(p%a);
newB = b-(p%b);
newC = c-(p%c);
if (p%a==0 || p%b==0 || p%c==0)
return 0;
return min(newA, newB, newC);
}
static long min(long a, long b, long c){
long t = Math.min(a, b);
return Math.min(t, c);
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
bf58b2ba710fde1fc537ceaf1fd782d7
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Solution{
static PrintWriter out = new PrintWriter(System.out);
static int mod = 1000000009;
public static void main(String[] args) throws IOException {
FastReader fs = new FastReader();
int t = fs.nextInt();
while(t > 0){
long p = fs.nextLong();
long a = fs.nextLong();
long b = fs.nextLong();
long c = fs.nextLong();
out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
t--;
}
out.flush();
}
static void op(int[] a, int n){
int max = Integer.MIN_VALUE;
for(int i = 0; i < n; i++){
max = Math.max(max, a[i]);
}
for(int i = 0; i < n; i++){
a[i] = max - a[i];
}
}
static int isCs(String x, String y, int n, int m, int[][] dp){
if(n == 0 || m == 0){
return 0;
}
if(dp[n-1][m-1] != 0) {return dp[n-1][m-1];}
if(x.charAt(n-1) == y.charAt(m-1)){
return dp[n-1][m-1] = isCs(x, y, n-1, m-1, dp) + 1;
}else{
return dp[n-1][m-1] = Math.max(isCs(x, y, n-1, m, dp), isCs(x, y, n, m-1, dp));
}
}
static int binarySearch(int[] a){
int n = a.length;
return 0;
}
static boolean distinct(String s){
for(int i = 0; i < s.length()-1; i++){
for(int j = i+1; j < s.length(); j++){
if(s.charAt(i) == s.charAt(j)){
return false;
}
}
}
return true;
}
static long sumOfDigit(long n){
long sum = 0;
while(n > 0){
long rem = n % 10;
sum += rem;
n = n / 10;
}
return sum;
}
static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
static int[] numArr(int n){
int len = countDigit(n);
int[] a = new int[len];
int i = 0;
while(i < len){
a[i] = n % 10;
n = n / 10;
i++;
}
return a;
}
static void lcs(String X, String Y, int m, int n, int temp)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
// Following code is used to print LCS
int index = L[m][n];
temp = index;
// Create a character array to store the lcs string
char[] lcs = new char[index+1];
lcs[index] = '\u0000'; // Set the terminating character
// Start from the right-most-bottom-most corner and
// one by one store characters in lcs[]
int i = m;
int j = n;
while (i > 0 && j > 0)
{
// If current character in X[] and Y are same, then
// current character is part of LCS
if (X.charAt(i-1) == Y.charAt(j-1))
{
// Put current character in result
lcs[index-1] = X.charAt(i-1);
// reduce values of i, j and index
i--;
j--;
index--;
}
// If not same, then find the larger of two and
// go in the direction of larger value
else if (L[i-1][j] > L[i][j-1])
i--;
else
j--;
}
for(int k=0;k<=temp;k++)
System.out.print(lcs[k]);
}
static void sortDe(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
int list = l.size() - 1;
for (int i = 0; i < a.length; i++)
{ a[i] = l.get(list);
list--;
}
}
static void bracket(int n){
for(int i = 0; i < n; i++){
out.print("(");
}
for(int i = 0; i < n; i++){
out.print(")");
}
}
static int countDigit(int n){
return (int) Math.floor(Math.log10(n)) + 1;
}
static int countDigit(long n){
return (int) Math.floor(Math.log10(n)) + 1;
}
static void print(int[] ar){
for(int i = 0; i < ar.length; i++){
out.print(ar[i] + " ");
}
}
static long countEven(long n){
long c = 0;
long rem = 0;
while(n > 1){
rem = n % 10;
if(rem % 2 == 0){
c++;
}
n = n / 10;
}
return c;
}
static boolean divisor(long n){
int count = 2;
for(int i = 2; i*i <= n; i++){
if(n % i == 0){
count++;
}
if(count < 3){
break;
}
}
if(count == 3){return true;}
else{return false;}
}
static String reverseString(String s){
int j = s.length() - 1;
String st = new String();
while(j >= 0){
st += s.charAt(j);
j--;
}
return st;
}
static boolean isPallindrome(String s){
int i = 0, j = s.length() - 1;
while(i < j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static boolean lcsPallindrom(String s){
int i = 0, j = s.length()-1;
while(i < j){
if(s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static int[] nprimeNumbers(int n){
int[] mark = new int[n+1];
Arrays.fill(mark, 1);
mark[0] = 0;
mark[1] = 0;
for(int i = 2; i < mark.length; i++){
if(mark[i] == 1){
for(int j = i*i; j < mark.length; j += i){
mark[j] = 0;
}
}
}
return mark;
}
static long gcd(long a, long b){
if(b > a){return gcd(b, a);}
if(b == 0){return a;}
return gcd(b, a % b);
}
static int gcd(int a, int b){
if(b > a){return gcd(b, a);}
if(b == 0){return a;}
return gcd(b, a % b);
}
static class Pair {
int lvl;
int str;
Pair(int lvl, int str){
this.lvl = lvl;
this.str = str;
}
}
static class lvlcompare implements Comparator<Pair> {
public int compare(Pair p1, Pair p2){
if(p1.lvl == p2.lvl){
return 0;
}else if(p1.lvl > p2.lvl){
return 1;
}else {
return -1;
}
}
}
static long count(long n){
long ct = 0;
while(n > 0){
n = n / 10;
ct++;
}
return ct;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{br = new BufferedReader(new InputStreamReader(System.in));}
String next()
{ while (st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch (IOException e) {e.printStackTrace();}}
return st.nextToken();}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
String nextLine()
{ String str = "";
try {str = br.readLine();}
catch (IOException e) {e.printStackTrace();}
return str;
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
c9e4c9b5cf15990cd10908ddadc9d5c6
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class file{
private static long solve(long p, long a, long b, long c){
if(p % a == 0 || p % b == 0 || p % c == 0){
return 0;
}
long first = a * ((p / a)+1) - p;
long second = b * ((p / b)+1) - p;
long third = c * ((p / c)+1) - p;
//System.out.println(first);
return Math.min(first, Math.min(second, third));
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long ans = solve(p, a, b, c);
System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
d5ca063b9fe9e49dbdb4bd5c7bd8081a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
///*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*///
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Vector;
import javafx.util.Pair;
public class NewClass{
public static void main(String[] args){
Scanner ob=new Scanner(System.in);
int t=ob.nextInt();
out:
for(int ii=0;ii<t;ii++){
long a=ob.nextLong();
long b=ob.nextLong();
long c=ob.nextLong();
long d=ob.nextLong();
if(a%b==0||a%c==0||a%d==0){
System.out.println("0");
continue out;
}
long bb=(((a/b)+1)*b)-a;
long cc=(((a/c)+1)*c)-a;
long dd=(((a/d)+1)*d)-a;
System.out.println(Math.min(bb,Math.min(cc,dd)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
9c8f5e4cb0fcc1c0d4f417fbb8becc3f
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class threeSwimmers {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()); // String separated by delimitor
int n = Integer.parseInt(st.nextToken());
for(int i = 0; i < n;i++){
st = new StringTokenizer(br.readLine());
long p = Long.parseLong(st.nextToken());
long a = Long.parseLong(st.nextToken());
long b = Long.parseLong(st.nextToken());
long c = Long.parseLong(st.nextToken());
long a1 = (a - p%a)%a;
long b1 = (b - p%b)%b;
long c1 = (c - p%c)%c;
System.out.println(Math.min(a1,Math.min(c1,b1)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
d921858780eda9931aa2b546ae1f507f
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class threeSwimmers {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()); // String separated by delimitor
int n = Integer.parseInt(st.nextToken());
for(int i = 0; i < n;i++){
st = new StringTokenizer(br.readLine());
long p = Long.parseLong(st.nextToken());
long a = Long.parseLong(st.nextToken());
long b = Long.parseLong(st.nextToken());
long c = Long.parseLong(st.nextToken());
long ans = 0;
long a1 = (a- p%a)%a;
long b1 = (b- p%b)%b;
long c1 = (c- p%c)%c;
ans = Math.min(a1, Math.min(b1, c1));
System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
f1055bd3743ec09fa43435b9fa37b46a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
public class Solution {
static class FastIO
{
BufferedReader br;
StringTokenizer st;
public FastIO()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
FastIO fastIO = new FastIO();
public static void main(String[] args){
new Solution().solve();
}
public void solve() {
int t = fastIO.nextInt();
StringBuilder strb = new StringBuilder();
while(t-->0)
{
long p = fastIO. nextLong();
long a=fastIO.nextLong();
long b = fastIO.nextLong();
long c=fastIO.nextLong();
strb.append(minWaitTime(p,a,b,c)).append('\n');
}
System.out.println(strb.toString());
}
/**
*
* 9 5 4 8
* 2 6 10 9
* 10 2 5 10
* 10 9 9 9
*
*/
long minWaitTime(long p, long a, long b, long c)
{
long min = p%a==0 ? 0 : a-p%a;
min = Math.min(min, p%b==0 ? 0 : b-p%b);
min = Math.min(min, p%c==0 ? 0 : c-p%c);
return min;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
4637cd14dd20dd6d90e30c9423077f1c
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class MainForces {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner fs = new FastScanner(inputStream);
PrintWriter pw = new PrintWriter(outputStream);
Solution s = new Solution();
s.solve(fs, pw);
}
static class Solution{
public void solve(FastScanner fs, PrintWriter pw){
int cases = fs.nextInt();
while(cases-- > 0){
long p = fs.nextLong();
long a = fs.nextLong();
long b = fs.nextLong();
long c = fs.nextLong();
long min1 = p%a == 0 ? 0 : a-(p%a);
long min2 = p%b == 0 ? 0 : b-(p%b);
long min3 = p%c == 0 ? 0 : c-(p%c);
System.out.println(Math.min(Math.min(min1, min2), min3));
}
pw.close();
}
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream is){
br = new BufferedReader(new InputStreamReader(is));
st = null;
}
String next(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
} catch (Exception e){
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
BigInteger nextBigInteger(){
return new BigInteger(next());
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
10adbcb544696de8e9a7c62a1cf60534
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import static java.util.Arrays.sort;
import javafx.util.Pair;
public class Main
{
public static void main(String[] args)
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
while (tc-- > 0) {
long p, a, b, c;
p = input.nextLong();
a = input.nextLong();
b = input.nextLong();
c = input.nextLong();
long ans1 = ((p / a +((p%a==0)?0:1)) * a - p);
long ans2 = ((p / b+((p%b==0)?0:1) ) * b - p);
long ans3 = ((p / c +((p%c==0)?0:1)) * c - p);
System.out.println(Math.min(ans1, Math.min(ans2, ans3)));
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
6f57afa185f8d412fd7c4988d4078487
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t--!=0) {
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
if(p%a==0||p%b==0||p%c==0) {
System.out.println(0);
}
else {
long d = a - (p%a);
long e = b - (p%b);
long f = c - (p%c);
long x = Math.min(d, e);
x = Math.min(x, f);
System.out.println(x);}
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
349bc9ebb8a3e8f2b03ce78faa30d9f0
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// uncomment this line if you want to use the txt file to take input
//FileReader fr=new FileReader("D:\\test.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- != 0){
String[] ss = br.readLine().split(" ");
long p = Long.parseLong(ss[0]);
long a = Long.parseLong(ss[1]);
long b = Long.parseLong(ss[2]);
long c = Long.parseLong(ss[3]);
System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
209868b6bb7918d6f2e61cde9b555794
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
static Scanner scan = new Scanner(System.in);
/**
* 999999999999999998
* 999999999998
* 1000000000000
*
* 1000000000000 999999999999 999999999999 999999999999
*/
public static void slove() {
long p = scan.nextLong();
long res = (long)1e18;
for(int i=0;i<3;i++) {
long x = scan.nextLong();
res=Math.min(res,(p + (x-1)) / x * x - p);
}
System.out.println(res);
}
public static void main(String[] args) {
int t = scan.nextInt();
while (t-- > 0) {
slove();
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
c837e3da08f3a8a275d406dcdb36e616
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Main{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int j=0;j<t;j++)
{
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
ArrayList<Long> ar=new ArrayList<>();
if(p%a==0)
{
ar.add((long)0);
}else{
ar.add(a-p%a);
}
if(p%b==0)
{
ar.add((long)0);
}else{
ar.add(b-p%b);
}
if(p%c==0)
{
ar.add((long)0);
}else{
ar.add(c-p%c);
}
// ar.add(b-p%b);
// ar.add(c-p%c);
long ans=Collections.min(ar);
System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
98afe67f19034088020077aae13f078a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Swimmer {
public static long firstComer(long p,long a,long b,long c) {
long RemA=p%a;
long RemB=p%b;
long RemC=p%c;
if(RemA!=0&&RemB!=0&&RemC!=0) {
long tArrivalA=a-RemA;
long tArrivalB=b-RemB;
long tArrivalC=c-RemC;
return Math.min(tArrivalA, Math.min(tArrivalC, tArrivalB));
}
else return 0;
}
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int t=in.nextInt();
long[][] arr=new long[t][4];
for(int i=0;i<t;i++) {
for(int j=0;j<4;j++) {
arr[i][j]=in.nextLong();
}
}
for(int k=0;k<t;k++) {
System.out.println(firstComer(arr[k][0], arr[k][1], arr[k][2], arr[k][3]));
}
in.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
c29f7d9994ec89340d764991d34430e2
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import static java.lang.Math.abs;
import static java.lang.Math.min;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
// https://codeforces.com/problemset/problem/1492/A
public class ThreeSwimmers
{
private static final MyReader myReader = new MyReader();
private static final PrintWriter myWriter = new PrintWriter(System.out);
public static void main(String[] args) {
long t = Long.parseLong(myReader.next());
long p, a, b, c;
while (t-- > 0) {
p = Long.parseLong(myReader.next());
a = Long.parseLong(myReader.next());
b = Long.parseLong(myReader.next());
c = Long.parseLong(myReader.next());
myWriter.println( min(
min(
((p + a - 1)/a) * a,
((p + b - 1) / b) * b),
((p + c - 1) / c) * c) - p);
}
myWriter.close();
}
private static class MyReader
{
private static BufferedReader br;
private static StringTokenizer st;
public MyReader() {
br = new BufferedReader(new InputStreamReader(System.in), Short.MAX_VALUE);
}
final String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
42e6633c63b111277ab8eb05d9b1c8c8
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
for(int i=1;i<=t;i++){
long a,b,c,p;
p=sc.nextLong();
a=sc.nextLong();
b=sc.nextLong();
c=sc.nextLong();
long m=min(a,b,c);
if((p%a==0)||(p%b==0)||(p%c==0)){
System.out.println(0);
}
else if(m>p)
System.out.println(m-p);
else{
System.out.println(min((a-p%a),(b-p%b),(c-p%c)));
}
}
}
public static long min(long a,long b,long c)
{
if(a<=b){
if(a<=c)
return a;
else
return c;
}
if(b<=c)
return b;
else return c;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
4d82a49e3a2450714d8a168bc09a1c0d
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader r =new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
long cases = Integer.parseInt(br.readLine());
for(int i = 0; i < cases; i++) {
String[] array = br.readLine().split(" ");
long delay = Long.parseLong(array[0]);
List<Long> differences = new ArrayList<Long>();
for(int x = 1; x < array.length; x++) {
if(delay % Long.parseLong(array[x]) != 0) {
differences.add( Long.parseLong(array[x]) - (delay % Long.parseLong(array[x])));
} else {
differences.add((long) 0);
}
}
Collections.sort(differences);
System.out.println(differences.get(0));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
ac74a9dcb6e110b31038853053b45d19
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- != 0){
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
long aRem = 0;
long bRem = 0;
long cRem = 0;
if(p > a){
long ans = p%a;
aRem = a - ans;
}else if(p == a){
aRem = 0;
}else{
aRem = a - p;
}
if(p > b){
long ans = p%b;
bRem = b - ans;
}else if(p == b){
bRem = 0;
}else{
bRem = b - p;
}
if(p > c){
long ans = p % c;
cRem = c - ans;
}else if(p == c){
cRem = 0;
}else{
cRem = c - p;
}
if((p%a == 0) || (p%b == 0) || (p%c == 0)){
System.out.println(0);
}else{
System.out.println(Math.min(aRem , Math.min(bRem , cRem)));
}
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
7556c59e336e51ef845379b6c8b88794
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Swimer {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args)throws IOException {
Reader scan = new Reader();
int T = scan.nextInt(); StringBuffer sb = new StringBuffer();
while (T-->0)
{
long p = scan.nextLong();
long a = scan.nextLong();
long b = scan.nextLong();
long c = scan.nextLong();
sb.append(solve(p,a,b,c)+"\n");
}
System.out.print(sb);
}
private static long solve(long p, long a, long b, long c) {
long Min = Long.MAX_VALUE;
if(p!=a)
Min = Math.min(Min,((p/a + 1)*a - p));
if(p!=b)
Min = Math.min(Min,((p/b + 1)*b - p));
if(p!=c)
Min = Math.min(Min,((p/c + 1)*c - p));
if(p%a==0 || p%c==0 || p%b==0)
Min = 0;
return Min;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
288ceba35fdceae9b3bc703100d9d4e8
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
import java.math.*;
import java.io.*;
public class Hell{
public static void main(String args[]) {
//Scanner sc = new Scanner(System.in);
FastIo sc = new FastIo();
int t = sc.nextInt();
while(t--!=0) {
BigInteger b[] = new BigInteger[4];
for(int i = 0; i<4; i++) b[i] = sc.nextBigInteger();
BigInteger rem = b[0].remainder(b[1]);
BigInteger one = new BigInteger("1");
BigInteger s;
BigInteger rem1;
BigInteger zero = new BigInteger("0");
if(rem.equals(zero)) {
s = zero;
}else {
b[1] = ((b[0].divide(b[1])).add(one)).multiply(b[1]);
s = b[1].subtract(b[0]);
}
for(int i =2; i<4;i++) {
rem1 = b[0].remainder(b[i]);;
if(rem1.equals(zero)) {
b[i] = b[0];
}else {
b[i] = ((b[0].divide(b[i])).add(one)).multiply(b[i]);
}
s = (b[i].subtract(b[0])).min(s);
}
System.out.println(s);
}
//sc.close();
}
}
class FastIo{
BufferedReader br;
StringTokenizer st;
BigInteger bb;
public FastIo() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st==null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
BigInteger nextBigInteger() {
return bb = new BigInteger(next());
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
34e2b7c7947612282e13a3fb9dee37d9
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
import java.math.*;
public class Hell{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t--!=0) {
BigInteger b[] = new BigInteger[4];
for(int i = 0; i<4; i++) b[i] = sc.nextBigInteger();
BigInteger rem = b[0].remainder(b[1]);
BigInteger one = new BigInteger("1");
BigInteger s;
BigInteger rem1;
BigInteger zero = new BigInteger("0");
if(rem.equals(zero)) {
s = zero;
}else {
b[1] = ((b[0].divide(b[1])).add(one)).multiply(b[1]);
s = b[1].subtract(b[0]);
}
for(int i =2; i<4;i++) {
rem1 = b[0].remainder(b[i]);;
if(rem1.equals(zero)) {
b[i] = b[0];
}else {
b[i] = ((b[0].divide(b[i])).add(one)).multiply(b[i]);
}
s = (b[i].subtract(b[0])).min(s);
}
System.out.println(s);
}
sc.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
17aa041e383b8dfb33d72ac2f476574a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Swim3 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
9d53889a414af57d486b4ae712b375bb
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
//package com.company;
import java.io.*;
import java.util.*;
public class Main {
static void sieveOfEratosthenes(int n){
boolean[] prime = new boolean[n + 1];
for (int i = 2; i <= n; i++) prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p]){
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
// Use greedy ONLY when locally optimum result yields globally optimum result.
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0) {
StringTokenizer st = new StringTokenizer(br.readLine());
long p = Long.parseLong(st.nextToken()),a=Long.parseLong(st.nextToken()),b=Long.parseLong(st.nextToken()),c=Long.parseLong(st.nextToken());
long t1=p%a==0?p/a:p/a+1;
t1=a*t1-p;
long t2=p%b==0?p/b:p/b+1;
t2=b*t2-p;
long t3=p%c==0?p/c:p/c+1;
t3=c*t3-p;
pw.println(Math.min(t1,Math.min(t2,t3)));
}
pw.flush();
pw.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
c5efd236aaebd2807806573f108808a0
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Check2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
a = a-(p%a==0?a:p%a);
b = b-(p%b==0?b:p%b);
c = c-(p%c==0?c:p%c);
List<Long> list = new ArrayList<>();
list.add(a);
list.add(b);
list.add(c);
Collections.sort(list);
System.out.println(list.get(0));
}
}
/* public static int getAns(int n,int[] ar,int orig){
int y1=n-ar[0];
int y2=n-ar[1];
int y3=n-ar[2];
if(y1==ar[0] || y1==ar[1] || y1==ar[2]){
}
if(y2==ar[0] || y2==ar[1] || y2==ar[2]){
}
if(y3==ar[0] || y3==ar[1] || y3==ar[2]){
}
}*/
public static long power(long a, long b, long c) {
long ans = 1;
while (b != 0) {
if (b % 2 == 1) {
ans = ans * a;
ans %= c;
}
a = a * a;
a %= c;
b /= 2;
}
return ans;
}
public static long power1(long a, long b, long c) {
long ans = 1;
while (b != 0) {
if (b % 2 == 1) {
ans = multiply(ans, a, c);
}
a = multiply(a, a, c);
b /= 2;
}
return ans;
}
public static long multiply(long a, long b, long c) {
long res = 0;
a %= c;
while (b > 0) {
if (b % 2 == 1) {
res = (res + a) % c;
}
a = (a + a) % c;
b /= 2;
}
return res % c;
}
public static long totient(long n) {
long result = n;
for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
//sum=sum+2*i;
while (n % i == 0) {
n /= i;
// sum=sum+n;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
public static boolean[] primes(int n) {
boolean[] p = new boolean[n + 1];
p[0] = false;
p[1] = false;
for (int i = 2; i <= n; i++) {
p[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (p[i]) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static int val(char c) {
if (c >= '0' && c <= '9')
return (int) c - '0';
else
return (int) c - 'A' + 10;
}
// Function to convert a
// number from given base
// 'b' to decimal
static int toDeci(String str,
int base) {
int len = str.length();
int power = 1; // Initialize
// power of base
int num = 0; // Initialize result
int i;
// Decimal equivalent is
// str[len-1]*1 + str[len-2] *
// base + str[len-3]*(base^2) + ...
for (i = len - 1; i >= 0; i--) {
// A digit in input number
// must be less than
// number's base
if (val(str.charAt(i)) >= base) {
System.out.println("Invalid Number");
return -1;
}
num += val(str.charAt(i)) * power;
power = power * base;
}
return num;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
6355e42305cae2463ffd02aedf426d63
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long A=a-p%a;
long B=b-p%b;
long C=c-p%c;
if(A==a)
A=0;
if(B==b)
B=0;
if(C==c)
C=0;
long ans=Math.min(A, B);
ans=Math.min(ans, C);
System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
ed8fe29c588b3261813241e531270c4b
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class p1492A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
for(int t=sc.nextInt();t-->0;) {
long p=sc.nextLong(),a=sc.nextLong(),b=sc.nextLong(),c=sc.nextLong();
if(p%a!=0) a*=(p/a+1); else a=p;
if(p%b!=0) b*=(p/b+1); else b=p;
if(p%c!=0) c*=(p/c+1); else c=p;
System.out.println(Math.min(a-p,Math.min(b-p, c-p)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
891d944dabb890e8630bdc9fd928a387
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class A {
public static void main(String args[]) {
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int tc, i, j;
String s;
tc = sc.nextInt();
while (tc-- > 0) {
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long ans=Long.MAX_VALUE;
ans= Math.min(((p-1)/a+1)*a-p,ans);
ans= Math.min(((p-1)/b+1)*b-p,ans);
ans= Math.min(((p-1)/c+1)*c-p,ans);
out.println(ans);
}
out.close();
}
/*
FASTREADER
*/
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/*DEFINED BY ME*/
//READING ARRAY
long[] readArray(int n) {
long arr[] = new long[n];
for (int i = 0; i < n; i++)
arr[i] = nextLong();
return arr;
}
//COLLECTIONS SORT
void sort(int arr[]) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : arr)
l.add(i);
Collections.sort(l);
for (int i = 0; i < arr.length; i++)
arr[i] = l.get(i);
}
//EUCLID'S GCD
int gcd(int a, int b) {
if (b != 0)
return gcd(b, a % b);
else
return a;
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
7a2a56e34063fb680fe97d79d7244089
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int testCases = Integer.parseInt(sc.nextLine());
for (int i = 0; i < testCases; i++) {
String entry = sc.nextLine();
String[] entryElements = entry.trim().split("\\s+");
long p = Long.parseLong(entryElements[0]);
long a = Long.parseLong(entryElements[1]);
long b = Long.parseLong(entryElements[2]);
long c = Long.parseLong(entryElements[3]);
long aMod = Math.floorMod(p, a);
long bMod = Math.floorMod(p, b);
long cMod = Math.floorMod(p, c);
long data = Math.min((a - (aMod == 0 ? a : aMod)) , (b - (bMod == 0 ? b : bMod)));
data = Math.min(data, c - (cMod == 0 ? c : cMod));
System.out.println(data);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
e20ae9b34640313bf746d979856d7084
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
public final class Main {
public static long ret(long a, long p) {
long e = p % a;
if(e > 0)
e = a - e;
return e;
}
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-- > 0) {
long p = scan.nextLong(), a = scan.nextLong(), b = scan.nextLong(), c = scan.nextLong();
long aret = ret(a, p), bret = ret(b, p), cret = ret(c, p);
System.out.println(Math.min(aret, Math.min(bret, cret)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
6e8fca58af6cf32f9c8d6ce9d23205b0
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Swim3 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
System.out.println(Math.min((a - p % a) % a, Math.min((b - p % b) % b, (c - p % c) % c)));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
4e1c7f712e874937a48aca07505e3f14
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0) {
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long ans=Math.min(a-p%a, Math.min(b-p%b, c-p%c));
if(p%a==0||p%b==0||p%c==0) System.out.println(0);
else System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
89636104f71a0bd0e856b1a65e2254df
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class A1492 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
long p = scanner.nextLong(), a = scanner.nextLong(),
b = scanner.nextLong(), c = scanner.nextLong();
long a1 = 0, b1 = 0, c1 = 0 ,min;
a1 = p % a;
b1 = p % b;
c1 = p % c;
if (a1==0 || b1==0 || c1==0){
System.out.println(0);
continue;
}else {
min = Math.min(Math.min(a - a1, b - b1), c - c1);
}
// System.out.println(a1 + " " + b1 + " " + c1);
// System.out.println(min);
System.out.println(min);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
03294a3152f0d05cce99ac8aed5c4476
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
LinkedList<Double> list = new LinkedList<>();
while(n>0)
{
long p,a,b,c;
p=scanner.nextLong();
a=scanner.nextLong();
b=scanner.nextLong();
c=scanner.nextLong();
if(p%a==0||p%b==0||p%c==0)
{
System.out.println(0);
}
else {
long cha=Math.min(a-p%a,b-p%b);
cha=Math.min(cha,c-p%c);
System.out.println(cha);
}
n--;
}
// for (int i = 0; i < list.size(); i++) {
// System.out.printf("%.0f\n",list.get(i));
// }
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
f79ba8ae01677afaa405517436f6742f
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
public class Thing{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int z = input.nextInt();
for(int j = 0; j < z; j++){
long p = input.nextLong();
long a = input.nextLong();
long b = input.nextLong();
long c = input.nextLong();
long ap = p%a;
long bp = p%b;
long cp = p%c;
while(ap > 0 && bp > 0 && cp >0){
ap -= a;
bp -= b;
cp -= c;
}
if(ap <= 0 && bp <= 0 && cp <=0){
System.out.println(Math.abs(Long.max(ap, Long.max(bp, cp))));
}else if(ap <= 0 && bp <= 0){
System.out.println(Math.abs(Long.max(ap, bp)));
}else if(bp <=0 && cp <= 0){
System.out.println(Math.abs(Long.max(bp, cp)));
}else if(cp <=0 && ap <=0){
System.out.println(Math.abs(Long.max(cp, ap)));
}else if(ap <= 0){
System.out.println(Math.abs(ap));
}else if(bp <= 0){
System.out.println(Math.abs(bp));
}else if(cp <= 0){
System.out.println(Math.abs(cp));
}
}
}
public static boolean isDivisible(String a, String b){
while(a.indexOf(b) == 0){
a = a.substring(b.length());
}
if(a.indexOf(b) == -1 && a.length() == 0){
return true;
}
return false;
}
public static String findThingy(String a, String b){
String p = "";
p += a;
while((!(isDivisible(p,b))) && (p.length() % b.length() != 0)){
p+=a;
}
if(isDivisible(p,b)){
return p;
}else{
return "n";
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
9b25b1c842624958e26cfd7eebf74b14
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
long p=sc.nextLong();
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long a1=(p+a-1)/a;
long b1=(p+b-1)/b;
long c1=(p+c-1)/c;
a1*=a;
b1*=b;
c1*=c;
long z=Math.min(a1-p, b1-p);
long res=Math.min(z,c1-p);
System.out.println(res);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
7889bb6f8672bbd7dd0448cc72132f2b
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
private static final int SWIMMERS = 3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long[] tab = new long[SWIMMERS];
long t = input.nextLong();
while (t-- > 0) {
long p = input.nextLong();
for (int i = 0; i < SWIMMERS; i++) {
tab[i] = input.nextLong();
}
System.out.println(subCase(p, tab));
}
}
private static long subCase(long p, long... swimmers) {
long min = Long.MAX_VALUE;
for (long swimmer : swimmers) {
min = Math.min((swimmer - (p % swimmer)) % swimmer, min);
}
return min;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
b3c990a5497556399635a726883e14a3
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
long p = in.nextLong();
long a = in.nextLong();
long b = in.nextLong();
long c = in.nextLong();
if ((p > a && p % a == 0) || (p > b && p % b == 0) || (p > c && p % c == 0)) {
System.out.println(0);
continue;
}
System.out.println((a == p || b == p || c == p) ? 0 : Math.min((a * ((p / a) + 1) - p), Math.min((b * ((p / b) + 1) - p), (c * ((p / c) + 1) - p))));
}
in.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
6dc363fdd4099d44005b0a62b9c4c720
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class ThreeSwimmers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
long p = scanner.nextLong();
long a = scanner.nextLong();
long b = scanner.nextLong();
long c = scanner.nextLong();
System.out.println(solve(p, a, b, c));
}
}
public static long solve(long p, long a, long b, long c) {
long min = 0;
if (p % a != 0 && p % b != 0 && p % c != 0) {
min = a - (p % a);
if (b - (p % b) < min)
min = b - (p % b);
if (c - (p % c) < min)
min = c - (p % c);
}
return min;
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
5d335c7fdf31c27b4f4942617620e62f
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sandip Jana
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
long p = in.nextLong();
long ans = Long.MAX_VALUE;
for (int i = 1; i <= 3; i++) {
long time = in.nextLong();
if (p % time == 0)
ans = Math.min(0, ans);
else {
long cost = ((p / time) * 1L * time);
ans = Math.min(ans, cost + time - p);
}
}
out.println(ans);
out.flush();
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
a12c17b611f7f1c86c40654554c51c01
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
long p = sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
int ans = 0;
if(p%a==0 || p%b==0 || p%c==0) System.out.println(ans);
else{
System.out.println(Math.min((a-p%a)%a, Math.min((b-p%b)%b, (c-p%c)%c)));
}
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
02b34f1d4a3d1ad7407ff2a9b65d406f
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class ThreeSwimmers {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int test = sc.nextInt();
long[] p = new long[test];
long[]a = new long[test];
long[]b = new long[test];
long[]c= new long[test];
long[]result=new long[test];
for (int i = 0; i < test; i++) {
p[i]=sc.nextLong();
a[i]=sc.nextLong();
b[i]=sc.nextLong();
c[i]=sc.nextLong();
result[i]=Math.min((a[i]-p[i]%a[i])%a[i],Math.min((b[i]-p[i]%b[i])%b[i],(c[i]-p[i]%c[i])%c[i]));
}
for(int i = 0 ; i <test;i++)
System.out.println(result[i]);
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
7e1c7966320d8b781cafd584e799096a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int tt=sc.nextInt();
for(int testcase=0;testcase<tt;testcase++){
long a=sc.nextLong();
long b=sc.nextLong();
long c=sc.nextLong();
long d=sc.nextLong();
long ans=(a+b-1)/b*b-a;
ans=Math.min(ans,(a+c-1)/c*c-a);
ans=Math.min((a+d-1)/d*d-a,ans);
System.out.println(ans);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
2ba0cfdd2eaa78f35f4cc384067fa8f6
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class ProbA {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int cases = Integer.parseInt(bf.readLine());
for(int x=0; x<cases; x++) {
st = new StringTokenizer(bf.readLine());
long p = Long.parseLong(st.nextToken());
long a = Long.parseLong(st.nextToken());
long b = Long.parseLong(st.nextToken());
long c = Long.parseLong(st.nextToken());
long na = (a*((p/a)+(p%a == 0 ? 0 : 1)));
long nb = (b*((p/b)+(p%b == 0 ? 0 : 1)));
long nc = (c*((p/c)+(p%c == 0 ? 0 : 1)));
System.out.println(Math.min(Math.min(na, nb), nc)-(long)p);
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
8aaad4edf43f317c141b1dac9d5cafe0
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class A {
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static class FastWriter extends PrintWriter {
FastWriter() {
super(System.out);
}
void println(int[] array) {
for (int i = 0; i < array.length; i++) {
print(array[i] + " ");
}
println();
}
void println(long[] array) {
for (int i = 0; i < array.length; i++) {
print(array[i] + " ");
}
println();
}
}
public static void main(String[] args){
FastScanner in = new FastScanner();
FastWriter out=new FastWriter();
int t=in.nextInt();
while(t-->0){
long p=in.nextLong();
long a=in.nextLong();
long b=in.nextLong();
long c=in.nextLong();
long a1=p/a;
if(p%a!=0){
a1++;
}
long b1=p/b;
if(p%b!=0){
b1++;
}
long c1=p/c;
if(p%c!=0){
c1++;
}
long min=Math.min(a1*a, Math.min(b1*b,c1*c));
out.println(min-p);
}
out.close();
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
1adb0da01f82bd4e40c6c46a90d6c800
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.util.Scanner;
public class Codeforces {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int t = kb.nextInt();
while(t-->0)
{
long p=kb.nextLong();
long a=kb.nextLong();
long b=kb.nextLong();
long c=kb.nextLong();
long ta=a*((p+a-1)/a)-p;
long tb=b*((p+b-1)/b)-p;
long tc=c*((p+c-1)/c)-p;
System.out.println(Math.min(Math.min(ta, tb), tc));
}
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
| |
PASSED
|
2895c3f36e5a4fdba6a5d61f01d8141a
|
train_109.jsonl
|
1614071100
|
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes.You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
|
512 megabytes
|
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
public class N704 {
static PrintWriter out;
static Scanner sc;
static ArrayList<Integer>q,w;
static ArrayList<Integer>adj[];
static HashSet<Integer>primesH;
static boolean prime[];
//static ArrayList<Integer>a;
static HashSet<Long>h,tmp;
static boolean[]v;
static int[]a,b,c,d;
static int[]l,dp;
static char[][]mp;
static int A,B,n,m;
public static void main(String[]args) throws IOException {
sc=new Scanner(System.in);
out=new PrintWriter(System.out);
A();
// B();
// C();
//D();
//minimum for subarrays of fixed length
//F();
out.close();
}
private static void A() throws IOException {
int t=ni();
while(t-->0) {
long p=nl(),a=nl(),b=nl(),c=nl();
long x1=(long)Math.ceil(p/(double)a);
long x2=(long)Math.ceil(p/(double)b);
long x3=(long)Math.ceil(p/(double)c);
// out.println(Math.min(Math.min(x1*1l*a-p, x2*1l*b-p), x3*1l*c-p));
out.println(Math.min(Math.min((a-p%a)%a, (b-p%b)%b), (c-p%c)%c));
}
}
static void B() throws IOException {
int t=ni();
while(t-->0) {
int n=ni();
a=nai(n);
Stack<Point>q=new Stack<Point>();
int cur=0;
for(int i=0;i<n;i++) {
int piv=a[i];
cur=i;
i++;
while(i<n&&a[i]<piv) {
i++;
}
i--;
q.add(new Point(cur,i));
}
while(!q.isEmpty()) {
Point p=q.pop();
for(int i=p.x;i<=p.y;i++) {
out.print(a[i]+" ");
}
}
out.println();
}
}
static void C() throws IOException{
n=ni();
m=ni();
String s=sc.next();
String t=sc.next();
Point[]occ=new Point[27];
for(int i=0;i<27;i++) {
occ[i]=new Point(n,-1);
}
for(int i=0;i<n;i++) {
int idx=s.charAt(i)-'a';
Point p=occ[idx];
occ[idx]=new Point(Math.min(p.x, i),Math.max(p.y, i));
}
int max=0;
for(int i=0;i<m-1;i++) {
int cur=t.charAt(i)-'a';
int nxt=t.charAt(i+1)-'a';
// if(i+2==m||occ[])
max=Math.max(occ[nxt].x-occ[cur].x, max);
}
for(int i=0;i<m-1;i++) {
int cur=t.charAt(i)-'a';
int nxt=t.charAt(i+1)-'a';
}
out.println(max);
}
static void D() throws IOException {
int n=ni();
}
static void E() throws IOException {
int t=ni();
while(t-->0) {
}
}
static void X() throws IOException {
int n=ni();
a=new int[n];
int lo=0,hi=n-1;
int locIdx=-1;
while(lo<=hi) {
int mid=(lo+hi)/2;
ol("? "+(mid+1));
out.flush();
int m=ni(),bm=-1,am=-1;
a[mid]=m;
if(mid>0) {
if(a[mid-1]==0) {
ol("? "+(mid));
out.flush();
bm=ni();
}else {
bm=a[mid-1];
}
a[mid-1]=bm;
if(bm<m) {
hi=mid-1;
continue;
}
}
if(mid<n-1) {
if(a[mid+1]==0) {
ol("? "+(mid+2));
out.flush();
am=ni();
}else {
am=a[mid+1];
}
a[mid+1]=am;
if(am<m) {
lo=mid+1;
continue;
}
}
locIdx=mid;break;
}
out.println("! "+(locIdx+1));
out.flush();
}
private static void disp(int[] revl) {
for(int i=0;i<revl.length;i++) {
out.print(revl[i]+" ");
}
out.println();
}
static class Pair implements Comparable<Pair>{
int a,b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
@Override
public int compareTo(Pair p) {
return a-p.a;
}
public boolean equals(Pair p) {
return a==p.a&&b==p.b;
}
public String toString() {
return "("+a+" "+b+")";
}
}
static int ni() throws IOException {
return sc.nextInt();
}
static double nd() throws IOException {
return sc.nextDouble();
}
static long nl() throws IOException {
return sc.nextLong();
}
static String ns() throws IOException {
return sc.next();
}
static int[] nai(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
return a;
}
static long[] nal(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextLong();
return a;
}
static int[][] nmi(int n,int m) throws IOException{
int[][]a=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextInt();
}
}
return a;
}
static long[][] nml(int n,int m) throws IOException{
long[][]a=new long[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j]=sc.nextLong();
}
}
return a;
}
static void o(String x) {
out.print(x);
}
static void ol(String x) {
out.println(x);
}
static void ol(int x) {
out.println(x);
}
static void disp1(int []a) {
for(int i=0;i<a.length;i++) {
out.print(a[i]+" ");
}
out.println();
}
static void disp2(Pair []a) {
for(int i=0;i<a.length;i++) {
out.print(a[i]+" ");
}
out.println();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
|
Java
|
["4\n9 5 4 8\n2 6 10 9\n10 2 5 10\n10 9 9 9"]
|
1 second
|
["1\n4\n0\n8"]
|
NoteIn the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, \ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, \ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, \ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side.In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, \ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
|
Java 8
|
standard input
|
[
"math"
] |
293f9b996eee9f76d4bfdeda85685baa
|
The first line of the input contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \leq p, a, b, c \leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
| 800
|
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.