#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> prefix_max;
std::vector<int> suffix_max;
std::vector<int> interval;
std::vector<int> number_index;
int n;
int find_furthest_to_the_right(int number)
{
int l = number_index[number];
int r = n - 1;
while (r > l)
{
int s = (r + l + 1) / 2;
if (suffix_max[s] >= number)
{
l = s;
}
else
{
r = s - 1;
}
}
return l;
}
int find_furthest_to_the_left(int number)
{
int l = 0;
int r = number_index[number];
while (r > l)
{
int s = (r + l) / 2;
if (prefix_max[s] >= number)
{
r = s;
}
else
{
l = s + 1;
}
}
return l;
}
int main()
{
//n = 8;
//std::vector<int> interval = {1, 2, 3, 4, 5, 6, 7, 8};
//while (std::next_permutation(interval.begin(), interval.end()))
{
scanf("%d", &n);
prefix_max.resize(n);
suffix_max.resize(n);
interval.resize(n);
number_index.resize(n + 1);
for (int i = 0; i < n; i++)
{
int value;
scanf("%d", &value);
interval[i] = value;
value = interval[i];
number_index[value] = i;
}
prefix_max[0] = interval[0];
for (int i = 1; i < n; i++)
{
prefix_max[i] = std::max(prefix_max[i - 1], interval[i]);
}
suffix_max[n - 1] = interval[n - 1];
for (int i = n - 2; i >= 0; i--)
{
suffix_max[i] = std::max(suffix_max[i + 1], interval[i]);
}
int max_score = 2 * n + 1; //this is independent from the scores order, and always equal to 2n + 1
long long result = 0;
for (int i = 0; i <= n; i++)
{
int double_median = 2 * i;
int number_of_elements = max_score - double_median;
int furthest_to_the_left = find_furthest_to_the_left(i);
int furthest_to_the_right = find_furthest_to_the_right(i);
int dupa = furthest_to_the_right - furthest_to_the_left + 1;
int d = std::max(0, number_of_elements - dupa + 1);
if (d < 0)
continue;
int free_space = furthest_to_the_left + n - furthest_to_the_right;
int elements_left = n - number_of_elements;
int full_d = d - 1;
if (furthest_to_the_left < full_d)
{
d -= (full_d - furthest_to_the_left);
}
if (n - furthest_to_the_right - 1 < full_d)
{
d -= (full_d - n + 1 + furthest_to_the_right);
}
if (elements_left >= 0)
result += std::max(d, 0);
if (number_of_elements > 1)
{
number_of_elements--;
elements_left++;
d = full_d;
full_d--;
if (furthest_to_the_left < full_d)
{
d -= (full_d - furthest_to_the_left);
}
if (n - furthest_to_the_right - 1 < full_d)
{
d -= (full_d - n + 1 + furthest_to_the_right);
}
if (elements_left >= 0)
result += std::max(d, 0);
}
}
printf("%d %lld\n", max_score, result);
}
}
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 | #include <iostream> #include <vector> #include <algorithm> std::vector<int> prefix_max; std::vector<int> suffix_max; std::vector<int> interval; std::vector<int> number_index; int n; int find_furthest_to_the_right(int number) { int l = number_index[number]; int r = n - 1; while (r > l) { int s = (r + l + 1) / 2; if (suffix_max[s] >= number) { l = s; } else { r = s - 1; } } return l; } int find_furthest_to_the_left(int number) { int l = 0; int r = number_index[number]; while (r > l) { int s = (r + l) / 2; if (prefix_max[s] >= number) { r = s; } else { l = s + 1; } } return l; } int main() { //n = 8; //std::vector<int> interval = {1, 2, 3, 4, 5, 6, 7, 8}; //while (std::next_permutation(interval.begin(), interval.end())) { scanf("%d", &n); prefix_max.resize(n); suffix_max.resize(n); interval.resize(n); number_index.resize(n + 1); for (int i = 0; i < n; i++) { int value; scanf("%d", &value); interval[i] = value; value = interval[i]; number_index[value] = i; } prefix_max[0] = interval[0]; for (int i = 1; i < n; i++) { prefix_max[i] = std::max(prefix_max[i - 1], interval[i]); } suffix_max[n - 1] = interval[n - 1]; for (int i = n - 2; i >= 0; i--) { suffix_max[i] = std::max(suffix_max[i + 1], interval[i]); } int max_score = 2 * n + 1; //this is independent from the scores order, and always equal to 2n + 1 long long result = 0; for (int i = 0; i <= n; i++) { int double_median = 2 * i; int number_of_elements = max_score - double_median; int furthest_to_the_left = find_furthest_to_the_left(i); int furthest_to_the_right = find_furthest_to_the_right(i); int dupa = furthest_to_the_right - furthest_to_the_left + 1; int d = std::max(0, number_of_elements - dupa + 1); if (d < 0) continue; int free_space = furthest_to_the_left + n - furthest_to_the_right; int elements_left = n - number_of_elements; int full_d = d - 1; if (furthest_to_the_left < full_d) { d -= (full_d - furthest_to_the_left); } if (n - furthest_to_the_right - 1 < full_d) { d -= (full_d - n + 1 + furthest_to_the_right); } if (elements_left >= 0) result += std::max(d, 0); if (number_of_elements > 1) { number_of_elements--; elements_left++; d = full_d; full_d--; if (furthest_to_the_left < full_d) { d -= (full_d - furthest_to_the_left); } if (n - furthest_to_the_right - 1 < full_d) { d -= (full_d - n + 1 + furthest_to_the_right); } if (elements_left >= 0) result += std::max(d, 0); } } printf("%d %lld\n", max_score, result); } } |
English