Educational Codeforces Round 30 - C. Strange Game On Matrix(贪心)
2017-10-13 22:19
411 查看
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-indexe
4000
d. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated
as follows:
Initially Ivan's score is 0;
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;
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
output
input
output
Note
In the first example Ivan will replace the element a1, 2.
题意:
给你n*m的矩阵和k,让你在每一列的里找i最小的1,从这个1向下出发(包含),直到长度为len=min(k,n-i+1)。其中的1的个数加到总分数中。
每一列找完之后,得到总分数。现在可以操作:把任意个1变为0。
现在想要总分数最大。求最大总分数和此时操作的最小次数。
POINT:
贪心。每一列都是一个独立的问题。
每一列算出当前位置向下len个长度的1的个数有多少。记一个max。这个可以用o(n)的效率跑出来。
然后遍历每一列,cnt=0。
【遇到1】,看看可以得到分数等不等于max,等于加上cnt。不等于就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;
}
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-indexe
4000
d. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated
as follows:
Initially Ivan's score is 0;
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;
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 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 first example Ivan will replace the element a1, 2.
题意:
给你n*m的矩阵和k,让你在每一列的里找i最小的1,从这个1向下出发(包含),直到长度为len=min(k,n-i+1)。其中的1的个数加到总分数中。
每一列找完之后,得到总分数。现在可以操作:把任意个1变为0。
现在想要总分数最大。求最大总分数和此时操作的最小次数。
POINT:
贪心。每一列都是一个独立的问题。
每一列算出当前位置向下len个长度的1的个数有多少。记一个max。这个可以用o(n)的效率跑出来。
然后遍历每一列,cnt=0。
【遇到1】,看看可以得到分数等不等于max,等于加上cnt。不等于就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;
}
相关文章推荐
- Educational Codeforces Round 30 C. Strange Game On Matrix
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Educational Codeforces Round 30(补题) A B略 C(贪心)
- Educational Codeforces Round 26 G. Functions On The Segments 主席树
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- Educational Codeforces Round 24 E. Card Game Again(双指针)
- Educational Codeforces Round 8 C. Bear and String Distance(贪心)
- Educational Codeforces Round 27 A B 暴力+贪心 C 贪心+排序
- Educational Codeforces Round 30 - B. Balanced Substring
- Educational Codeforces Round 30 D.Merge Sort
- Educational Codeforces Round 20 A - Maximal Binary Matrix
- Educational Codeforces Round 16 B. Optimal Point on a Line
- Educational Codeforces Round 31 E. Binary Matrix 并查集
- Educational Codeforces Round 7 E. Ants in Leaves 贪心
- Educational Codeforces Round 5-B. Dinner with Emma(贪心)
- Educational Codeforces Round 6 C. Pearls in a Row(贪心)
- Educational Codeforces Round 22 C. The Tag Game(思维)
- Educational Codeforces Round 7 C. Not Equal on a Segment 并查集
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- Educational Codeforces Round 31 E. Binary Matrix 并查集