1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 *  Copyright (C) 2021  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;


float pop(vector<float>& stack) {
	float top = stack.back();
	stack.pop_back();
	return top;
}


void push(vector<float>& stack, int start, int end, float median) {
	stack.push_back(median);
	stack.push_back(end);
	stack.push_back(start);
}


void trim(vector<float>& stack, set<pair<int, int>>& subsets, vector<int>& ranks) {
	// with every removal of rank < current median,
	// median += 0.5 and score += 1 - 1

	int start, end;
	float median;

	while (!stack.empty()) {
		start = pop(stack);
		end = pop(stack);
		median = pop(stack);

		if (ranks[start] < median) {
			subsets.insert(make_pair(start + 1, end));
			push(stack, start + 1, end, median + 0.5);
		}

		if (ranks[end - 1] < median) {
			subsets.insert(make_pair(start, end - 1));
			push(stack, start, end - 1, median + 0.5);
		}
	}
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n, rank;
	cin >> n;

	vector<int> ranks;
	ranks.reserve(n);

	for (int i = 0; i < n; ++i) {
		cin >> rank;
		ranks.push_back(rank);
	}

	float median = (1 + n) / 2;
	int best = 2 * n + 1;

	set<pair<int, int>> subsets;

	int start = 0;
	int end = n;
	// special case: full sequence
	subsets.insert(make_pair(start, end));

	vector<float> stack;
	stack.reserve(3 * n);

	// find simple subsets by trimming numbers on the edges
	push(stack, start, end, median);
	trim(stack, subsets, ranks);

	int found = subsets.size();

	// find index of each rank
	vector<int> indices(n + 1);
	for (int i = 0; i < n; ++i) {
		indices[ranks[i]] = i;
	}

	start = indices[n];
	end = indices[n] + 1;
	for (int i = 1; i < n; ++i) {
		start = min(start, indices[n - i]);
		end = max(end, indices[n - i] + 1);

		// don't search too far, if many subsets where already found with trimming
		if (end - start > n - found) {
			break;
		}

		int size = i + 1;  // subset smallest size (max is 2*size - 1)
		int expand = 2 * size - (end - start);  // expansion size
		if (expand > 0) {
			// add extended subsets with extra [0, expand) ranks
			// e.g. for size=3 -> XXOOO, XOOOX, OOOXX, XOOO, OOOX, OOO
			for (int s = 0; s < expand; ++s) {
				for (int e = 0; e < expand; ++e) {
					if (s + e < expand && start - s >= 0 && end + e <= n) {
						subsets.insert(make_pair(start - s, end + e));
					}
				}
			}
		}
	}

	// special case: max value alone
	subsets.insert(make_pair(indices[n], indices[n] + 1));

	cout << best << " " << subsets.size() << endl;
	return 0;
}