USACO Section 1.3 Greedy Algorithm
来源:互联网 发布:java web工作内容 编辑:程序博客网 时间:2024/06/05 05:17
两道贪心题, 刷水题完全没有成就感, 更不用说刷水题还一直wa了 Orz
Mixing Milk
按照农夫提供的牛奶价格 sort一遍农夫, 贪心
#include <bits/stdc++.h>using namespace std;struct Farmer { int p, w; void read() { scanf("%d %d", &p, &w); } bool operator < (const Farmer & b) const { return p < b.p; }} a[5005];int main() { freopen("milk.in", "r", stdin); freopen("milk.out", "w", stdout); int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) a[i].read(); sort(a, a + m); int t = n, ans = 0; for (int i = 0; i < m && t; i++) { int b = min(t, a[i].w); t -= b; ans += b * a[i].p; } printf("%d\n", ans);}
sort 相邻两个有牛的牛棚的间距进行贪心, 这题我没考虑到 m 的值可能能大于有牛的牛棚数量, 更无奈的是之前的程序在我本地的 C++ 11 版本的运行结果是正确的, 导致一直debug无果, 幸好后来反应过来, 不过也算是栽在了水题上面了, 还是太弱啊 ...
#include <bits/stdc++.h>using namespace std;int a[222], b[222];int main() { freopen("barn1.in", "r", stdin); freopen("barn1.out", "w", stdout); int n, m, c; scanf("%d %d %d", &m, &c, &n); for (int i = 0; i < n; i++) scanf("%d", a + i); sort(a, a + n); n--; for (int i = 0; i < n; i++) b[i] = a[i + 1] - a[i] - 1; sort(b, b + n); int ans = a[n] - a[0] + 1; for (int i = 1; i < m && n - i >= 0; i++) ans -= b[n - i]; printf("%d\n", ans);}
0 0
- USACO Section 1.3 Greedy Algorithm
- USACO 之 Section 1.3 Greedy Algorithm
- Section 1.3 Greedy Algorithm
- USACO Training Section 1.3 Greedy Algorithm 贪心算法
- Section 1.2 Translate:USACO/Greedy Algorithm
- usaco 1.3.1 Greedy Algorithm
- USACO:Section 1.1 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USACO Section 1.1.2 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USACO Section 1.1 Greedy Gift Givers
- USACO之Section 1.1.2 PROB Greedy Gift Givers
- greedy algorithm
- Greedy algorithm
- USACO section 1.3
- USACO/milk Section 1.3
- USACO Section 1.3 题解
- 数据库MySQL2 存储过程 事务
- NetBeans的快捷键使用汇总
- 使用_beginThreadex创建多线程(C语言版多线程)
- linux下安装ImageMagick
- java笔记——初学面向对象06
- USACO Section 1.3 Greedy Algorithm
- 常用Linux命令
- Oracle 树操作(select…start with…connect by…prior)
- 两个表,一个表中的两列关联另一个表的id,如何将这个表中的两列显示为另一个表id对应的内容
- IE、360浏览器兼容解决
- C#去掉窗口最小化时左下角的窗口
- JSP 中九大内置对象和四大作用域
- Xcode升到6.4插件失效不能用解决方法
- 贝塞尔曲线的计算方式与js计算