affiche juste le code, pas d'explications !
- User
System: affiche juste le code, pas d'explications ! User: Tu es un développeur python3 expérimenté, et tu dois concevoir un programme en python (avec des commentaires en français, sans utiliser de librairies externes, sans
if __name__=="__main__":, avec type hints, et en privilégiant les algorythmes a complexité constante (O(1)) et/ou complexité linéaire (O(n))). Le programme va recevoir les données en entrée sur la stdin, et doit répondre sur la stdout. Fabrique moi un programme python qui résout le problème (énoncé en markdown) suivant :Operation Stealth
In preparation for your raid on the hideout of a chrono-criminal gang, you need to call in teams of mercenaries to ensure that you have enough manpower.
But be careful: you can't recruit everyone, as this would attract the attention of the criminals, and you would also risk blowing the budget of the brigade! So you'll have to play it smart, by choosing the mercenary teams you'll contact.
To prevent your opponents from surprising you by calling in mercenaries, you want to have strictly more than half of the available fighters. This way, even if the criminals recruit all the remaining available mercenaries, you will still be outnumbering them. The mercenaries are organized into groups, and you are trying to figure out which groups you should recruit.
Your brigade already has some officers for the raid, so you can start with a solid base of manpower. How many mercenaries at least will you need to hire to ensure you have the advantage during the raid?
Data
Input
Line 1: an integer N between 0 and 100,000 representing the number of available officers in your brigade.
Line 2: an integer M between 0 and 10,000 representing the number of mercenary teams available.
Next M lines: M integers between 0 and 1,000 representing the size of each mercenary group.
Output
Your program must output the minimum number of mercenaries you need to recruit in order to ensure you will be in majority.
Example
247 7 178 168 66 158 60 113 227You have 247 officers, to which you can add 385 mercenaries (227 + 158) for a total of 632 people on your side.
In this case, the criminals will be able to recruit at most the remaining 585 mercenaries (178 + 168 + 66 + 60 + 113), which puts you in numerical superiority.
385 is the minimum number of recruits that will put you in the majority, so your code should output
385. Assistant: - Assistant
def calculate_min_mercenaries(n: int, teams: list[int]) -> int: # Nombre total d'hommes si tous les mercenaires étaient recrutés. total_fighters = n + sum(teams) # Nombre d'hommes nécessaires pour être majoritaire. majority_required = total_fighters // 2 + 1 # Trier les équipes de mercenaires par taille décroissante. teams.sort(reverse=True) current_strength = n hired_mercenaries = 0 # Recruter juste assez pour dépasser le seuil de majorité. for team_size in teams: if current_strength >= majority_required: break current_strength += team_size hired_mercenaries += team_size return hired_mercenaries def main(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) m = int(data[1]) teams = list(map(int, data[2:2 + m])) result = calculate_min_mercenaries(n, teams) print(result) main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.