问题虫洞:1107.纪念品分组
黑洞内窥:
把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品, 并且每组纪念品的价格之和不能超过一个给定的整数。
思维光年:
思路:排序,然后前后相加就完事。
#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<math.h>
#include<map>
#include<algorithm>
#include<cstring>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
using namespace std ;
typedef long long ll;
#define LEN sizeof(struct student)//开辟动态存储区
#define INF 0x3f3f3f3f//将近int类型最大数的一半,而且乘2不会爆int
#define MAXN 10005
int cmp(int x, int y)
{
return x > y;
}
int a[30005];
int main( )
{
int w, n;
cin >> w >> n;
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a+n, cmp);
int b=0, c=n-1;
for(int i=0; i<n; i++)
{
if(a[i] == 0)
continue ;
if(a[i] + a[c] > w || i == c)
{
b++;
}
else
{
b++;
a[c] = 0;
c--;
}
}
cout << b << '\n';
return 0;
}