Repair the Wall

Source: Internet
Author: User

Repair the Walltime limit:5000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): 2 A ccepted Submission (s): 2font:times New Roman | Verdana | Georgiafont Size:←→problem descriptionlong time ago, Kitty lived in a small village. The air was fresh and the scenery were very beautiful. The only thing that troubled she 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 is that's all of the Kitty's walls were made of wood.

One day, Kitty found that there is a crack in the wall. The shape of the crack is
A rectangle with the size of 1xL (inch). Luckly Kitty got N blocks and a saw (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 some of the blocks (of course she could use it directly without cutting) and Put them in the crack, and the wall is 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 yo U help her? Inputthe problem contains many test cases, please process to the end of file (EOF).
Each test case contains the lines.
In the first line, there is integers L (0<l<1000000000) and N (0<=n<600) which
mentioned above.
In the second line, there is N positive integers. The ith integer Ai (0<ai<1000000000) means that the ith block have the size of 1xAi (in inch). Outputfor each test case, print an integer which represents the minimal number of blocks is needed.
If Kitty could not repair the wall, just print "impossible" instead. Sample Input

3 2 1
5 2
2 1
Sample Output
2 Impossible
Simple greedy Oh, don't say more. The title is to use wood to repair the wall, the minimum number of use, remember that the wood can also be separated from the broken.
So only when the total size of all the wood and less than the wall is impossible, the others from the big to the small sum, until the len>= wall
It's OK.
1#include <iostream>2 using namespacestd;3 intcmpConst void*a,Const void*b)4 {5     return*((int*) b)-* ((int*) a);6 }7 intMain ()8 {9     intL, N, I;Ten     inta[601], sum, Len; One      while(Cin >> L >>N) A     { -          for(i =0; i < N; i++) -CIN >>A[i]; the         -Qsort (A, N,sizeof(a[0]), CMP); -sum = Len =0; -          for(i =0; i < N; i++) +         if(Len <L) -         { +Len + =A[i]; Asum++; at         } -         Else -              Break; -         if(Len <L) -cout <<"Impossible"<<Endl; -         Else incout << sum <<Endl; -     } to     return 0; +}
View Code

Repair the Wall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.