8000 Added solution of Tinkoff Education/The Eternal Contest/3 task · Tamada4a/Algorithms@0a6a1e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a6a1e2

Browse files
committed
Added solution of Tinkoff Education/The Eternal Contest/3 task
1 parent 5fecb10 commit 0a6a1e2

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.stream.Collectors;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
// The corresponding value will be stored in this variable
11+
int n = Integer.MAX_VALUE, t = Integer.MAX_VALUE, empIdx = Integer.MAX_VALUE;
12+
13+
ArrayList<Integer> empFloors = new ArrayList<>();
14+
15+
// Reading data
16+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
17+
String[] split = reader.readLine().split(" ");
18+
n = Integer.parseInt(split[0]);
19+
t = Integer.parseInt(split[1]);
20+
21+
empFloors = Arrays.stream(reader.readLine().split(" "))
22+
.map(Integer::parseInt)
23+
.collect(Collectors.toCollection(ArrayList::new));
24+
25+
empIdx = Integer.parseInt(reader.readLine());
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
30+
// Find the time from the first floor to the one we need
31+
int left = empFloors.get(empIdx - 1) - empFloors.get(0);
32+
33+
// Find the time from the top floor to the one we need
34+
int right = empFloors.get(n - 1) - empFloors.get(empIdx - 1);
35+
36+
// Find the time from the top floor to the bottom. This is the time in which all floors will be
37+
// completed if the required employee is on site at time t
38+
int maxDif = empFloors.get(n - 1) - empFloors.get(0);
39+
40+
// If the employee does not leave
41+
if (left <= t || right <= t)
42+
System.out.print(maxDif);
43+
// Otherwise, we go up to the floor of the right employee and from this floor we bypass all
44+
// others floors
45+
else
46+
System.out.print(Math.min(left, right) + maxDif);
47+
}
48+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 3 task
2+
<table>
3+
<tr>
4+
<td>Time limit</td>
5+
<td>1 second</td>
6+
</tr>
7+
<tr>
8+
<td>Memory limit</td>
9+
<td>256 MB</td>
10+
</tr>
11+
</table>
12+
13+
Katya has a busy day at work. She needs to transfer *n* different contracts to her colleagues. All meetings take place on different floors, and
14+
you can only move between floors by flights of stairs — it is believed that this improves the physical fitness of employees. The passage of each
15+
flight takes exactly 1 minute.
16+
17+
Katya is currently on the parking floor, planning her route. Colleagues can be visited in any order, but one of them will leave the office in *t*
18+
minutes. There are no stairs from the parking floor — only an elevator that can take you to any floor.
19+
20+
As a result, Katya's plan is as follows:
21+
- Take the elevator to an arbitrary floor. It is considered that the elevator rises to any floor in 0 minutes.
22+
- Transfer contracts to all colleagues by moving between floors up the stairs. It is believed that contracts on the floor are transferred instantly.
23+
- In the first *t* minutes, transfer the contract to the colleague who plans to leave.
24+
- Go through the minimum number of flights of stairs.
25+
26+
Help Katya complete all the points of her plan.
27+
28+
## Input data format
29+
The first line contains positive integers *n* and *t* (2 ≤ *n*, *t* ≤ 100) — the number of employees and the time when one of the employees will
30+
leave the office (in minutes). In the next line of *n* numbers are the numbers of the floors where the employees are located. All numbers are
31+
different and do not exceed 100 in absolute value. The floor numbers are given in ascending order. The next line contains the number of the
32+
employee who will leave in *t* minutes.
33+
34+
## Output data format
35+
Print one number — the minimum possible number of flights of stairs that Katya will need to go through.
36+
37+
## Remark
38+
In the first example, there is enough time for Katya to go up the floors in order.
39+
40+
In the second example, Katya will need to go up to the outgoing employee, and then go through all the others — for example, in the order
41+
{1,2,3,4,6}.
42+
43+
## Examples of data
44+
45+
### Example 1
46+
<table>
47+
<thead>
48+
<tr>
49+
<th align= "left">Input</th>
50+
<th align= "left">Output</th>
51+
</tr>
52+
</thead>
53+
<tbody>
54+
<tr>
55+
<td>
56+
5 5</br>
57+
1 4 9 16 25</br>
58+
2
59+
</td>
60+
<td>
61+
24
62+
</td>
63+
</tr>
64+
</tbody>
65+
</table>
66+
67+
### Example 2
68+
<table>
69+
<thead>
70+
<tr>
71+
<th align= "left">Input</th>
72+
<th align= "left">Output</th>
73+
</tr>
74+
</thead>
75+
<tbody>
76+
<tr>
77+
<td>
78+
6 4</br>
79+
1 2 3 6 8 25</br>
80+
5
81+
</td>
82+
<td>
83+
31
84+
</td>
85+
</tr>
86+
</tbody>
87+
</table>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 3 задание
2+
<table>
3+
<tr>
4+
<td>Ограничение времени</td>
5+
<td>1 секунда</td>
6+
</tr>
7+
<tr>
8+
<td>Ограничение памяти</td>
9+
<td>256 МБ</td>
10+
</tr>
11+
</table>
12+
13+
У Кати насыщенный день на работе. Ей надо передать *n* разных договоров коллегам. Все встречи происходят на разных этажах, а между этажами можно
14+
перемещаться только по лестничным пролетам — считается, что это улучшает физическую форму сотрудников. Прохождение каждого пролета занимает ровно
15+
1 минуту.
16+
17+
Сейчас Катя на парковочном этаже, планирует свой маршрут. Коллег можно посетить в любом порядке, но один из них покинет офис через *t* минут.
18+
С парковочного этажа лестницы нет — только лифт, на котором можно подняться на любой этаж.
19+
20+
В итоге план Кати следующий:
21+
- Подняться на лифте на произвольный этаж. Считается, что лифт поднимается на любой этаж за 0 минут.
22+
- Передать всем коллегам договоры, перемещаясь между этажами по лестнице. Считается, что договоры на этаже передаются мгновенно.
23+
- В первые *t* минут передать договор тому коллеге, который планирует уйти.
24+
- Пройти минимальное количество лестничных пролетов.
25+
26+
Помогите Кате выполнить все пункты ее плана.
27+
28+
## Формат входных данных
29+
В первой строке вводятся целые положительные числа *n* и *t* (2 ≤ *n*, *t* ≤ 100) — количество сотрудников и время, когда один из сотрудников покинет
30+
офис (в минутах). В следующей строке *n* чисел — номера этажей, на которых находятся сотрудники. Все числа различны и по абсолютной величине не
31+
превосходят 100. Номера этажей даны в порядке возрастания. В следующей строке записан номер сотрудника, который уйдет через *t* минут.
32+
33+
## Формат выходных данных
34+
Выведите одно число — минимально возможное число лестничных пролетов, которое понадобится пройти Кате.
35+
36+
## Замечение
37+
В первом примере времени достаточно, чтобы Катя поднялась по этажам по порядку.
38+
39+
Во втором примере Кате понадобится подняться к уходящему сотруднику, а потом пройти всех остальных — например, в порядке {1,2,3,4,6}.
40+
41+
## Примеры данных
42+
43+
### Пример 1
44+
<table>
45+
<thead>
46+
<tr>
47+
<th align= "left">Ввод</th>
48+
<th align= "left">Вывод</th>
49+
</tr>
50+
</thead>
51+
<tbody>
52+
<tr>
53+
<td>
54+
5 5</br>
55+
1 4 9 16 25</br>
56+
2
57+
</td>
58+
<td>
59+
24
60+
</td>
61+
</tr>
62+
</tbody>
63+
</table>
64+
65+
### Пример 2
66+
<table>
67+
<thead>
68+
<tr>
69+
<th align= "left">Ввод</th>
70+
<th align= "left">Вывод</th>
71+
</tr>
72+
</thead>
73+
<tbody>
74+
<tr>
75+
<td>
76+
6 4</br>
77+
1 2 3 6 8 25</br>
78+
5
79+
</td>
80+
<td>
81+
31
82+
</td>
83+
</tr>
84+
</tbody>
85+
</table>

0 commit comments

Comments
 (0)
0