p1124 纪念品分组

版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/84438208

题目

描述 Description
元旦快到了,校学生会让乐乐负责新年晚会的纪念品发放工作。为使得参加晚会的同学所获得 的纪念品价值相对均衡,他要把购来的纪念品根据价格进行分组,但每组最多只能包括两件纪念品, 并且每组纪念品的价格之和不能超过一个给定的整数。为了保证在尽量短的时间内发完所有纪念品,乐乐希望分组的数目最少。

你的任务是写一个程序,找出所有分组方案中分组数最少的一种,输出最少的分组数目。

【限制】

50%的数据满足: 1 <=n <= 15

100%的数据满足: 1 <= n <= 30000, 80 <= W <= 200

输入格式 Input Format
第1行包括一个整数w,为每组纪念品价格之和的上眼= 第2行为一个整数n,表示购来的纪念品的总件数G

第3-n+2行每行包含一个正整数Pi (5 <= Pi <= w3)w表示所对应纪念品的价格。

输出格式 Output Format
仅1行,包含一个整数, ep最少的分组数目合
样例输入 Sample Input

100
9
90
20
20
30
50
60
70
80
90

样例输出 Sample Output

6

时间限制 Time Limitation
各个测试点1s
来源 Source
Noip2007普及组第2题

代码

#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
int w,n,ans;
int pi[30010];
int main()
{
	cin>>w>>n;
	for(int i=1;i<=n;i++)	scanf("%d",&pi[i]);
	sort(pi,pi+n+1);
	int a=1,b=n;
	while(a<=b)
	{
	    if(pi[a]+pi[b]<=w)  a++;
		b--;
		ans++;
    }
    cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/huashuimu2003/article/details/84438208