Codeforces 873C Strange Game on matrix greedy water problem __codeforces

Source: Internet
Author: User
Tags integer numbers

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'll be calculated as follows:initially Ivan score is 0; In each column, Ivan would find the topmost 1  (which, if the current column is j, then he'll find Minimu M i such That ai, j = 1). If there are no 1 ' s in the column, this column is skipped; Ivan Next min (k, n-i + 1)  elements in this column (starting from the element he found) and co UNT 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.

The main effect of the topic:


Gives a n*m matrix, We can make any one of the 1 in the matrix become 0. The value is calculated by finding the first 1 of each column, then calculate the number of 1 in the contiguous paragraph below 1 and below it, and then add it to the answer, and now we want to make the result as large as possible, and make the transformation as small as possible, output the maximum value and the minimum number of transformations.


Ideas:


Each column is a child problem, so when we enumerate to 1 for each column, we decide whether it is better to delete all the 1 in front of it.


AC Code:

 #include <stdio.h> #include <string.h> using namespace std; int a[150][150]; int main () {I
    NT N,m,k; while (~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 ans1=0,ans2=0;
            for (int j=1;j<=m;j++) {int pre=0;
            int maxn=0;
            int del=0;
                for (int i=1;i<=n;i++) {int cnt=0;
                for (int l=i;l<=n&&l-i+1<=k;l++) {if (a[l][j]==1) cnt++;
                    } if (maxn<cnt) {maxn=cnt;
                Del=pre;
            } Pre+=a[i][j];
            } ANS1+=MAXN;
        Ans2+=del;
    printf ("%d%d\n", ans1,ans2); }
}










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.