標籤:acm 單調隊列 資料結構 多校 codeforces
解題報告 之 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 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
題目大意:給一個n個元素的序列,從中挑出最長的子序列,要求子序列中元素差的最大值不超過k。問有幾個最長子序列,子序列長度,以及這幾個子序列的起始、終止位置。
分析:不會單調隊列的朋友先看看我的另一篇博文,題目跟著道題其實差不多。(http://blog.csdn.net/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 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;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
解題報告 之 CodeForces 6E Exposition