Codeforces 230C Shifts【思维】

C. Shifts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right.

To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100".

Determine the minimum number of moves needed to make some table column consist only of numbers 1.

Input

The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table.

It is guaranteed that the description of the table contains no other characters besides "0" and "1".

Output

Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1.

Examples
input
3 6
101010
000100
100000
output
3
input
2 3
111
000
output
-1
Note

In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s.

In the second sample one can't shift the rows to get a column containing only 1s.


题目大意:


每一行都可以将其循环挪位一下,问最少挪动多少下,能够使得某一列都是1.


思路:

①我们预处理出L【i】【j】表示第i行从位子j到最左边,距离j点最近的那个1的位子。

同理有R【i】【j】;


②然后我们再预处理出LL【i】表示第i行最左边的1的位子。

同理有RR【i】;


③那么我们O(m)枚举一列,表示最终这一列都是1,然后我们贪心的判断一下哪个1走过来最优即可。

过程维护一下。


Ac代码:

#include
#include
#include
#include
using namespace std;
char a[150][15000];
int L[150][15000];
int R[150][15000];
int LL[150];
int RR[150];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(LL,-1,sizeof(LL));
        memset(RR,-1,sizeof(RR));
        memset(L,-1,sizeof(L));
        memset(R,-1,sizeof(R));
        for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(LL[i]==-1&&a[i][j]=='1')LL[i]=j;
                if(a[i][j]=='1')L[i][j]=j;
                else L[i][j]=L[i][j-1];
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=1;j--)
            {
                if(RR[i]==-1&&a[i][j]=='1')RR[i]=j;
                if(a[i][j]=='1')R[i][j]=j;
                else R[i][j]=R[i][j+1];
            }
        }
        int output=0x3f3f3f3f;
        for(int i=1;i<=m;i++)
        {
            int sum=0;
            for(int j=1;j<=n;j++)
            {
                int A=15000000;
                int B=15000000;
                if(L[j][i]!=-1)A=i-L[j][i];
                if(R[j][i]!=-1)B=R[j][i]-i;
                int C=15000000;
                int D=15000000;
                if(LL[j]!=-1)C=LL[j]+m-i;
                if(RR[j]!=-1)D=m-RR[j]+i;
                sum+=min(min(A,B),min(C,D));
            }
            output=min(output,sum);
        }
        if(output>=15000000)printf("-1\n");
        else printf("%d\n",output);
    }
}













你可能感兴趣的:(思维,暴力枚举)