A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the same length l and each item i has length li<=l . We look for a minimal number of bins q such that
You are requested, given the integer values n , l , l1 , ..., ln , to compute the optimal number of bins q .
- each bin contains at most 2 items,
- each item is packed in one of the q bins,
- the sum of the lengths of the items packed in a bin does not exceed l .
You are requested, given the integer values n , l , l1 , ..., ln , to compute the optimal number of bins q .
The first line of the input contains the number of items n (1<=n<=10
5) . The second line contains one integer that corresponds to the bin length l<=10000 . We then have n lines containing one integer value that represents the length of the items.
Your program has to write the minimal number of bins required to pack all items.
10 80 70 15 30 35 10 80 20 35 10 30
6
/*哇,想多了,该题直接用o(n)的算法就可以完成有点像二分的思想,将一个有序的数列从两头向中间枚举移动下标即可*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100000+10];
bool cmp(const int a, const int b)
{
return a > b;
}
int main()
{
int n, s;
while(scanf("%d", &n) != EOF){
memset(a, 0, sizeof(a));
scanf("%d", &s);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a+n, cmp);
int j = n-1, i = 0;
int sum = 0;
while(i <= j){
if(a[i] + a[j] <= s && i != j) j--;
i++;
sum++;
}
printf("%d\n", sum);
}
return 0;
}

513

被折叠的 条评论
为什么被折叠?



