Sequence Partitioning poj3245

 题意不太好懂,大概就是给出n对数,每对数都是有序对即<Ai, Bi>, 求这些有序对的一个划分,需要满足给出的俩个条件

条件一:设对序列划分的第k部分为Tk,则对于任意的k1 < k2, i∈Tk1, j∈Tk2,都有Bi > Aj; 

条件二:   ∑ max(Ai) (i∈Tk) <= LIMIT,LIMIT为题目给出;

同时要最小化max(∑ Bi  (i∈Tk) 

分析后不难得出条件一可以通过预处理给出的序列来满足,预处理后的序列仍然是有序对,题目要求的是最大值最小化,不难想到二分后判定是否可以满足条件二,而判定的过程和poj3017一样了,预处理序列比较有技巧,开始想到了一个用RMQ处理的方法,但是写起来很烦,从hzq神牛那里看了一个利用排序的方法,很巧妙。


#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>
#include <bitset>

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::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
typedef long double LF;

const int MAXN(50010);
const int MAXM(100010);
const int MAXE(100010);
const int MAXK(6);
const int HSIZE(31313);
const int SIGMA_SIZE(26);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(20100403);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> T ABS(const T &a){return a < 0? -a: a;}

int p1[MAXN], p2[MAXN];
int A[MAXN], B[MAXN];
LL table[MAXN];
LL sum[MAXN];
int N, LI;
multiset<LL> st;
deque<LL> que;
bool comp(int a, int b)
{
	return B[a] < B[b];
}	

bool legal(LL value)
{
	st.clear();
	que.clear();
	table[0] = 0;
	int last = 0;
	for(int i = 1; i <= N; ++i)
	{
		while(sum[i]-sum[last] > value) ++last;
		if(last == i) return false;
		while(!que.empty() && que.front() <= last)
		{
			int temp = que.front();
			que.pop_front();
			if(!que.empty())
				st.erase(table[temp]+A[que.front()]);
		}
		while(!que.empty() && A[i] >= A[que.back()])
		{
			int temp = que.back();
			que.pop_back();
			if(!que.empty())
				st.erase(table[que.back()]+A[temp]);
		}
		if(!que.empty())
			st.insert(table[que.back()]+A[i]);
		que.push_back(i);
		table[i] = table[last]+A[que.front()];
		if(!st.empty())
			checkmin(table[i], *st.begin());
	}
	return table[N] <= LI;
}

int main()
{
	int n;
	while(~scanf("%d%d", &n, &LI))
	{
		while(n == 0);
		for(int i = 1; i <= n; ++i)
		{
			scanf("%d%d", A+i, B+i);
			p1[i] = p2[i] = i;
		}
		sort(p1+1, p1+1+n, comp);   //按照B的值间接排序
		for(int i = 1, j = n; j > 0; --j)   //从大到小枚举,保证找到的是必须何必的最右边
			while(i <= n && B[p1[i]] <= A[j])  //找到每个有序对必须合并的最右边
				checkmax(p2[p1[i++]], j);
		N = 0;
		for(int i = 1; i <= n;)
		{
			A[++N] = A[i];
			B[N] = B[i];
			int l = i+1, r = max(i, p2[i]);
			while(l <= r)
			{
				checkmax(A[N], A[l]);
				B[N] += B[l];
				checkmax(r, p2[l]);
				++l;
			}
			i = l;
		}
		for(int i = 1; i <= N; ++i)
			sum[i] = sum[i-1]+B[i];
		LL ans;
		LL low = 0, hig = INT_MAX;
		while(low <= hig)
		{
			LL m = low+(hig-low)/2;
			if(legal(m))
			{
				ans = m;
				hig = m-1;
			}
			else
				low = m+1;
		}
		printf("%I64d\n", ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值