POJ 3262 Protecting the Flowers
来源:互联网 发布:淘宝助理是免费的吗 编辑:程序博客网 时间:2024/05/24 03:14
FJ的牛跑到他的花坛里吃他的花,FJ看到了,心想:小母牛你爬单杠,于是乎他要把它们赶回栏里面呀。N头牛,T_i 是牛i要回到栏里需要的时间,D_i 牛i的破坏力(每分钟能毁掉的花的数量)。还有一个重要条件是:FJ要把牛i赶回栏需要2* T_i 时间。
输入:
- line1: N
line2~N+1:T_i 和D_i
输出:牛最少破坏多少花
初看此题可能觉得策略是:首先,将破坏力最大的牛先赶走,然后,破坏力相同的赶走时间短的,其实不然,此题其实是一个与比率有关的题(单位时间破坏力) ,单位时间破坏力越高的越先带走。
#include<iostream>#include<utility>#include<queue>using namespace std;class cmp {public: bool operator() (const pair<int, int>& a, const pair<int, int>& b) { return a.first*b.second > b.first*a.second; }};priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> que;int main() { int n; long long cost = 0; int ratio = 0; cin >> n; for (int i = 0; i < n; ++i) { pair<int, int> cows; cin >> cows.first >> cows.second; que.push(cows); } pair<int, int> it = que.top(); ratio += 2 * it.first; que.pop(); while (!que.empty()) { pair<int, int> it = que.top(); cost += it.second*ratio; ratio += 2 * it.first; que.pop(); } cout << cost << endl; system("pause"); return 0;}
这里需要特别注意cost数据类型,如果你使用int提交后会得到WA。第一次提交我用的是int,错误;改成long long后,提交通过。
0 0
- POJ 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ-3262-Protecting the Flowers
- Poj 3262 Protecting the flowers
- POJ 3262 Protecting the Flowers
- POJ-3262-Protecting the Flowers
- POJ-3262-Protecting the Flowers
- poj-3262-Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ 3262 - Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- mktime函数实现获取当前系统时间
- hitTest
- 配置各类硬件
- C++——Const总结
- .NET执行SQL插入时间的问题
- POJ 3262 Protecting the Flowers
- Android中文API:Android 如何保持屏幕长亮?
- C Tricks(三)—— 以一维数组的形式对二维数组赋值
- 面试算法-数组反转
- 如何登陆google
- AndroidStudio降低编译版本报错
- 题目要求:一个人从x年开始庆祝生日,直到死去为止,每过一年就把当年的年龄与以前相加得到一个sum值,求开始过生日的年龄,与寿命
- 两次讲座
- crash工具debug内核dump文件