解题报告 之 CodeForces 6E Exposition

本文提供了解决CodeForces6E Exposition问题的算法报告,包括输入输出规范、问题描述、算法分析及代码实现,旨在帮助读者理解和解决相关问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解题报告 之 CodeForces 6E Exposition


Description

There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.

The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.

Input

The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.

Output

In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.

In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.

Sample Input

Input
3 3
14 12 10
Output
2 2
1 2
2 3
Input
2 0
10 10
Output
2 1
1 2
Input
4 5
8 19 10 13
Output
2 1
3 4

题目大意:给一个n个元素的序列,从中挑出最长的子序列,要求子序列中元素差的最大值不超过k。问有几个最长子序列,子序列长度,以及这几个子序列的起始、终止位置。

分析:不会单调队列的朋友先看看我的另一篇博文,题目跟着道题其实差不多。(http://blog.youkuaiyun.com/maxichu/article/details/47612497

我们用单调队列动态维护序列中的最大值和最小值。设立前后指针i、j,一旦发现前指针走到了最大值-最小值>k的地方,就试图更新一下最大值,然后 j 再往前走,以此类推。

上代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<deque>
#include<utility>
#include<vector>
using namespace std;

const int MAXN = 1e5 + 10;

int book[MAXN];
vector<pair<int, int> > p;
deque<int> high;
deque<int> low;

int main()
{
	int n, k;
	while(cin >> n >> k)
	{
		for(int i = 1; i <= n; i++)
		{
			scanf( "%d", &book[i] );
		}

		p.clear();
		high.clear();
		low.clear();

		int i, j; //a is pre pointer
		int ans = 0, cnt = 0;
		for(i = j = 1; i <= n; i++)
		{

			while(!high.empty()&&high.back() < book[i]) high.pop_back();
			high.push_back( book[i] );

			while(!low.empty()&&low.back() > book[i]) low.pop_back();
			low.push_back( book[i] );

			while(!high.empty() && !low.empty() && high.front() - low.front()>k)
			{
				if(p.empty() || i - j >ans)
				{
					p.clear();
					cnt = 1;
					ans = i - j;
					p.push_back( make_pair( j, i - 1 ) );
				}

				else if(!p.empty() && i - j == ans)
				{
					cnt++;
					p.push_back( make_pair( j, i - 1 ) );
				}
				if(high.front() == book[j]) high.pop_front();
				if(low.front() == book[j]) low.pop_front();
				j++;
			}
		}

		while(j <=n)
		{
			if(p.empty() || i - j >ans)
			{
				p.clear();
				cnt = 1;
				ans = i - j;
				p.push_back( make_pair( j, i - 1 ) );
			}

			else if(!p.empty() && i - j == ans)
			{
				cnt++;
				p.push_back( make_pair( j, i - 1 ) );
			}
			if(high.front() == book[j]) high.pop_front();
			if(low.front() == book[j]) low.pop_front();
			j++;

		}
		
		cout << ans << " " << cnt << endl;
		for(int i = 0; i < p.size(); i++)
		{
			cout << p[i].first << " " << p[i].second << endl;
		}
	}
	return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值