Serie: Algorithms
c
47 righe
· Aggiornato 2026-02-09
flip_coins.c
Algorithms/flip_coins.c
#include <stdio.h>
#include <stdlib.h>
int max (int a, int b) { return a > b ? a : b;}
int main() {
int n, m;
char grid[105][15];
while (scanf("%d %d", &n, &m) == 2)
{
for (int i = 0; i < n; i++)
scanf("%s", grid[i]);
int best = 0;
// Enumerate column flips mask
for (int mask = 0; mask < (1 << m); mask++)
{
int total = 0;
for (int i = 0; i < n; i++)
{
int ones = 0;
for (int j = 0; j < m; j++)
{
int bit = grid[i][j] - '0';
// If this column flipped
if (mask&(1<<j)) bit ^= 1;
if (bit == 1) ones++;
}
// We can flip this row to have ones = m - ones
total += max(ones, m - ones);
}
if (total > best) best = total;
}
printf("%d\n", best);
}
return 0;
}
Articoli correlati
Algorithms
java
Aggiornato 2026-03-02
#include <iostream>.java
#include <iostream>.java — java source code from the Algorithms learning materials (Algorithms/#include <iostream>.java).
Leggi l'articolo →
Algorithms
cpp
Aggiornato 2026-04-07
748.cpp
748.cpp — cpp source code from the Algorithms learning materials (Algorithms/748.cpp).
Leggi l'articolo →
Algorithms
cpp
Aggiornato 2026-04-07
827.cpp
827.cpp — cpp source code from the Algorithms learning materials (Algorithms/827.cpp).
Leggi l'articolo →
Algorithms
cpp
Aggiornato 2026-04-07
827_best_greedy.cpp
827_best_greedy.cpp — cpp source code from the Algorithms learning materials (Algorithms/827_best_greedy.cpp).
Leggi l'articolo →
Algorithms
cpp
Aggiornato 2026-04-07
8402.cpp
8402.cpp — cpp source code from the Algorithms learning materials (Algorithms/8402.cpp).
Leggi l'articolo →
Algorithms
cpp
Aggiornato 2026-04-07
860.cpp
860.cpp — cpp source code from the Algorithms learning materials (Algorithms/860.cpp).
Leggi l'articolo →