传送门:牛客
题目描述:
元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得 的纪念品价值相对均
衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品, 并且每组纪念品的价格之和不能超过\
一个给定的整数。为了保证在尽量短的时间内发完所有纪念品,乐乐希望分组的数目最少。
你的任务是写一个程序,找出所有分组方案中分组数最少的一种,输出最少的分组数目。
输入:
100
9
90
20
20
30
50
60
70
80
90
输出:
6
当年NOIP普及组的一道题,牛客网上两星的一道题,感觉应该是一星题??
主要思路:
首先每组的个数最大只能为2(这个限定大大降低的做题的难度),为了最优的方案,我们不妨给这个序列排一个序,然后用双指针扫一遍这个序列就行(以序列中的最大值为标准),当我们,目前最大和最小的和比要求的限制要大时,说明大的要自己为一组,所以将R指针左移即可,反之R指针左移,L指针右移
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
int p[30005];
int main() {
int w,n;
w=read();n=read();
for(int i=1;i<=n;i++) p[i]=read();
sort(p+1,p+n+1);
int l=1,r=n;
int sum=0;
while(l<=r) {
if(p[r]+p[l]>w) {
sum++;
r--;
}else {
sum++;
r--;
l++;
}
}
cout<<sum<<endl;
return 0;
}