Codeforces Round #142 (Div. 2) C. Shifts
2014-04-03 13:25
387 查看
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.
Sample test(s)
input
output
input
output
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.
题意:给你一个只包含0和1的矩阵,左右移动看看至少多少步后有至少一整列全是1。 模拟一下下。。。把每列的最小步数算出,然后再求出最大值。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sum[10001],b[10001],ans;
int n, m,i,j;
char a[10001];
int main()
{
scanf("%d%d", &n, &m);
int flag=0;
for(i=0;i<n;i++)
{
cin>>a;
int ss=1;
for(j=0;j<m;j++)
{
if(a[j]=='1')
b[ss++]=j;
}
if(ss==1)
{
flag=1; continue;
}
b[0]=b[ss-1]-m;
b[ss]=b[1]+m;
int r,l;ans=0;
for(j=0;j<m;j++)
{
r=j-b[ans];
l=b[ans+1]-j;
if(l==0)
++ans;
sum[j]+=min(l,r);
}
}
int min=1e8;
if(flag==1)
printf("-1\n");
else
{
for(j=0;j<m;j++)
{
//cout<<sum[j]<<"^^";
if(min>sum[j])
min=sum[j];
}
printf("%d\n",min);
}
return 0;
}
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.
Sample test(s)
input
3 6 101010 000100 100000
output
3
input
2 3111
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.
题意:给你一个只包含0和1的矩阵,左右移动看看至少多少步后有至少一整列全是1。 模拟一下下。。。把每列的最小步数算出,然后再求出最大值。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sum[10001],b[10001],ans;
int n, m,i,j;
char a[10001];
int main()
{
scanf("%d%d", &n, &m);
int flag=0;
for(i=0;i<n;i++)
{
cin>>a;
int ss=1;
for(j=0;j<m;j++)
{
if(a[j]=='1')
b[ss++]=j;
}
if(ss==1)
{
flag=1; continue;
}
b[0]=b[ss-1]-m;
b[ss]=b[1]+m;
int r,l;ans=0;
for(j=0;j<m;j++)
{
r=j-b[ans];
l=b[ans+1]-j;
if(l==0)
++ans;
sum[j]+=min(l,r);
}
}
int min=1e8;
if(flag==1)
printf("-1\n");
else
{
for(j=0;j<m;j++)
{
//cout<<sum[j]<<"^^";
if(min>sum[j])
min=sum[j];
}
printf("%d\n",min);
}
return 0;
}
相关文章推荐
- Shifts-Codeforces Round #142 (Div. 2) C (矩阵)
- Codeforces Round #142 (Div. 2) C. Shifts
- Codeforces Round #142 (Div. 2) C
- Codeforces Round #142 (Div. 2)
- Codeforces Round #142 (Div. 2) 解题报告
- Codeforces Round #142 (Div. 2) B. T-primes (数学、O(n)筛素数)
- Codeforces Round #142 (Div. 2)
- 【CodeForce】Codeforces Round #142 (Div. 2) A B C
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts(瞎搞)
- Codeforces Round #142 (Div. 2)——B
- Codeforces Round #142 (Div. 2) 解题报告
- Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
- Codeforces Round #142 (Div. 1) C. Triangles
- 解题报告:Codeforces Round #142 (Div. 1) C. Triangles (思维好题)
- Codeforces Round #142 (Div. 2) B - T-primes
- Codeforces Round #142 (Div. 2)
- Codeforces Round #142 (Div. 1) C. Triangles
- Codeforces Round #421 (Div. 1):B. Mister B and PR Shifts 思维,乱搞
- Codeforces Round #142 (Div. 2) E. Triangles
- Codeforces Round #142 (Div. 2)