UVA 11729

UVA 11729

大体是贪心做法,执行时间长的先交代

训练指南原题解

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

struct Job 
{
	int j, b;
	bool operator < (const Job& x) const 
	{
		return j > x.j;
	}
};

int main() 
{
	int n, b, j, kase = 1;
	while(scanf("%d", &n) == 1 && n) 
	{
		vector<Job> v;
		for(int i = 0; i < n; i++) 
		{
			scanf("%d%d", &b, &j); 
			v.push_back((Job){j,b});
		}
		sort(v.begin(), v.end());

		int s = 0;
		int ans = 0;

		for(int i = 0; i < n; i++)
		{
			s += v[i].b;
			ans = max(ans, s+v[i].j);
		}
		printf("Case %d: %d
", kase++, ans);
	}
	return 0;
}

用cmp函数进行排序而非重载运算符

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

struct Job 
{
	int j, b;
	bool operator < (const Job& x) const 
	{
		return j > x.j;
	}
};

int main() 
{
	int n, b, j, kase = 1;
	while(scanf("%d", &n) == 1 && n) 
	{
		vector<Job> v;
		for(int i = 0; i < n; i++) 
		{
			scanf("%d%d", &b, &j); 
			v.push_back((Job){j,b});
		}
		sort(v.begin(), v.end());

		int s = 0;
		int ans = 0;

		for(int i = 0; i < n; i++)
		{
			s += v[i].b;
			ans = max(ans, s+v[i].j);
		}
		printf("Case %d: %d
", kase++, ans);
	}
	return 0;
}

透过泪水看到希望
原文地址:https://www.cnblogs.com/ronnielee/p/9495967.html