HDU 3177 Crixalis's Equipment (贪心,差值)
39 HDU 贪心 差值
2023-09-11 14:17:19 时间
题意:判断 n 件物品是否可以搬进洞里,每件物品有实际体积A和移动时的额外体积 B 。
析:第一反应就是贪心,一想是不是按B从大到小,然后一想,不对,比如体积是20,第一个
是A=11, B=19.第二个是A = 1,B = 18.很明显不对。
我们取AB的差值,进行贪心,为什么呢?
我反过来想一下,假设我们把两个物品搬到洞中,所需要的洞的最小体积。第一个,A = 2,B = 10.
第二个,A = 5, B = 10.如果先放第一个,第放第二个,体积为12,如果反过来则是15,很明显是先放
差值大的,这样为下一个放留下较大的空间。
代码如下:
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map> #include <cctype> using namespace std; const int maxn = 1000 + 5; struct node{ int A, B; bool operator < (const node &p) const { return (B - A) > (p.B - p.A); } }; node a[maxn]; int main(){ // freopen("in.txt", "r", stdin); int T, n, v; cin >> T; while(T--){ scanf("%d %d", &v, &n); for(int i = 0; i < n; ++i) scanf("%d %d", &a[i].A, &a[i].B); sort(a, a+n); bool ok = true; for(int i = 0; i < n; ++i){ if(v < a[i].B){ ok = false; break; } v -= a[i].A; } if(ok) puts("Yes"); else puts("No"); } return 0; }
相关文章
- linux git 报错提示 fatal: 'origin' does not appear to be a git repository 解决办法
- PRJ0003 : Error spawning 'midl.exe'
- MySQL 从 5.5 升级到 5.6,启动时报错 [ERROR] Plugin 'InnoDB' init function returned error
- PHP文件夹文件拷贝/复制函数 dir_copy($src = '', $dst = '')
- python中if __name__ == '__main__': 的解析
- 【Feign调用异常】org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
- $.store.book[?(@.category=='fiction')].category
- Python 中的 if __name__ == '__main__' 该如何理解
- mysql: unknown variable 'character-set-client=utf8'
- hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)
- TypeError: 'bases' is null or not an object。IE8 bug 腐朽的对象
- HDU 4430 Yukari's Birthday(二分)
- HDU 4706 Children's Day(简单模拟)
- error CS0117: `UnityEditor.EditorUtility' does not contain a definition for `GetAssetPreview'
- Unmet dependencies. Try 'apt-get -f install' with no packages
- [Angular] 'providedIn' for service
- [Javascript] Safer property access with Lodash's 'get' method
- PING[ARC2]: Heartbeat failed to connect to standby 'phydb'. Error is 16191.
- 大叔经验分享(13)spark运行报错WARN Utils: Service 'sparkDriver' could not bind on port 0. Attempting port 1.
- 源码安装mysql5.6x启动报错:[ERROR] Can't find messagefile '/data/mysqldata/3306/english/errmsg.sys'
- [Angular] Export directive functionalities by using 'exportAs'
- Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':classpath'.
- [FAQ] golang-migrate/migrate error: default addr for network '127.0.0.1:3306' unknown
- Atitit 图像处理 平滑 也称 模糊, 归一化块滤波、高斯滤波、中值滤波、双边滤波)
- Atitit ati licenseService 设计原理
- atitit. web组件化原理与设计
- Atitit. Ati IDE 开发平台的第一版规划
- Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案
- atitit.md5算法的原理 与 总结
- spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'
- QT新建工程编译出现"Moc'ing xxx.h..." 找不到文件或者error MSB6006: "cmd.exe" exited with code 3.
- c++报错: No viable overloaded '=' 临时变量与引用参数 const
- kibana We couldn't activate monitoring
- Python中的魔术(双下划线'__xxx__')方法详解
- 用acme.sh申请免费ssl证书-let's encrypt
- Let's Encrypt 免费通配符 SSL 证书申请教程——但是也需要email,域名所有权等,如果是黑产用的话会这样用吗?会不会暴露自己身份???