码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 11729 - Commando War 题解

时间:2014-05-15 04:23:35      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   c   http   int   

安排任务的问题。

思路:

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

原题: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++;
		}
	}
};






UVa 11729 - Commando War 题解,布布扣,bubuko.com

UVa 11729 - Commando War 题解

标签:blog   class   code   c   http   int   

原文地址:http://blog.csdn.net/kenden23/article/details/25704227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!