Answer the question
In order to leave comments, you need to log in
How to solve this problem (USE Informatics 2021)?
Task:
The system administrator creates an archive of user files once a week. However, the size of the disk where it places the archive may be less than the total size of the archived files. It is known how much space each user's file occupies.
Given the information about the size of user files and the free space on the archive disk, determine the maximum number of users whose files can be archived, as well as the maximum size of an existing file that can be stored in the archive, provided that the files of the maximum possible number of users are saved.
Input data:
The first line of the input file contains two numbers: S is the amount of free disk space (a natural number not exceeding 10,000) and N is the number of users (a natural number not exceeding 2000). The next N lines contain the values of each user's file sizes (all natural numbers not exceeding 100), each in a separate line.
Write down two numbers in your answer: first, the largest number of users whose files can be archived, then the maximum size of the existing file that can be archived, provided that the files of the maximum possible number of users are stored.
Input file example:
100 4
80
30
50
40
With this initial data, you can save the files of a maximum of two users. The possible sizes of these two files are 30 and 40, 30 and 50, or 40 and 50. The largest file size of the listed pairs is 50, so the answer for the example given is:
2 50
All I had the strength to do was find the maximum number of users:
f = open("zadanie26_var_1.txt")
s = list(f)
k = 0
for i in range(int(s[1]), len(s)):
if s[i] + s[i+1] <= s[0]:
k+=1
print(k)
Answer the question
In order to leave comments, you need to log in
It is forbidden to post puzzles here, but you suggested some code, although it was incorrect, so you deserve a hint. I would advise you not to write the code first, but to speculate about how you are going to solve the problem. Come up with an algorithm first in Russian in free form. You have no experience, so the programming language distracts you too much from the essence of the task.
There is such a classic task "packing a backpack". Indivisible objects and their masses are given there. The backpack has a maximum allowable weight. All items will not fit into the backpack, but you need to choose such a set in order to load the backpack as much as possible. This task is computationally difficult for a large number of items. It is solved by complete enumeration.
Your task is much easier. You need to put as many items as possible from the available items in such a backpack so as not to exceed the maximum mass (in your case, the volume of the backup disk).
Here you have a large set of different weights, and you need to place the maximum number of weights on the scales so as not to exceed some weight. How will you choose weights?
Here is a hint for you. Think, decide, ask specific questions and you will be given more clues. But I hope they won't give you a ready-made solution here. We need a worthy replacement, and not binary ones, for which uncles solve problems.
Let me refactor your code. It won't make him right, but he will get better.
with open("zadanie26_var_1.txt") as f: # Так открытый файл будет закрыт после считывания
# Так вы считаете первые два числа в отдельные переменные:
total_volume, users_count = map(int, f.readline().strip().split())
# А так считаете остальные строки в список и нечаянные пустые строки вас не смутят:
user_sizes = [int(line.strip()) for line in f if line]
# Хорошо бы вынести логику решения в отдельную функцию:
def solve(s, n, lst):
# Вот так можно сделать проверки корректности (ну чисто для себя) входных данных:
assert s <= 10000, 'Invalid total volume'
assert len(lst) == n, 'Users count is not valid.'
assert n <= 2000, 'Wrong users count.'
assert all(0 < x <= 100 for x in lst), 'Invalid user size.'
# Вот здесь вот вы сделаете нужные вычисления ;)
backup_users_count = ...
max_backup_file_size = ...
return backup_users_count, max_backup_file_size
# Ну а вызвать функцию и напечатать ее результат вы тоже сумеете, правда?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question