UVa 1149 装箱
2024-10-16 02:44:07
https://vjudge.net/problem/UVA-1149
题意:给定N个物品的重量和背包的容量,同时要求每个背包最多装两个物品。求至少需要的背包数。
思路:很简单的贪心。每次将最轻的和最重的放一个背包里,如果放不下,则只放一个最重的。
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = + ; int n, m;
int a[maxn];
int ans; void solve()
{
ans = ;
sort(a, a + n);
int L = , R = n - ;
while (L <= R)
{
if (a[L] + a[R] <= m)
{
ans++;
L++;
R--;
}
else
{
ans++;
R--;
}
}
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
for (int i = ; i < n; i++)
{
cin >> a[i];
}
solve();
cout << ans << endl;
if (t) cout << endl;
}
return ;
}
最新文章
- C、C++: 引用、指针、实例、内存模型、namespace
- gif图片加载问题
- 【SharePoint学习笔记】第3章 SharePoint列表新特性以及数据访问
- Json对象与Json字符串互转(转载)
- vim 标准环境的配置
- 用c#开发微信 (8) 微渠道 - 推广渠道管理系统 3 UI设计及后台处理
- Object C学习笔记22-#define 用法
- [python]实现单机版一行wordcount
- POJ 1419
- 远程开发调试与hot-update | (R "think-of-lisper" 'Albertlee)
- *** Terminating app due to uncaught exception &#39;NSUnknownKeyException&#39;, reason: &#39;[ViewController >; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
- mysql数据库第一弹
- jquery ajax超时设置(转载)
- python3元组,列表的几个属性
- spring与mybatis四种整合方法
- 顺便谈谈对于Java程序猿学习当中各个阶段的建议
- Jpa 报错 :HTTP Status 400 - Required String parameter &#39;xx&#39; is not present
- C# lambda方式取出前十个经常联系的手机号码
- python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
- 复合词 (Compund Word,UVa 10391)
热门文章
- 【深入理解javascript】闭包
- C#基础笔记(第十一天)
- [svc]sublime text3设置py环境最佳姿势
- [LeetCode] 38. Count and Say_Easy
- [LeetCode] questions conclusion_ Dynamic Programming
- testng入门教程10 TestNG参数化测试
- 5Lambda表达式
- 服务器返回的json数据中含有null的处理方法
- python 常见脚本
- 利用arcgis处理遥感栅格数据,得到省平均值数据