hdu 1735(贪心)

来源:互联网 发布:php ture还是true 编辑:程序博客网 时间:2024/05/16 04:36

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1735

思路:贪心,只要开头有两个0,则需要判断其上一行最后有多少个连续的0,按上一行最后处0的数量由大到小排序(特殊处理一下最后一行末尾0就可以了);

View Code
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 10000+10 7 #define MAXL 100+10 8 int map[MAXN][MAXL]; 9 int n,l,m,k,ans;10 int _count[MAXN];//(如果当前行前2个为0)记录上一行行尾0的个数11 12 int cmp(const int &p,const int &q){13     return p>q;14 }15 16 17 int main(){18     while(~scanf("%d%d%d",&n,&l,&m)){19         k=0,ans=0;20         for(int i=1;i<=n;i++){21             for(int j=1;j<=l;j++){22                 scanf("%d",&map[i][j]);23                 if(!map[i][j])ans++;24             }25         }26         //除去最后一行行尾0的个数27         for(int i=l;i>=1;i--){28             if(!map[n][i])ans--;29             else break;30         }31         ans-=2*m;//除去m行开始的2个032         for(int i=2;i<=n;i++){33             int count=0;34             if(!map[i][1]&&!map[i][2]){35                 //记录上一行行尾0的个数36                 for(int j=l;j>=1;j--){37                     if(!map[i-1][j])count++;38                     else break;39                 }40                 _count[k++]=count;41             }42         }43         sort(_count,_count+k,cmp);44         //只需m-1行就行;45         for(int i=0;i<m-1;i++){46             ans-=_count[i];47         }48         printf("%d\n",ans);49     }50     return 0;51 }

 

0 0
原创粉丝点击