Uva - 1149 - Bin Packing
2015-06-21 23:29
369 查看
贪心直接搞定,最近这些算法设计的题目总体都没有多少人提交过,都不超过100人的样子。。。数学还是很多人的一道坎
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> #include <functional> using namespace std; const int maxn = 100005; int lengthOfItems[maxn]; int main() { ios::sync_with_stdio(false); int T; cin >> T; int kase = 0; while (T--) { if (kase++) { cout << endl; } int n, l; cin >> n >> l; for (int i = 0; i < n; i++) { cin >> lengthOfItems[i]; } sort(lengthOfItems, lengthOfItems + n); int p1 = 0, p2 = n - 1, cnt = 0; // 贪心算法 while (p1 <= p2) { if (p1 == p2) { // 剩下一个了 cnt++; break; } else if (lengthOfItems[p1] + lengthOfItems[p2] <= l) { // 大加小,小于容量限制,可以放到同一个背包中 cnt++; p1++; p2--; } else { // 只把大的放到一个背包中 cnt++; p2--; } } cout << cnt << endl; } return 0; }
相关文章推荐
- poj 1060 Modular multiplication of polynomials 除数是大数的高精度除法
- 团队作业-第5周-提交处-测试与调试 :
- 标准字符串和宽字符串处理函数
- STL中的常用算法
- leetcode-226-Invert Binary Tree
- 2015第25周日
- 测试
- 运行及总结
- Struts2(七)基于XML方式进行数据输入校验
- cmd下运行php脚本 不支持curl库的问题
- Servlet 初步(一)
- 土木/岩土期刊版面费/审稿费统计列表
- 我是chongjg
- <<出现问题的自绘组合框>>的分析和修正
- 说说那些光说不练的事
- 读后感
- 测试与调试
- Redis 笔记与总结2 String 类型和 Hash 类型
- auto_ptr
- 赋值函数;复制函数