Processor Seoul 2008

求最大值最小化,比较容易想到二分,如何判断是否合法呢,采用的策略为执行当前时间内可以执行的结束时间最早的任务,题目时间点都是 <= 20000,所以可以直接模拟,选任务时用最小堆优化,总的时间复杂度为O(log(20000)*20000*log(10000))。

给自己提个醒, 注意:对地址操作的最小堆(类似于间接排序), 在内存回收过程中,回收的应为间接数组的第一个单元即p[1],而不是最后一个单元p[size],因为操作的是地址所以p[1]=p[size],实际上相当于把p[1]所记录的地址删掉了, 所以回收的应为p[1]所记录的地址单元,只一点极容易出错!!!


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits> 
#include <set>
#include <string> 
#include <sstream>
#include <utility>
#include <ctime>
//#pragma comment(linker, "/STACK:1024000000,1024000000") 

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;
using std::make_pair;
using std::greater;

const int MAXN(20010);

struct NODE
{
	int l, r, w;
	friend bool operator < (const NODE &op1, const NODE &op2)
	{
		return op1.r < op2.r;
	}
};

vector<NODE> vec[MAXN];

template<typename T>
struct HEAP
{
	T arr[10010];
	T *pool[10010];
	T *p[10010];
	int size;
	int ind;
	int full;

	void init()
	{
		size = 0;
		ind = 0;
		full = 0;
	}

	void swim(int sour)
	{
		int fa = sour >> 1;
		T *temp = p[sour];
		while(fa >= 1 && *temp < *(p[fa]))
		{
			p[sour] = p[fa];
			sour = fa;
			fa >>= 1;
		}
		p[sour] = temp;
	}

	void sink(int sour)
	{
		int ch = sour << 1;
		T *temp = p[sour];
		while(ch <= size)
		{
			if(ch < size && (*p[ch+1]) < (*p[ch]))
				++ch;
			if(!(*p[ch] < *temp))
				break;
			p[sour] = p[ch];
			sour = ch;
			ch <<= 1;
		}
		p[sour] = temp;
	}
	void push(const NODE &value)
	{
		if(full)
		{
			*pool[full] = value;
			p[++size] = pool[full--];
		}
		else
		{
			arr[ind] = value;
			p[++size] = arr+ind++;
		}
		swim(size);
	}

	bool empty()
	{
		return size == 0;
	}

	NODE &top()
	{
		return *(p[1]);
	}
	
	void pop()
	{
		pool[++full] = p[1];   //注意回收的是p[1]所记录的地址
		p[1] = p[size--];
		sink(1);
	}
};

HEAP<NODE> heap;
int S, E;

bool legal(int value)
{
	heap.init();
	NODE cur;
	bool flag(false);
	for(int i = S; i <= E; ++i)
	{
		if(flag)
		{
			int temp = cur.w-value;
			while(temp <= 0 && !heap.empty())
			{
				cur = heap.top();
				heap.pop();
				temp = cur.w+temp;
			}
			if(temp <= 0 && heap.empty())
				flag = false;
			else
			{
				if(cur.r <= i)
					return false;
				cur.w = temp;
				heap.push(cur);
			}
		}
		int ts = vec[i].size();
		for(int j = 0; j < ts; ++j)
			heap.push(vec[i][j]);
		if(!heap.empty())
		{
			cur = heap.top();
			heap.pop();
			flag = true;
		}
		else
			flag = false;
	}
	return true;
}

int main()
{
	int T;
	scanf("%d", &T);
	NODE temp;
	while(T--)
	{
		S = 20000;
		E = 1;
		int n;
		scanf("%d", &n);
		int l = 0, r = 0;
		for(int i = 0; i < n; ++i)
		{
			scanf("%d%d%d", &temp.l, &temp.r, &temp.w);
			l += temp.r-temp.l;
			r += temp.w;
			S = min(S, temp.l);
			E = max(E, temp.r);
			vec[temp.l].push_back(temp);
		}
		l = r/l;
		if(!l)
			++l;
		while(l < r)
		{
			int m = (l+r) >> 1;
			if(legal(m))
				r = m;
			else
				l = m+1;
		}
		printf("%d\n", l);
		for(int i = S; i <= E; ++i)
			vec[i].clear();
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值