Educational Codeforces Round 30 - C. Strange Game On Matrix(貪心)__CodeForces

來源:互聯網
上載者:User

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: 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 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的矩陣和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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.