10000 Added solution of Yandex Contest/Training on algorithms 1.0/Lesson 1/… · Tamada4a/Algorithms@9c5576c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c5576c

Browse files
committed
Added solution of Yandex Contest/Training on algorithms 1.0/Lesson 1/E. Ambulance
1 parent 2a62e5c commit 9c5576c

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.io.BufferedReader;
2+
import java.io.FileReader;
3+
import java.io.IOException;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
int k1 = -1, m = -1, k2 = -1, p2 = -1, n2 = -1;
8+
9+
// Reading the data
10+
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
11+
String line;
12+
while ((line = reader.readLine()) != null) {
13+
String[] split = line.split(" ");
14+
k1 = Integer.parseInt(split[0]);
15+
m = Integer.parseInt(split[1]);
16+
k2 = Integer.parseInt(split[2]);
17+
p2 = Integer.parseInt(split[3]);
18+
n2 = Integer.parseInt(split[4]);
19+
}
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
24+
// We are going through all possible options
25+
int p1 = -1, n1 = -1;
26+
for (int maxFlat = 1; maxFlat < 10e6 + 1; ++maxFlat) {
27+
if (!isCorrectMaxFlat(k2, p2, m, n2, maxFlat)) {
28+
continue;
29+
}
30+
31+
// We get the correct data for this stage
32+
int entrance = getEntrance(k1, m, maxFlat);
33+
int floor = getFloor(k1, m, maxFlat);
34+
35+
// If this is the first change
36+
if (p1 == -1) {
37+
p1 = entrance;
38+
n1 = floor;
39+
}
40+
// If it is impossible to determine the entrance
41+
else if (p1 != entrance && p1 != 0) {
42+
p1 = 0;
43+
}
44+
// If it is impossible to determine the floor
45+
else if (n1 != floor && n1 != 0) {
46+
n1 = 0;
47+
}
48+
}
49+
50+
System.out.print(p1 + " " + n1);
51+
}
52+
53+
// Check whether the given number of apartments per floor is correct
54+
private static boolean isCorrectMaxFlat(int k, int p, int m, int n, int maxFlat) {
55+
return getEntrance(k, m, maxFlat) == p && getFloor(k, m, maxFlat) == n;
56+
}
57+
58+
// Getting an entrance
59+
private static int getEntrance(int k, int m, int maxFlat) {
60+
return (int) Math.ceil((double) k / (maxFlat * m));
61+
}
62+
63+
// Getting a floor
64+
private static int getFloor(int k, int m, int maxFlat) {
65+
int p = getEntrance(k, m, maxFlat);
66+
return (int) Math.ceil((double) k / maxFlat - ((p - 1) * m));
67+
}
68+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# E. Ambulance
2+
3+
<table>
4+
<tr>
5+
<td>Time limit</td>
6+
<td>1 second</td>
7+
</tr>
8+
<tr>
9+
<td>Memory limit</td>
10+
<td>64Mb</td>
11+
</tr>
12+
<tr>
13+
<td>Input</td>
14+
<td>standart input or input.txt</td>
15+
</tr>
16+
<tr>
17+
<td>Output</td>
18+
<td>standart output or output.txt</td>
19+
</tr>
20+
</table>
21+
22+
An ambulance crew went on call to one of the remote areas. Unfortunately, when the dispatcher received the call, he managed to write down only the address of the house and the apartment
23+
number *K<sub>1</sub>*, and then the connection was interrupted. However, he remembered that at the same address of the house some time ago, an ambulance went to the apartment *K<sub>2</sub>*,
24+
which is located in the entrance *P<sub>2</sub>* on the floor *N<sub>2</sub>*. It is known that the house has *M* floors and the number of apartments on each landing is the same. Write a
25+
program that calculates the entrance number *P<sub>1</sub>* and the floor number *P<sub>1</sub>* of the apartment *K<sub>1</sub>*.
26+
27+
## Input format
28+
The input file contains five positive integers *K<sub>1</sub>*, *M*, *K<sub>2</sub>*, *P<sub>2</sub>*, *N<sub>2</sub>*. All numbers do not exceed 10<sup>6</sup>.
29+
30+
## Output format
31+
Print two numbers *P<sub>1</sub>* and *N<sub>1</sub>*. If the input data does not allow you to uniquely identify *P<sub>1</sub>* or *N<sub>1</sub>*, type 0 instead of the corresponding number.
32+
If the input data is inconsistent, print two -1 numbers (minus one).
33+
34+
## Example 1
35+
<table>
36+
<thead>
37+
<tr>
38+
<th align= "left">Input</th>
39+
<th align= "left">Output</th>
40+
</tr>
41+
</thead>
42+
<tbody>
43+
<tr>
44+
<td>
45+
89 20 41 1 11
46+
</td>
47+
<td>
48+
2 3
49+
</br>
50+
</td>
51+
</tr>
52+
</tbody>
53+
</table>
54+
55+
## Example 2
56+
<table>
57+
<thead>
58+
<tr>
59+
<th align= "left">Input</th>
60+
<th align= "left">Output</th>
61+
</tr>
62+
</thead>
63+
<tbody>
64+
<tr>
65+
<td>
66+
11 1 1 1 1
67+
</td>
68+
<td>
69+
0 1
70+
</br>
71+
</td>
72+
</tr>
73+
</tbody>
74+
</table>
75+
76+
## Example 3
77+
<table>
78+
<thead>
79+
<tr>
80+
<th align= "left">Input</th>
81+
<th align= "left">Output</th>
82+
</tr>
83+
</thead>
84+
<tbody>
85+
<tr>
86+
<td>
87+
3 2 2 2 1
88+
</td>
89+
<td>
90+
-1 -1
91+
</br>
92+
</td>
93+
</tr>
94+
</tbody>
95+
</table>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# E. Скорая помощь
2+
3+
<table>
4+
<tr>
5+
<td>Ограничение времени</td>
6+
<td>1 секунда</td>
7+
</tr>
8+
<tr>
9+
<td>Ограничение памяти</td>
10+
<td>64Mb</td>
11+
</tr>
12+
<tr>
13+
<td>Ввод</td>
14+
<td>стандартный ввод или input.txt</td>
15+
</tr>
16+
<tr>
17+
<td>Вывод</td>
18+
<td>стандартный вывод или output.txt</td>
19+
</tr>
20+
</table>
21+
22+
Бригада скорой помощи выехала по вызову в один из отдаленных районов. К сожалению, когда диспетчер получил вызов, он успел записать только адрес дома и номер квартиры *K<sub>1</sub>*,
23+
а затем связь прервалась. Однако он вспомнил, что по этому же адресу дома некоторое время назад скорая помощь выезжала в квартиру *K<sub>2</sub>*, которая расположена в подъезда *P<sub>2</sub>* на
24+
этаже *N<sub>2</sub>*. Известно, что в доме *M* этажей и количество квартир на каждой лестничной площадке одинаково. Напишите программу, которая вычисляет номер подъезда *P<sub>1</sub>* и номер
25+
этажа *P<sub>1</sub>* квартиры *K<sub>1</sub>*.
26+
27+
## Формат ввода
28+
Во входном файле записаны пять положительных целых чисел *K<sub>1</sub>*, *M*, *K<sub>2</sub>*, *P<sub>2</sub>*, *N<sub>2</sub>*. Все числа не превосходят 10<sup>6</sup>.
29+
30+
## Формат вывода
31+
Выведите два числа *P<sub>1</sub>* и *N<sub>1</sub>*. Если входные данные не позволяют однозначно определить *P<sub>1</sub>* или *N<sub>1</sub>*, вместо соответствующего числа напечатайте 0.
32+
Если входные данные противоречивы, напечатайте два числа –1 (минус один).
33+
34+
## Пример 1
35+
<table>
36+
<thead>
37+
<tr>
38+
<th align= "left">Ввод</th>
39+
<th align= "left">Вывод</th>
40+
</tr>
41+
</thead>
42+
<tbody>
43+
<tr>
44+
<td>
45+
89 20 41 1 11
46+
</td>
47+
<td>
48+
2 3
49+
</br>
50+
</td>
51+
</tr>
52+
</tbody>
53+
</table>
54+
55+
## Пример 2
56+
<table>
57+
<thead>
58+
<tr>
59+
<th align= "left">Ввод</th>
60+
<th align= "left">Вывод</th>
61+
</tr>
62+
</thead>
63+
<tbody>
64+
<tr>
65+
<td>
66+
11 1 1 1 1
67+
</td>
68+
<td>
69+
0 1
70+
</br>
71+
</td>
72+
</tr>
73+
</tbody>
74+
</table>
75+
76+
## Пример 3
77< 9865 code class="diff-text syntax-highlighted-line addition">+
<table>
78+
<thead>
79+
<tr>
80+
<th align= "left">Ввод</th>
81+
<th align= "left">Вывод</th>
82+
</tr>
83+
</thead>
84+
<tbody>
85+
<tr>
86+
<td>
87+
3 2 2 2 1
88+
</td>
89+
<td>
90+
-1 -1
91+
</br>
92+
</td>
93+
</tr>
94+
</tbody>
95+
</table>

0 commit comments

Comments
 (0)
0