Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个c++代码,单调队列

Posted by a280920481 at 2019-07-04 15:32:15 on Problem 2019 and last updated at 2019-07-04 15:32:50
#include <iostream>
#include <deque>
using namespace std;

const int MAX_N = 256;

struct P
{
	int v;
	int n;
	P(const int& v = 0, const int& n = 0)
		:v(v), n(n) {}
};

int dp[2][MAX_N][MAX_N];

int main()
{
	deque<P> dq1, dq2;
	int N, B, K, m, n;
	scanf("%d%d%d", &N, &B, &K);

	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			scanf("%d", &m);
			
			if (!dq1.empty() && dq1.front().n + B <= j)
			{
				dq1.pop_front();
			}
			if (!dq2.empty() && dq2.front().n + B <= j)
			{
				dq2.pop_front();
			}
			while (!dq1.empty() && dq1.back().v > m)
			{
				dq1.pop_back();
			}
			while (!dq2.empty() && dq2.back().v < m)
			{
				dq2.pop_back();
			}
			dq1.push_back(P(m, j));
			dq2.push_back(P(m, j));
			if (j - B + 1 >= 0)
			{
				dp[0][i][j - B + 1] = dq1.front().v;
				dp[1][i][j - B + 1] = dq2.front().v;
			}
		}
		dq1.clear();
		dq2.clear();
	}
	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			const int& m1 = dp[0][j][i];
			const int& m2 = dp[1][j][i];

			if (!dq1.empty() && dq1.front().n + B <= j)
			{
				dq1.pop_front();
			}
			if (!dq2.empty() && dq2.front().n + B <= j)
			{
				dq2.pop_front();
			}
			while (!dq1.empty() && dq1.back().v > m1)
			{
				dq1.pop_back();
			}
			while (!dq2.empty() && dq2.back().v < m2)
			{
				dq2.pop_back();
			}
			dq1.push_back(P(m1, j));
			dq2.push_back(P(m2, j));
			if (j - B + 1 >= 0)
			{
				dp[0][j - B + 1][i] = dq1.front().v;
				dp[1][j - B + 1][i] = dq2.front().v;
			}
		}
		dq1.clear();
		dq2.clear();
	}

	while (K--)
	{
		scanf("%d%d", &m, &n);
		--m;
		--n;
		printf("%d\n", dp[1][m][n] - dp[0][m][n]);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator