Problem Solving report codeforces 6E Exposition
Description
There is several days left before the fiftieth birthday of a famous Berland ' s writer Berlbury. In this connection, the local library decided to make a exposition of the works of this famous science-fiction writer. It's decided as well that it's necessary to include into the exposition only those books that were published during a P Articular time period. It is obvious so if the books differ much in size, the visitors would not be like it. That is why the organizers came to the opinion, that the difference between the highest and the lowest books in the Expos Ition should is 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 are 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 the find out who periods of his creative work they would manage to cover. You is asked to help the organizers cope with this hard task.
Input
The first line of the input data contains, numbers separated by a spaceN(1?≤? n? ≤?105 ) andk(0?≤? k. ≤?106 )-the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the H Ighest Books. The second line containsNInteger numbers separated by a space. Each number Hi (1?≤? h i? ≤?106 ) is the height of theI-th book in millimeters.
Output
In the first line of the output data print 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 are not mo Re Than k milllimeters.
In each of the following b lines print both integer numbers separated by a space-indexes of the first and the LA St volumes from each of the required time periods of Berlbury ' s creative work.
Sample Input
Input
3 314 12 10
Output
2 21 22 3
Input
2 010 10
Output
2 11 2
Input
4 58 19) 10 13
Output
2 13 4
The main idea: to give a sequence of n elements, from which the longest sub-sequence is selected, requiring that the maximum value of the element difference in the sub-sequence not exceed K. There are several eldest-son sequences, the length of the subsequence, and the starting and ending positions of the subsequence.
Analysis: Not a monotonous queue of friends first look at my other blog post, the topic follows the question is actually similar. (http://blog.csdn.net/maxichu/article/details/47612497)
we use a monotone queue to dynamically maintain the maximum and minimum values in the sequence. Before and after the establishment of the pointer I, J, once found that the front pointer to the maximum-minimum value >k, try to update the maximum value, and then J go forward, and so on.
on the code:
#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-pointerint 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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Problem Solving report Codeforces 6E Exposition