--hdu 2124 Repair the Wall(贪心)

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

Ac code :

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
    return *(int *)b-*(int *)a;
}
int main()
{
    int L, N, i;
    int a[601], sum, len;
    while(scanf("%d",&L)!=EOF)
    {
        scanf("%d",&N);
        for(i = 0; i < N; i++)
            scanf("%d",&a[i]);
        qsort(a, N, sizeof(a[0]), cmp);
        sum = len = 0;
        for(i = 0; i < N; i++)
        {
            if(len < L)
            {
                len += a[i];
                sum++;
            }
            else
                break;
        }
        if(len < L)
            printf("impossible\n");
        else
            printf("%d\n",sum);
    }
    return 0;
}

  

时间: 2024-12-08 19:42:47

--hdu 2124 Repair the Wall(贪心)的相关文章

HDU 2124 Repair the Wall

Repair the Wall Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3076    Accepted Submission(s): 1504 Problem Description Long time ago , Kitty lived in a small village. The air was fresh and th

【贪心专题】HDU 1800 Flying to the Mars (寻找最大重复元素) &amp;&amp; HDU 2124 Repair the Wall (贪心)

链接:click here~~ 题意: 有n个士兵每个人有一个水平值,水平高的的人可以教低的人,意思就是求最合适的组合使花费最小 [解题思路] 刚看到此题,竟没有思路..想 了一会,其实找到最大重复元素的次数即可,因为相同的人肯定不能共用一个,所以求得最少即为最大的重复次数,跟前面一道题差不多,做完看了别人思路,发现用map容器来做很方便:map容器的内部是一个红黑树,我们是在对它的叶节点进行操作,一共有两个数,第二个数是作为计数用的. 代码: #include <stdio.h> #incl

hdoj 2124 Repair the Wall 【贪心】

题意:有一栋墙坏了(台风吹坏的,并且宽度一定),这个猪脚要修这栋墙,并且找到了一些宽度跟刮坏的墙一样,只是长度不一样的木块,让你求这些木块能不能修好这堵墙, 一句话就是判断这些的木块的长度的和能不能大于破坏的墙的长度,如果能,输出最少用几块, 不能输出impossible. 这道题水的不行...从大到小排下序就好了 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 代码: #include<stdio.h> #include<algo

杭电 2124 Repair the Wall

http://acm.hdu.edu.cn/showproblem.php?pid=2124 Repair the Wall Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2076    Accepted Submission(s): 1003 Problem Description Long time ago , Kitty liv

HDU2124 Repair the Wall(贪心)

Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon. When the typhoon came, everything is terrible. It kept blowing and raining for

HDU2124 Repair the Wall 【贪心】

Repair the Wall Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2080    Accepted Submission(s): 1007 Problem Description Long time ago , Kitty lived in a small village. The air was fresh and th

杭电2124(Repair the Wall )

Repair the Wall Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Long time ago , Kitty l

Repair the Wall

Repair the Wall Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Long time ago , Kitty l

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)