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 :The Loneliness of the Long Distance Runner
For the Cross-Country event, the organising committee has installed a series of radio-frequency gates throughout the race. These enabled the detection of runners passing through and the live updating of the rankings. However, not all the gates have the same range and some have been placed far too close together, so that their emission areas overlap. This overlap can lead to some interference between gates and gives false results.
One way of solving the problem is to assign different radio frequencies to two gates that are too close together. However, the tags placed on the athletes can only react to a limited number of frequencies and more sophisticated ones are expensive. To minimise the costs of organising the race, determine the minimum number of frequencies so that no two gates have the same frequency as those close to them.
Data
Input
Line 1 : Two integers
PandI, representing respectively the number of gates and the number of pairs of gates sufficiently close together to cause interferences.Line 2 to I+1 : Two integers
AandB, representing the identifiers of two interfering gates.The integer
Pis between0and10000included.The integer
Iis betweenP-1and(P*P-P)/2included.For each line, both integers
AandBare between0andP-1and such that `AIf
Ais interfering withB, thenBis interfering withA.The whole race course is covered by the gates and each gate covers a single continuous section of the race.
We consider that the trajectory of the race is strictly straight and that the emission zone of the gates are circular. Thus, no interference can be due to the sinuosity of the path of the race.
Output
The minimum number of frequencies that can be affected to the gates such that no two distinct gates can interfere.
- Assistant
from sys import stdin, stdout from collections import defaultdict from typing import List, Tuple def minimum_frequencies(P: int, I: int, pairs: List[Tuple[int, int]]) -> int: # Création d'un graphe pour représenter les interférences entre les portes graph = defaultdict(list) for A, B in pairs: graph[A].append(B) graph[B].append(A) # Tableau pour stocker les couleurs (fréquences) assignées aux portes colors = [-1] * P # Fonction pour colorier le graphe def greedy_coloring() -> int: for node in range(P): # Vérifier les couleurs des voisins neighbor_colors = set() for neighbor in graph[node]: if colors[neighbor] != -1: neighbor_colors.add(colors[neighbor]) # Attribuer la couleur la plus basse possible color = 0 while color in neighbor_colors: color += 1 colors[node] = color # Retourner le nombre maximum de couleurs utilisées + 1 return max(colors) + 1 # Appel de la fonction de coloriage return greedy_coloring() # Lecture des données d'entrée input_data = stdin.read().strip().splitlines() P, I = map(int, input_data[0].split()) pairs = [tuple(map(int, line.split())) for line in input_data[1:I + 1]] # Calcul et affichage du résultat result = minimum_frequencies(P, I, pairs) stdout.write(f"{result}\n")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.