hdu 1735(贪心) 统计字数

戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735

对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实

这是道贪心的题目,要有贪心的意识。

首先将所有为0的地方统计,因为是求最小的字数,所有最后一行后面的0可以看为空格直接减掉,

因为有m段(一定包括第一行),再减去m*2,最后枚举每行,将至少前两个为0的上一行的最后有多少

的0统计起来排序,再依次减去前m-1个大的,这样就保证了得到的答案是符合条件中最小的

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 int yj[10001][101],lsy[10001];
 5 bool cmp(int x,int y){return x>y;}
 6 int main()
 7 {
 8     int n,m,x,sum,ans,i,j,k,flag;
 9     while (~scanf("%d %d %d",&n,&m,&x))
10     {
11         sum=0;
12         for (i=1;i<=n;i++)
13         {
14             for (j=1;j<=m;j++)
15             {
16                scanf("%d",&yj[i][j]);
17                if (!yj[i][j])
18                   sum++;
19             }
20         }
21         //printf("%d\n",sum);
22         for (i=m;i>=1;i--)
23         {
24             if (!yj[n][i]) sum--;
25             else break;
26         }
27         //printf("%d\n",sum);
28         k=1;
29         for (i=2;i<=n;i++)
30         {
31             ans=0;flag=0;
32             if (!yj[i][1]&&!yj[i][2])
33             {
34                 flag=1;
35                 for (j=m;j>=1;j--)
36                 {
37                     if (!yj[i-1][j])
38                         ans++;
39                     else
40                         break;
41                 }
42             }
43             if (flag)
44                lsy[k++]=ans;
45         }
46         sum-=2*x;x--;
47         /*for (i=1;i<k;i++)
48             printf("%d ",lsy[i]);
49         printf("\n");*/
50         sort(lsy+1,lsy+k,cmp);
51         for (i=1;i<=x;i++)
52             sum-=lsy[i];
53         printf("%d\n",sum);
54     }
55     return 0;
56 }
时间: 2024-11-02 15:19:14

hdu 1735(贪心) 统计字数的相关文章

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

HDU 4909 String 统计+状压

因为连续异或满足区间减法性质,所以可以状压之后用异或来判断是否为符合条件的单词并且存储次数 一开始用map,一直超时.虽然直接用开1<<26的数组内存存的下,但是memset的时间肯定会超,但是只要在每次循环之后把加过的值减掉就可以绕过memset了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits>

HDU 2008 数值统计

数值统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 53462    Accepted Submission(s): 27366 Problem Description 统计给定的n个数中,负数.零和正数的个数. Input 输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

hdu 1050(贪心算法)

Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19278    Accepted Submission(s): 6582 Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a