Codeforces 873C Strange Game On Matrix【贪心】水题
来源:互联网 发布:java视频直播技术架构 编辑:程序博客网 时间:2024/05/21 01:58
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.
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.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
4 3 20 1 01 0 10 1 01 1 1
4 1
3 2 11 00 10 0
2 0
In the first example Ivan will replace the element a1, 2.
题目大意:
给出一个N*M的矩阵,我们可以使得矩阵中任意一个1变成0.计算价值的方法是:找到每一列的第一个1,然后计算这个1以及其下边小于等于k的连续段落里边有多少个1,然后加到答案中,现在我们希望使得结果尽可能的大,而且使得变换的次数尽可能的小,输出最大值以及变换的最小次数。
思路:
每一列都是一个子问题,那么对应每一列我们枚举到1的时候,判断删除其前边所有的1是否更优即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int a[150][150];int main(){ int 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); }}
- Codeforces 873C Strange Game On Matrix【贪心】水题
- codeforces 873 C. Strange Game On Matrix【贪心】
- Educational Codeforces Round 30C-模拟&贪心-Strange Game On Matrix
- Educational Codeforces Round 30 C. Strange Game On Matrix
- (Coderforces 873C)C. Strange Game On Matrix 贪心+模拟
- codeforces——873C —— Strange Game On Matrix
- Codeforces-Strange Game On Matrix(暴力直接写)
- [codeforces] C - Efim and Strange Grade 模拟+贪心
- Codeforces 719C Efim and Strange Grade 贪心+模拟
- Codeforces 719C Efim and Strange Grade【贪心】
- CodeForces 280C Game on Tree 概率
- codeforces 280C Game on Tree
- CodeForces 873C(贪心)
- Codeforces 388C Fox and Card Game(贪心)
- 【Codeforces 765 C. Table Tennis Game 2】+ 贪心
- Educational Codeforces Round 22-C. The Tag Game-搜索,贪心
- Codeforces Round #419 (Div. 2) C. Karen and Game【贪心】
- CodeForces -Game on Paper
- XListView的使用
- 字符串函数strspn、strcspn和strpbrk
- 上机练习题——继承
- 《零基础入门学习python》学习过程(五)
- CreticalSection临界区
- Codeforces 873C Strange Game On Matrix【贪心】水题
- 1005 大数加法 ——51Nod(java BigInteger)
- RequireJS模块化加载
- 如何在Ubuntu下安装go语言的Ethereum (geth)
- OkHttp的基本使用
- Mutex互斥体 学习
- Divide Two Integers--LeetCode
- 如何判断链表是否有环
- Event事件对象 学习