C. Strange Game On Matrix
time limit per test:
1 second
memory limit per test:
256 megabytes
input:
standard input
output:
standard output
Ivan is playing a strange game.
He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan’s score is 0;
2. In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1’s in the column, this column is skipped;
3. Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1’s among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn’t want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
Input
The first line contains three integer numbers n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100).
Then n lines follow, i-th of them contains m integer numbers — the elements of i-th row of matrix a. Each number is either 0 or 1.
Output
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
Examples
Input
4 3 20 1 01 0 10 1 01 1 1
Output
4 1
Input
3 2 11 00 10 0
Output
2 0
Note
In the first example Ivan will replace the element a1, 2.
题意:
有一个n*m的矩阵,每个元素要么是0,要么是1,现在进行这样的游戏:
首先,你可以将其中的一些1换成0,初始时你的得分为0。
然后,对于每一列,从上到下找到第一个1的位置,从这个位置之后连续的k个元素中1的个数,就是你在这一轮中获得的分数。
问你可以获得最大的分数是多少?在分数最大时,替换的最少次数是多少?
分析:
对于每一列,我们可以看成是单独的一个游戏部分。
要使分数最大,那就是找每一列中连续k个位置的1的数目最大的第一个位置。
所以我们只需要按照题目的意思进行模拟一遍就可以了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
const int maxn = 110;
int n,m,k,maxans,minnum;
int a[maxn][maxn];
int sum[maxn];
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
maxans = 0;
minnum = 0;
int t=0,maxnum=0;
sum[0]=0;
for(int j=1;j<=m;j++)
{
t=0,maxnum=0,sum[0]=0;
for(int i=1;i<=n;i++) sum[i] = sum[i-1] + a[i][j];
for(int i=1;i<=n-k+1;i++)
{
int tt = sum[i+k-1] - sum[i-1];
if(tt > maxnum)
{
maxnum = tt;
t = i;
}
}
minnum += sum[t] - a[t][j];
maxans += maxnum;
}
printf("%d %d\n",maxans,minnum);
}
return 0;
}