hdoj 2124 Repair the Wall 【贪心】

题意:有一栋墙坏了(台风吹坏的,并且宽度一定),这个猪脚要修这栋墙,并且找到了一些宽度跟刮坏的墙一样,只是长度不一样的木块,让你求这些木块能不能修好这堵墙,

一句话就是判断这些的木块的长度的和能不能大于破坏的墙的长度,如果能,输出最少用几块, 不能输出impossible。

这道题水的不行。。。从大到小排下序就好了

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

代码:

#include<stdio.h>
#include<algorithm>
using std::sort;
int s[700];
int main()
{
	int l, n, i, ans, sum;
	while(scanf("%d%d", &l, &n) == 2){
		for(i = 0; i < n; i ++)
			scanf("%d", &s[i]);
		sort(s, s+n);
		sum = 0;
		ans = 0;
		int flag = 0;
		for(i = n-1; i >= 0; i --){
			sum+= s[i];
			++ans;
			if(sum >= l){
				flag = 1;
				break;
			}
		}
		if(!flag)
		printf("impossible\n");
		else
		printf("%d\n", ans);
	}
	return 0;
}

hdoj 2124 Repair the Wall 【贪心】,布布扣,bubuko.com

时间: 2024-10-08 20:50:27

hdoj 2124 Repair the Wall 【贪心】的相关文章

--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",

杭电 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

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

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

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

杭电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

HDOJ 1009. Fat Mouse&#39; Trade 贪心 结构体排序

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 56784    Accepted Submission(s): 19009 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g