Super solid foundation --P2676 bookshelf

Topic links: https://www.luogu.org/problem/P2676

P2676 Super bookshelf

Title Description

Farmer John has recently acquired a huge library bookshelf for the cows, although it is so big, but it is almost instantly be filled with all kinds of books. Now, only the top of the bookshelf also left little space.

All N (1 <= N <= 20,000) cow has a height determined H_i (1 <= H_i <= 10,000). All set for the cows height and S. Height of shelves is B, and to ensure that 1 <= B <= S <2,000,000,007.

In order to reach the other end is even higher than the highest of the top shelves of dairy cows, the cows had to perform acrobatics like a general, a stand on the back of another head, folded into a "cow tower." Of course, the height of the tower, the tower is the sum of all the cow's height. In order to put things on top of shelves, height and all cows must not be less than the height of the shelves.

Obviously, the more the number of cows in the tower, the more unstable the whole tower, so the cows are hoping, subject to the bookshelf to the top, so that the number of cows in the tower as little as possible. Now, the cows found you, I hope you help them to calculate the minimum number.

Input Format

  • Line 1: 2 with a space-separated integers: N and B * + 1 2..n line: first line i + 1 is an integer 1: H_i

Output Format

  • Line 1: Output an integer of 1, i.e., how many cows folded minimum column, the top shelf to be able to

Sample input and output

Input # 1
6 40
6
18
11
13
19
11
Output # 1
3

Description / Tips

Enter a description:

A total height of six cows, shelves for 40 cows height between 6..19.

Output Description:

One kind of only three cows to reach the height of Method 40: 18 + 11 + 13. Of course, there are other ways, not in this list.

This question is the basis of comparison of a question, be regarded as entry-level questions.

In fact, there is no difficulty, as long as you a little programming ideas and basic sorting on the line.

But in order to lay a solid foundation, so I'll have to write a basic ordering again, and then sort STL sort of write it again.

Ado, on the code!!!

STL version of the sort:

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int n, a[100010], sum, tol, h;
 8 
 9 bool cmp(int x, int y)
10 {
11     return x > y;
12 }
13 
14 int main()
15 {
16     cin >> n >> h;
17     for (int i = 1; i <= n; i++)
18         cin >> a[i], sum += a[i];
19     if (sum == h)
20     {
21         cout << n << endl;
22         return 0;
23     }
24     sort(a + 1, a + 1 + n, cmp);
25     sum = 0;
26     for (int i = 1; i <= n; i++)
27     {
28         if (sum >= h)
29             break;
30         else
31             sum += a[i], tol++;
32     }
33     cout << tol << endl;
34     return 0;
35 }

 

Select Sort:

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int n, a[100010], sum, tol, h;
 8 
 9 int main()
10 {
11     cin >> n >> h;
12     for (int i = 1; i <= n; i++)
13         cin >> a[i], sum += a[i];
14     if (sum == h)
15     {
16         cout << n << endl;
17         return 0;
18     }
19     for (int i = 1; i <= n; i++)
20     {
21         int m = a[i];
22         for (int j = i; j <= n; j++)
23             if (m < a[j])
24             {
25                 int tmp = a[j];
26                 a[j] = m;
27                 m = tmp;
28             }
29         a[i] = m;
30     }
31     sum = 0;
32     for (int i = 1; i <= n; i++)
33     {
34         if (sum >= h)
35             break;
36         else
37             sum += a[i], tol++;
38     }
39     cout << tol << endl;
40     return 0;
41 }

Bubble Sort:

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int n, a[100010], sum, tol, h;
 8 
 9 int main()
10 {
11     cin >> n >> h;
12     for (int i = 1; i <= n; i++)
13         cin >> a[i], sum += a[i];
14     if (sum == h)
15     {
16         cout << n << endl;
17         return 0;
18     }
19     for (int i = 1; i <= n; i++)
20     {
21         for (int j = i + 1; j <= n; j++)
22         {
23             if (a[i] < a[j])
24             {
25                 int tmp = a[i];
26                 a[i] = a[j];
27                 a[j] = tmp;
28             }
29         }
30     }
31     sum = 0;
32     for (int i = 1; i <= n; i++)
33     {
34         if (sum >= h)
35             break;
36         else
37             sum += a[i], tol++;
38     }
39     cout << tol << endl;
40     return 0;
41 }

 

Lay a solid foundation, do a good job each question! ! !

Author: Gmax

This article belongs to the author and blog Park total, reproduced, please use the link, please do not reprint the original, Thanks ♪ (· ω ·) Techno

2019-08-09

Guess you like

Origin www.cnblogs.com/Gmax/p/11329688.html