HDU 1735 统计数字(贪心)

来源:互联网 发布:115浏览器for mac 编辑:程序博客网 时间:2024/06/05 05:59

题意:告诉你,n行,l 列,m段

找出最少被涂掉的字的个数

题解:记录总共0的个数,减掉m*2 ,在两个0开头的上一行尾数 0的个数记下来,排个序,减掉越长的越好,一共减掉m-1个就可以了。

#include <stdio.h>#include <string.h>#include <cstring>#include <algorithm>#include <iostream>using namespace std;int mp[10005][105];int b[10005];//尾部 int n,l,m;int hang[10005];//确定那几行 int a1,a0;int main(){    while(~scanf("%d%d%d",&n,&l,&m))    {        memset(hang,0,sizeof(hang));        memset(b,0,sizeof(b));        a1=0;a0=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=l;j++)            {                scanf("%d",&mp[i][j]);                if(mp[i][j]==0)    a0++;                else a1++;            }            if(mp[i][1]==0 &&mp[i][2]==0 && i!=1)                hang[i]=1;         }        int cnt=1;//记录个数         for(int i=2;i<=n;i++)        {            if(hang[i]==1)            {                for(int j=l;j>=1;j--)                {                    if(mp[i-1][j]==1)                         break;                    else {                        b[cnt]++;                    }                }                cnt++;            }        }        cnt--;        //最下面一层         for(int j=l;j>=1;j--)        {            if(mp[n][j]==1)                 break;            else {                a0--;            }        }        a0-=m*2;        sort(b+1,b+cnt+1);               for(int i=cnt;i>=0 &&m>1 ;i--,m--)        {            a0-=b[i];        }         printf("%d\n",a0);    }    return 0;}


0 0
原创粉丝点击