Parade poj3926 hdoj2490

可以从最南边任意位置开始,可以在最北边任意位置结束,单调队列优化,这题poj上卡输入输出,输入加速+G++可以过

#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>
 
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::make_pair;
using std::greater;
using std::endl;

const int MAXN(110);
const int MAXM(10010);
const int INFI((INT_MAX-1)/3);

int s_val[MAXN][MAXM];
int s_dist[MAXN][MAXM];
int table[MAXM][2];
int cur, last;


template <class T>
inline bool scan_d(T &ret) {
   char c; int sgn;
   if(c=getchar(),c==EOF) return 0; //EOF
   while(c!='-'&&(c<'0'||c>'9')) c=getchar();
   sgn=(c=='-')?-1:1;
   ret=(c=='-')?0:(c-'0');
   while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
   ret*=sgn;
   return 1;
}

inline int value1(int i, int *sp)
{
	return table[i][last]-sp[i-1];
}

inline int value2(int i, int *sp)
{
	return table[i][last]+sp[i-1];
}

int deque[MAXM];
int front, rear;

int main()
{
	int n, m, K;
	while(scanf("%d%d%d", &n, &m, &K), n|| m|| K)
	{
		++n;
		for(int i = 1; i <= n; ++i)
		{
			s_val[i][0] = 0;
			for(int j = 1; j <= m; ++j)
			{
				scan_d(s_val[i][j]);
				s_val[i][j] += s_val[i][j-1];
			}
		}
		for(int i = 1; i <= n; ++i)
		{
			s_dist[i][0] = 0;
			for(int j = 1; j <= m; ++j)
			{
				scan_d(s_dist[i][j]);
				s_dist[i][j] += s_dist[i][j-1];
			}
		} 
		cur = 0;
		last = 1;
		for(int i = 1; i <= m+1; ++i)
			table[i][last] = 0;
		for(int i = n; i >= 1; --i)
		{
			front = 0;
			rear = -1;
			for(int j = 1; j <= m+1; ++j)
			{
				table[j][cur] = -INFI;
				while(front <= rear && value1(j, s_val[i]) > value1(deque[rear], s_val[i]))
					--rear;
				deque[++rear] = j;
				while(front <= rear && s_dist[i][j-1]-s_dist[i][deque[front]-1] > K)
					++front;
				table[j][cur] = max(table[j][cur], s_val[i][j-1]+value1(deque[front], s_val[i]));
			}
			front = 0;
			rear = -1;
			for(int j = m+1; j >= 1; --j)
			{
				while(front <= rear && value2(j, s_val[i]) > value2(deque[rear], s_val[i]))
					--rear;
				deque[++rear] = j;
				while(front <= rear && s_dist[i][deque[front]-1]-s_dist[i][j-1] > K)
					++front;
				table[j][cur] = max(table[j][cur], value2(deque[front], s_val[i])-s_val[i][j-1]);
			}

			cur ^= 1;
			last ^= 1;
		}
		int ans = -INFI;
		for(int i = 1; i <= m+1; ++i)
			ans = max(ans, table[i][last]);
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值