UVa 11729 - Commando War 题解

安排任务的问题。

思路:

因为分报告时间和实际执行任务时间,这里要做的就是尽量使得报告时间和实际执行任务的时间重合,并行,那么就可以减小总时间。

原题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2829

0.012s的算法,达到网站最高纪录。

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using std::sort;

class CommandoWar11729
{
	static const int MAX_BUF = 5120;
	int st, len;
	char inBuf[MAX_BUF];

	char getFromBuf()
	{
		if (st >= len)
		{
			len = fread(inBuf, sizeof(char), MAX_BUF, stdin);
			st = 0;
		}
		return inBuf[st++];
	}
	int intFromBuf()
	{
		char c = getFromBuf();
		while ((c < '0' || '9' < c) && len)
		{
			c = getFromBuf();
		}
		int num = 0;
		while ('0' <= c && c <= '9' && len)
		{
			num = (num<<3) + (num<<1) + (c - '0');
			c = getFromBuf();
		}
		return num;
	}
	struct BriJob
	{
		int bri, job;
		bool operator<(const BriJob &bj) const
		{
			return job > bj.job;
		}
	};
public:
	CommandoWar11729() : st(0), len(0)
	{
		int n = 0, C = 1;
		while ((n = intFromBuf()) != 0)
		{
			BriJob * bj = (BriJob *) malloc(n * sizeof(BriJob));
			for (int i = 0; i < n; i++)
			{
				bj[i].bri = intFromBuf(), bj[i].job = intFromBuf();
			}
			sort(bj, bj + n);
			long long times = 0;
			int left = 0;
			for (int i = 0; i < n; i++)
			{
				times += bj[i].bri;
				left = std::max(left - bj[i].bri, bj[i].job);
			}
			times += left;
			printf("Case %d: %lld\n", C, times);
			C++;
		}
	}
};






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值