HDU3090 Go Home
2015-07-20 10:39
561 查看
Go Home
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
There comes the holiday, Partychen set foot on the way home. He takes some ECNU coins to hire bodyguards to prevent from being robbed before he went home. But the bodyguard takes one coin for every kilometer. If Partychen walks without
bodyguard , he will be robbed one ECNU coin by every robber on every kilometer . Of course , he can choose where to hire bodyguard or where to be robbed as he like.
For example , there are two roads on his way home and he wants to use 8 ECNU coins to hire bodyguard , the first road takes 4 kilometers with 5 robbers ( per kilometer ) and the second takes 5 kilometers with 6 robbers. He could choose the last 3 kilometers
on the first road and the whole kilometers on the second road to hire bodyguard to protect him, and leave the first kilometer on the first road to be robbed by 5 robbers, which he will be robbed 5 ECNU coins.
Now , Partychen want to know how many ECNU coins will be robbed at least.
Input
It consists of multi-case .
Every case starts with two integers N and M ( 0�N�10,000, 0�M�1,000,000,000 ) which means that there are N roads and M ECNU coins to hire bodyguard.
The followed N lines contains two integers D and P (1<=D<=10,000 , 0<=P<=10 ) , which means the length of every road and the number of robbers in every kilometer on this road.
End with N=0 and M=0 .
Output
An integer means the number of ECNU coins to be robbed at least.
Sample Input
2 8
4 5
5 6
3 1
5 10
5 10
5 10
0 0
Sample Output
5
140
题意
回家共有 n (0 <= n <=10,000)段路,有 m (0 <= m <= 1,000,000,000)块钱可以雇佣保镖。每段路有ai(0 <= i < n)千米,每千米有bi(0 <= i < n)个强盗。雇用一个保镖一千米需要花费一块钱,可以保证这一千米内不被强盗抢劫。每千米每个强盗抢一块钱。最少被抢劫的钱数是多少。
分析
以每千米的强盗数降序排序,贪心
AC代码如下
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
int d,p;
};
Node roads[10001];
int n,m;
int sum;
bool cmp(Node n1,Node n2)
{
return n1.p > n2.p;
}
int main()
{
while(scanf("%d %d", &n, &m), m!=0 || n!=0)
{
sum = 0;
for(int i=0; i<n; i++)
{
scanf("%d%d", &roads[i].d, &roads[i].p);
sum += roads[i].d*roads[i].p;
}
sort(roads, roads+n, cmp);<span style="white-space:pre"> </span>//以强盗数降序sort
for(int i=0; i<n; i++)<span style="white-space:pre"> </span>//贪心
{
if(m > roads[i].d)
{
m -= roads[i].d;
sum -= roads[i].d * roads[i].p;
}
else
{
sum -= m * roads[i].p;
break;
}
}
printf("%d\n",sum);
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
There comes the holiday, Partychen set foot on the way home. He takes some ECNU coins to hire bodyguards to prevent from being robbed before he went home. But the bodyguard takes one coin for every kilometer. If Partychen walks without
bodyguard , he will be robbed one ECNU coin by every robber on every kilometer . Of course , he can choose where to hire bodyguard or where to be robbed as he like.
For example , there are two roads on his way home and he wants to use 8 ECNU coins to hire bodyguard , the first road takes 4 kilometers with 5 robbers ( per kilometer ) and the second takes 5 kilometers with 6 robbers. He could choose the last 3 kilometers
on the first road and the whole kilometers on the second road to hire bodyguard to protect him, and leave the first kilometer on the first road to be robbed by 5 robbers, which he will be robbed 5 ECNU coins.
Now , Partychen want to know how many ECNU coins will be robbed at least.
Input
It consists of multi-case .
Every case starts with two integers N and M ( 0�N�10,000, 0�M�1,000,000,000 ) which means that there are N roads and M ECNU coins to hire bodyguard.
The followed N lines contains two integers D and P (1<=D<=10,000 , 0<=P<=10 ) , which means the length of every road and the number of robbers in every kilometer on this road.
End with N=0 and M=0 .
Output
An integer means the number of ECNU coins to be robbed at least.
Sample Input
2 8
4 5
5 6
3 1
5 10
5 10
5 10
0 0
Sample Output
5
140
题意
回家共有 n (0 <= n <=10,000)段路,有 m (0 <= m <= 1,000,000,000)块钱可以雇佣保镖。每段路有ai(0 <= i < n)千米,每千米有bi(0 <= i < n)个强盗。雇用一个保镖一千米需要花费一块钱,可以保证这一千米内不被强盗抢劫。每千米每个强盗抢一块钱。最少被抢劫的钱数是多少。
分析
以每千米的强盗数降序排序,贪心
AC代码如下
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
int d,p;
};
Node roads[10001];
int n,m;
int sum;
bool cmp(Node n1,Node n2)
{
return n1.p > n2.p;
}
int main()
{
while(scanf("%d %d", &n, &m), m!=0 || n!=0)
{
sum = 0;
for(int i=0; i<n; i++)
{
scanf("%d%d", &roads[i].d, &roads[i].p);
sum += roads[i].d*roads[i].p;
}
sort(roads, roads+n, cmp);<span style="white-space:pre"> </span>//以强盗数降序sort
for(int i=0; i<n; i++)<span style="white-space:pre"> </span>//贪心
{
if(m > roads[i].d)
{
m -= roads[i].d;
sum -= roads[i].d * roads[i].p;
}
else
{
sum -= m * roads[i].p;
break;
}
}
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解