题目分析:贪心(先排序再处理),贪心算法切记要设置结构体,这样才能记住原来的位置和排序后的数字的联系~~
代码实现:
#include#include 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 是具有线程安全,有另外加写锁代码的,这样的话性能会减低的,还不如直接定义个数组自己编写。