Skip to content

Instantly share code, notes, and snippets.

@brickgao
Created October 10, 2012 10:07
Show Gist options
  • Save brickgao/3864526 to your computer and use it in GitHub Desktop.
Save brickgao/3864526 to your computer and use it in GitHub Desktop.
Codeforces Round 142 (Div. 2) C
//By Brickgao
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int n, m;
int rec[10005], sum[10005];
int main()
{
bool flag = true;
int l, r, t;
cin >> n >> m;
for(int i = 0; i < n && flag; i++)
{
t = 1;
getchar();
for(int j = 0; j < m; j++)
{
if(getchar() == '1')
rec[t++] = j;
}
if(t == 1)
flag = false;
else
{
int num = 0;
rec[0] = rec[t - 1] - m;
rec[t] = rec[1] + m;
for(int j = 0; j < m ; j++)
{
l = rec[num + 1] - j;
r = j - rec[num];
if(l == 0)
num++;
sum[j] += min(l, r);
}
}
}
if(flag)
printf("%d\n", *min_element(sum,sum+m));
else
printf("-1\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment