首页 > 代码库 > UVa 11729 - Commando War 题解

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++;
		}
	}
};