UVa 11729突击战

  有n个部下,每个部下需要完成一项任务,第i个部下需要花Bi分钟交待任务,然后执行Ji分钟后完成任务,不能同时给两个部下交待任务,但部下们可以同时执行各自任务。输入要求第一行为部下个数,以下每行两个整数B(交待任务的时间)J(执行任务的时间)。

  分析:执行任务时间长的先交待,即应该从大到小排序任务时间,这是一个贪心算法。

 

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Job
{
	int j, b;//j is the time to finish mission,b is the time to tell the mission, you need to tell the mission first
	bool operator <  (const Job &x) const
	{
		return j > x.j;
	}
};

int main()
{
	int n, b, j;//n is the number of soldiers
	cout << "please input you soldiers' number" << endl;
	cin >> n;
	vector <Job> v;
	for (int i = 0; i < n; i++)
	{
		cin >> b;
		cin >> j;
		v.push_back(job);
	}
	sort(v.begin(), v.end());
	int s = 0;
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		s = s + v[i].b;
		ans = max(ans, s + v[i].j);
	}
	cout << ans << endl;
	return 0;
}
但以上程序在VS2015中编译未能通过,在V.push_back((Job){j,b})报错应输入表达式。

查资料后发现应该在定义结构体时增加一个构造函数:

Job(int m, int n) :j(m), b(n) {}
在压入容器前应该构造Job对象job:
 
Job job(j,b);
在使用sort时必须要支持<运算符,这也是本程序中重载运算符的原因,关于C++的迭代器和构造函数方面尚存很多知识漏洞,亟待努力。
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值