Uva 11729 - Commando War ( 贪心 + 水 )
2013-11-21 15:11
399 查看
题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2829
题目大意:有n个人需要你分配任务,第 i 个部下交代任务需要 Bi 时间,执行任务需要 Ji 时间,要求最早完成任务,请输出最后完成对的工作的时间。
题目分析:贪心(先排序再处理),贪心算法切记要设置结构体,这样才能记住原来的位置和排序后的数字的联系~~
代码实现:#include<stdio.h>
#include<algorithm>
using namespace std;
struct Node
{
int j, b;
}Job[1005];
int cmp(Node x,Node y)
{
if(x.j > y.j)
return 1;
return 0;
}
int main(){
int n , cas = 1;
while( scanf("%d", &n) && n ){
for(int i = 0; i < n; i++)
scanf("%d%d", &Job[i].b, &Job[i].j);
sort(Job, Job + n, cmp );
int total = 0;
int sum = 0;
for(int i = 0; i < n; i++){
sum += Job[i].b;
total = max(total, sum + Job[i].j);
}
printf("Case %d: %d\n", cas++, total);
}
return 0;
}
在信息竞赛入门经典训练指南中的做法:
1.在结构体中实现运算符重载,即
struct node
{
int B, J;
bool operator < (const node &a) const
{
return J > a.J;
}
}A[maxn];
2.使用了系统自带的数组 vector ,vector 是具有线程安全,有另外加写锁代码的,这样的话性能会减低的,还不如直接定义个数组自己编写。
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2829
题目大意:有n个人需要你分配任务,第 i 个部下交代任务需要 Bi 时间,执行任务需要 Ji 时间,要求最早完成任务,请输出最后完成对的工作的时间。
题目分析:贪心(先排序再处理),贪心算法切记要设置结构体,这样才能记住原来的位置和排序后的数字的联系~~
代码实现:#include<stdio.h>
#include<algorithm>
using namespace std;
struct Node
{
int j, b;
}Job[1005];
int cmp(Node x,Node y)
{
if(x.j > y.j)
return 1;
return 0;
}
int main(){
int n , cas = 1;
while( scanf("%d", &n) && n ){
for(int i = 0; i < n; i++)
scanf("%d%d", &Job[i].b, &Job[i].j);
sort(Job, Job + n, cmp );
int total = 0;
int sum = 0;
for(int i = 0; i < n; i++){
sum += Job[i].b;
total = max(total, sum + Job[i].j);
}
printf("Case %d: %d\n", cas++, total);
}
return 0;
}
在信息竞赛入门经典训练指南中的做法:
1.在结构体中实现运算符重载,即
struct node
{
int B, J;
bool operator < (const node &a) const
{
return J > a.J;
}
}A[maxn];
2.使用了系统自带的数组 vector ,vector 是具有线程安全,有另外加写锁代码的,这样的话性能会减低的,还不如直接定义个数组自己编写。
相关文章推荐
- 强烈的求知欲望
- 买楼10大误解
- 1.一些 贪心算法 的简单思维题:
- 最大容器
- 满足和为定值的两个数或多个数
- Codeforces 193 Div 2
- Sicily 2013. Pay Back
- HDU-1009 FatMouse' Trade
- uva11729
- [CTSC2007]挂缀pendant
- Pku2376 Cleaning Shifts
- Pku2054 Color a Tree
- BZOJ 1909 Berth Allocation
- 七夕祭( Poetize系列)
- 快递员工作-贪心-区间
- [Usaco 2011 Dec]Umbrellas for Cows
- [SCOI2009]生日礼物
- [usaco2004 dec]打扫机房II-Pku2376 Cleaning Shifts(区间问题)
- Pku2054 Color a Tree(并查集优化+堆优化+贪心)
- hdu4608