# 2A.algo - Puzzles algorithmes (2) - correction¶

Eléments de réponses pour des puzzles algorithmiques tirés de Google Code Jam et autres sites équivalents, nombres premiers, écoulement d’eau, séparation des bagarreurs, formation de binômes.

```from jyquickhelper import add_notebook_menu
```

## Exercice 1 : nombres premiers¶

CodeForces 483A

On propose la conjecture suivante :

Si sont premiers entre eux et si sont premiers entre eux alors sont premiers entre eux.

On veut montrer que cette conjecture est fausse. Pour cela, il faut trouver trois entiers pour lesquels l’énoncé est faux et on les cherche tels qu’il existe deux entiers :

### Première idée¶

On peut chercher de la forme . On peut trouver tel que , soient premiers eux. ne le seront pas. Exemple : 4,5,6.

## Exercice 2 : watersheds¶

Google Code Jam 2009

Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.

Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.

• From each cell, water flows down to at most one of its 4 neighboring cells.

• For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell’s, then the water does not flow, and the current cell is called a sink.

• Otherwise, water flows from the current cell to the neighbor with the lowest altitude.

• In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.

Every cell that drains directly or indirectly to the same sink is part of the same drainage basin. Each basin is labeled by a unique lower-case letter, in such a way that, when the rows of the map are concatenated from top to bottom, the resulting string is lexicographically smallest. (In particular, the basin of the most North-Western cell is always labeled ‘a’.)

## Exercice 3 : séparation des bagarreurs¶

Google Code Jam 2014

As the leader of the Evil League of Evil, Bad Horse has a lot of problems to deal with. Most recently, there have been far too many arguments and far too much backstabbing in the League, so much so that Bad Horse has decided to split the league into two departments in order to separate troublesome members. Being the Thoroughbred of Sin, Bad Horse isn’t about to spend his valuable time figuring out how to split the League members by himself. That what he’s got you – his loyal henchman – for.

## Exercice 4 : formation de binômes¶

UVa 10911

You have been given the job of forming the quiz teams for the next ‘MCA CPCI Quiz Championship’. There are students interested to participate and you have to form teams, each team consisting of two members. Since the members have to practice together, all the students want their member’s house as near as possible. Let be the distance between the houses of group 1, be the distance between the houses of group 2 and so on. You have to make sure the summation () is minimized.

### solution¶

L’ordre dans lequel l’appariement sont envisagés n’a pas d’importance (contrairement à l’appariement de deux séquences d’ADN). Alors, une direction possible aurait été un algorithme du type distance de Levenstein.

Dans ce problème est un problème d’appariment ou de couplage dans un graphe bi-parti. Plusieurs algorithmes approchés existent pour résoudre ce problème :

L’algorithme optimal retourne la solution optimale en un temps polynômial : Blossom algorithm.