C. Strange Game on Matrix time limit/test 1 second memory limit per test 256 megabytes input standard input output STA Ndard 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 the ones in this matrix with zeroes. After that, he score in the game would be calculated as follows:initially Ivan's score is 0; in each column, I Van would find the topmost 1 (which, if the current column is j, then he'll find Minimum i suc H That ai, j = 1). If there are no 1 ' s in the column, this column is skipped; Ivan would look at the Next min (k, n-i + 1) &nb Sp;elements in this column (starting from the element him found) and count the number of 1 ' s among these elements. This number is added to his score.
Of course, Ivan wants to maximize he score in this strange game. Also He doesn ' t want to change many elements, so he'll replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score him can get and the minimum possible # of replacements required to AC Hieve that score. Input
The 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 to get this SCO Re. Examples input
4 3 2 0, 1 0 1 0 1 0 1 0 1 1 1
Output
4 1
Input
3 2 1
1 0 0 1 0 0
Output
2 0
Note
In the example Ivan would replace the element A1, 2.
Meaning
Give you the n*m matrix and K, so that you find I the smallest 1 in each column, starting from this 1 down (inclusive) until the length is len=min (k,n-i+1). The number of 1 is added to the total score.
after each column is searched, the total number is obtained. You can now manipulate: change any 1 to 0.
Now you want to get the most out of your score. Find the maximum total score and the minimum number of times the operation is at this time.
Point:
Greedy. Each column is an independent issue.
Each column calculates the number of 1 of the current position down Len length. Remember one max. This can be run out with an O (n) efficiency.
Then iterate through each column, cnt=0.
"Meet 1" and see if you can get a score that is not equal to Max, equals plus cnt. Doesn't mean it's cnt++.
#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream> #include <
Math.h> #include <map> using namespace std;
int a[111][111];
int num[111][111];
int max[111];
int main () {int n,m,k;
scanf ("%d%d%d", &n,&m,&k);
for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {scanf ("%d", &a[i][j]);
} int ansscore=0;
int ans=0;
for (int j=1;j<=m;j++) {int num1=0;
int len=min (K,N);
for (int i=1;i<=len;i++) {if (a[i][j]==1) num1++;
} num[j][1]=num1;
Max[j]=max (max[j],num[j][1]);
for (int i=2;i<=n;i++) {if (a[i-1][j]==1) {num[j][i]=num[j][i-1]-1;
}else Num[j][i]=num[j][i-1];
if (i+len-1<=n&&a[i+len-1][j]==1) {num[j][i]++;
} max[j]=max (Max[j],num[j][i]); } ansscore+=max[J];
num1=0;
for (int i=1;i<=n;i++) {if (num[j][i]==max[j]&&a[i][j]==1) {break;
}else if (a[i][j]==1) {num1++;
}} ans+=num1;
printf ("%d%d\n", Ansscore,ans);
return 0; }