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 ;
}

最新文章

  1. C、C++: 引用、指针、实例、内存模型、namespace
  2. gif图片加载问题
  3. 【SharePoint学习笔记】第3章 SharePoint列表新特性以及数据访问
  4. Json对象与Json字符串互转(转载)
  5. vim 标准环境的配置
  6. 用c#开发微信 (8) 微渠道 - 推广渠道管理系统 3 UI设计及后台处理
  7. Object C学习笔记22-#define 用法
  8. [python]实现单机版一行wordcount
  9. POJ 1419
  10. 远程开发调试与hot-update | (R "think-of-lisper" 'Albertlee)
  11. *** Terminating app due to uncaught exception &#39;NSUnknownKeyException&#39;, reason: &#39;[ViewController &gt; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
  12. mysql数据库第一弹
  13. jquery ajax超时设置(转载)
  14. python3元组,列表的几个属性
  15. spring与mybatis四种整合方法
  16. 顺便谈谈对于Java程序猿学习当中各个阶段的建议
  17. Jpa 报错 :HTTP Status 400 - Required String parameter &#39;xx&#39; is not present
  18. C# lambda方式取出前十个经常联系的手机号码
  19. python脚本监控获取当前Linux操作系统[内存]/[cpu]/[硬盘]/[登录用户]
  20. 复合词 (Compund Word,UVa 10391)

热门文章

  1. 【深入理解javascript】闭包
  2. C#基础笔记(第十一天)
  3. [svc]sublime text3设置py环境最佳姿势
  4. [LeetCode] 38. Count and Say_Easy
  5. [LeetCode] questions conclusion_ Dynamic Programming
  6. testng入门教程10 TestNG参数化测试
  7. 5Lambda表达式
  8. 服务器返回的json数据中含有null的处理方法
  9. python 常见脚本
  10. 利用arcgis处理遥感栅格数据,得到省平均值数据