博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电ACM——蝎子搬新家(贪心)
阅读量:4049 次
发布时间:2019-05-25

本文共 2601 字,大约阅读时间需要 8 分钟。

Problem Description

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.

Input

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.

Output

For each case output “Yes” if Crixalis can move all his equipment into the new hole or else output “No”.

Sample Input

2

20 3

10 20
3 10
1 7

10 2

1 10
2 11

Sample Output

Yes

No

突破口:要找那种占空间,即a尽量小的,同时要先装那些需要临时空间,即b大的,这样才能装的更多的equipment当b-a越大时,说明这就是我们所要找的先装进去的equipment。

代码如下:

#include
#include
using namespace std;struct equip{ int a; int b; int v; //v=b-a,用于衡量哪个应该先装}e[1005];bool cmp(equip x,equip y) //按v从大到小排序,v同,按b从大到小排{ if(x.v!=y.v) return x.v>y.v; return x.b>y.b; }int main(){ int T,n,V; int i,j; int flag1,flag2,flag3,left,sum; scanf("%d",&T); while(T--) { flag1=flag2=flag3=1;sum=0; // printf("\n"); scanf("%d %d",&V,&n); left=V; for(i=0;i<=n-1;i++) { scanf("%d%d",&e[i].a,&e[i].b); e[i].v=e[i].b-e[i].a; sum+=e[i].a; if(e[i].b>V) flag1=0; } if(sum>V) flag2=0; if(flag1&&flag2) { sort(e,e+n,cmp); // for(i=0;i<=n-1;i++) printf("%d %d %d\n",e[i].a,e[i].b,e[i].v); for(i=0;i<=n-1;i++) { if(left>=e[i].b) { left-=e[i].a; } else { flag3=0;break; } } if(flag3) printf("Yes\n"); else printf("No\n"); } else printf("No\n"); } return 0;}

小结:

AC思路:
1.要尽可能多装equipment;
2.首选那些a相对较小的;
3.有些a很小,但b很大,这种东西就应该先装,由局部扩展到整体,则应先选那些a相对b较小的东西。
4.不要单纯只看见一个物理量,如只考虑b的大小而不考虑a的大小。

转载地址:http://vfdci.baihongyu.com/

你可能感兴趣的文章
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
idea讲web项目部署到tomcat,热部署
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
微信小程序开发全线记录
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>