首页 技术 正文
技术 2022年11月16日
0 收藏 610 点赞 2,429 浏览 855 个字

题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1099

     https://www.luogu.com.cn/problem/P1094

题目描述

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

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

输入

输入文件group.in包含n+2行:

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

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

输出

输出文件group.out仅→行,包含一个整数, ep最少的分组数目合

样例输入

100
9
90
20
20
30
50
60
70
80
90

样例输出

6

提示

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

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

作者分析:这道题我们先排序,再定义一个整数t,表示当前数组纪念值最小的位置,再判断与最大值的和。

#include <iostream>
#include <algorithm>
using namespace std;int main(){
int n,m,t = 1,ans = 0;
cin >> n >> m;
int a[m+1];
for (int i = 1;i <= m;i++){
cin >> a[i];
}
sort(a,a+m+1);
for (int i = m;i > 0;i--){
if (a[i] == -1){
continue;
}
if (a[i] + a[t] <= n && t != i){
a[i] = -1;
a[t] = -1;
t++;
ans++;
}
else{
if (a[i] <= n){
a[i] = -1;
ans++;
}
}
}
cout << ans;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848