hdu---3177 Crixalis's Equipment 根据 两个元素 之间的权衡进行排序
2016-03-03 09:40
417 查看
Crixalis's Equipment
[align=left]Problem Description[/align]![](https://oscdn.geek-share.com/Uploads/Images/Content/201603/12fc0a97997b09048f24a453bcd33675.jpg)
Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts of Kalimdor. Though he's a guardian of Lich King now, he keeps the living habit of a scorpion like living underground and digging holes.
Someday Crixalis decides to move to another nice place and build a new house for himself (Actually it's just a new hole). As he collected a lot of equipment, he needs to dig a hole beside his new house to store them. This hole has a volume of V units, and Crixalis has N equipment, each of them needs Ai units of space. When dragging his equipment into the hole, Crixalis finds that he needs more space to ensure everything is placed well. Actually, the ith equipment needs Bi units of space during the moving. More precisely Crixalis can not move equipment into the hole unless there are Bi units of space left. After it moved in, the volume of the hole will decrease by Ai. Crixalis wonders if he can move all his equipment into the new hole and he turns to you for help.
[align=left]Input[/align]
The first line contains an integer T, indicating the number of test cases. Then follows T cases, each one contains N + 1 lines. The first line contains 2 integers: V, volume of a hole and N, number of equipment respectively. The next N lines contain N pairs of integers: Ai and Bi.
0<T<= 10, 0<V<10000, 0<N<1000, 0 <Ai< V, Ai <= Bi < 1000.
[align=left]Output[/align]
For each case output "Yes" if Crixalis can move all his equipment into the new hole or else output "No".
[align=left]Sample Input[/align]
2
20 3
10 20
3 10
1 7
10 2
1 10
2 11
[align=left]Sample Output[/align]
Yes
No
题意: 蝎子想搬家(洞穴) 然后因为有家具所以 所以挖洞的时候要考虑家具的 摆放 摆放家具的时候 家具会占用 a 个 空间 在将家具移动到 合适的位置的时候 需要b个空间 新家的空间v 和 家具的件数n 然后跟着n行 分别是 每件家具的 占用空间 和 移动空间 现在问你 能不能 将 这些 家具 移动到 新家
因为有 移动空间 所以 问题就变得复杂了 . 但是核心思想还是 让 空间尽量慢的减少 在下一个家具没有移动进去之前要尽量的节省剩余空间 而且如果想移进去 剩余空间必须大于等于 家具的 占地空间和移动空间 排序的话 就按照 上个家具的占地空间+这个家具的移动空间 最小就行 (这样每次移动的时候 都会出现相同的状况(程序设计 找 长状态节点的思想))
#include<stdio.h> #include<algorithm> using namespace std; struct hole { int a,b; // 占地 为a 移动空间 为b }; bool cmp(hole a,hole b) { if(a.a+b.b<a.b+b.a) return 1; else if(a.a+b.b==a.b+b.a) return a.b>b.b; else return 0; // 让上次的 已经占地的物品 和 这次的物品的移动空间 尽量小 . } int main() { int t,i,n,v; hole a[1050]; scanf("%d",&t); while(t--) { int flag=1; scanf("%d%d",&v,&n); for(i=0;i<n;i++) scanf("%d%d",&a[i].a,&a[i].b); sort(a,a+n,cmp); for(i=0;i<n;i++) { if(v>=a[i].a&&v>=a[i].b) // 剩下的 空间 大于等于 这次物品的 占地空间和 移动空间 { v-=a[i].a; } else { flag=0; break; } } if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- Build Active Architecture Only
- Break和Continue的一些注意事项
- NSAttributedString UIKit Additions 文章翻译参考
- =Cursor cursor = dbInstance.query(String table,String []Columns, String selection, String[]selectionArgs, String having, string orderBy);参数的意思
- SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <1MB model size阅读笔记
- GCD与NSOperationQueue的区别
- UI进阶之--网易彩票手写plist文件,动态创建控制器与tableViewcell
- 【ios】UIColor,CGColor,CIColor三者的区别和联系
- 334. Increasing Triplet Subsequence
- NSOperationQueue与GCD的使用原则和场景
- question_011-JAVA之Collection
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
- HTML中让表单input等文本框为只读不可编辑但可以获取value值的方法;让文本域前面的内容显示在左上角,居中
- Android UI学习 - FrameLayou和布局优化
- 详解 CALayer 和 UIView 的区别和联系
- 数论+差分 hdu1121 Complete the Sequence
- apue file i/o 学习笔记
- 微信团队讲课笔记 Android 开发(二)UI设计
- UINavigationBar UINavigationItem UIBarButtonItem
- 数字及字母混合的验证码