hdu 2124 Repair the Wall

ACM比赛整理

共 2957字,需浏览 6分钟

 · 2021-10-01

Repair the Wall

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8787    Accepted Submission(s): 4076


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 a long time. And what made the situation worse was that all of Kitty's walls were made of wood.

One day, Kitty found that there was a crack in the wall. The shape of the crack is
a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors.
The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap.

Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ?

 


Input

The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).

 


Output

For each test case , print an integer which represents the minimal number of blocks are needed.
If Kitty could not repair the wall, just print "impossible" instead.

 


Sample Input

5 3
3 2 1
5 2
2 1

 


Sample Output

2
impossible



修复长城


问题描述


很久以前,基蒂住在一个小村庄里。空气清新,风景优美。唯一困扰她的是台风。

台风来的时候,一切都很可怕。风不停地吹,雨不停地下了很长时间。更糟糕的是,凯蒂家所有的墙都是木头做的。

一天,基蒂发现墙上有一条裂缝。裂缝的形状是

1×L大小的长方形(英寸)。幸运猫得到了N块和锯(锯子)从她的邻居。

砖块的形状也是矩形的,所有砖块的宽度都是1英寸。所以,在锯的帮助下,基蒂可以砍下一些木块(当然她可以直接使用它而不切割),把它们放在裂缝中,墙壁可能会修复得很完美,没有任何缝隙。

现在,基蒂知道了每个积木的大小,想用尽可能少的积木来修补墙,你能帮她吗?


输入

问题包含很多测试用例,请处理到文件末尾(EOF)。

每个测试用例包含两行。

在第一行中,有两个整数L(0

上面提到的。

在第二行,有N个正整数。第i个整数Ai(0


输出

对于每个测试用例,打印一个表示所需的最小块数的整数。

如果基蒂不能修复墙壁,就打印“不可能”。


Sample Input

5 3
3 2 1
5 2
2 1

 


Sample Output

2

impossible



解题思路:

简单的贪心
注意:使用int变量存储木块的长度和会超出int的范围。


代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 650;
long long a[maxn];
int main()
{
long long L , sum, ans;
int n;
while (scanf("%lld%d", &L, &n)!=EOF)
{
sum = 0; ans = 0;
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
sort(a, a + n);
int i = n - 1;
while (sum < L&&i >=0)
{
ans++; sum += a[i--];
}
if (sum < L)
printf("impossible\n");
else
printf("%lld\n", ans);
}
}


浏览 9
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报