Codeforces 230C Shifts【思维】

本文探讨了一个有趣的问题:如何通过循环挪动二维表格的行来使某列全为1,采用预处理方法加速求解,并提供了AC代码实现。

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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
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);
    }
}













### Codeforces 思维题解题思路和技巧 #### 预处理的重要性 对于许多竞赛编程问题而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类题目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体题目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此题目的关键在于如何高效地追踪最新状态而无需重复更新整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值