#include<iostream>
#include<vector>
#include<algorithm>
#include <stdio.h>
using namespace std;
#define MAXN 1000050
struct s {
int value;
int position;
};
bool comp(s i1, s i2)
{
return (i1.value > i2.value);
}
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(NULL);
int n;
vector<s> v;
scanf("%d", &n);
//cin >> n;
//cout << "CZYTAMY DANE" << endl;
for (int i = 0; i < n; i++) {
int input;
scanf("%d", &input);
//cin >> input;
v.push_back(s());
v.back().value = input;
v.back().position = i;
}
sort(v.begin(), v.end(), comp);
//for (vector<s>::iterator iter = v.begin(); iter != v.end(); iter++) {
// cout << iter->value << " at " << iter->position << endl;
//}
long long solutions = 1;
int left_pos = v[0].position;
int right_pos = v[0].position;
int consumed = 1;
//cout << "=============== ITERATION 0 =================" << endl;
//cout << "[" << left_pos << ", " << right_pos << "]; --> +1" << endl;
for (int i = 1; i < n; ++i) {
//cout << "=============== ITERATION " << i << " =================" << endl;
int range_length = i + 1;
int tops_needed = range_length / 2 + 1;
while (tops_needed > consumed) {
consumed++;
int next_pos = v[consumed-1].position;
if (next_pos < left_pos) left_pos = next_pos;
if (next_pos > right_pos) right_pos = next_pos;
}
//cout << "Dla długości " << range_length << " potrzeba " << tops_needed << " top liczb, " << "[" << left_pos << ", " << right_pos << "]" << endl;
int distance = right_pos - left_pos + 1;
if (i == n-1) {
//cout << "ALL --> +1" << endl;
solutions++;
} else if (distance > range_length) {
//cout << "Distance is too long --> +0" << endl;
continue;
} else if (distance == range_length) {
//cout << "Distance is equal to range length --> +1" << endl;
solutions++;
} else if (distance < range_length) {
//cout << "Distance " << distance << " is smaller --> +1" << endl;
int diff = range_length - distance;
int min_start = max(0, left_pos - diff);
int max_end = min(n - 1, right_pos + diff);
int delta = (max_end - min_start + 1) - range_length + 1;
//cout << "[" << min_start << ", " << max_end << "]; --> +" << delta << endl;
solutions += delta;
}
}
long long fun_result = 2 * n + 1;
printf("%lld %lld\n", fun_result, solutions);
//cout << fun_result << " " << solutions << endl;
}
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 | #include<iostream> #include<vector> #include<algorithm> #include <stdio.h> using namespace std; #define MAXN 1000050 struct s { int value; int position; }; bool comp(s i1, s i2) { return (i1.value > i2.value); } int main() { //ios::sync_with_stdio(false); //cin.tie(NULL); int n; vector<s> v; scanf("%d", &n); //cin >> n; //cout << "CZYTAMY DANE" << endl; for (int i = 0; i < n; i++) { int input; scanf("%d", &input); //cin >> input; v.push_back(s()); v.back().value = input; v.back().position = i; } sort(v.begin(), v.end(), comp); //for (vector<s>::iterator iter = v.begin(); iter != v.end(); iter++) { // cout << iter->value << " at " << iter->position << endl; //} long long solutions = 1; int left_pos = v[0].position; int right_pos = v[0].position; int consumed = 1; //cout << "=============== ITERATION 0 =================" << endl; //cout << "[" << left_pos << ", " << right_pos << "]; --> +1" << endl; for (int i = 1; i < n; ++i) { //cout << "=============== ITERATION " << i << " =================" << endl; int range_length = i + 1; int tops_needed = range_length / 2 + 1; while (tops_needed > consumed) { consumed++; int next_pos = v[consumed-1].position; if (next_pos < left_pos) left_pos = next_pos; if (next_pos > right_pos) right_pos = next_pos; } //cout << "Dla długości " << range_length << " potrzeba " << tops_needed << " top liczb, " << "[" << left_pos << ", " << right_pos << "]" << endl; int distance = right_pos - left_pos + 1; if (i == n-1) { //cout << "ALL --> +1" << endl; solutions++; } else if (distance > range_length) { //cout << "Distance is too long --> +0" << endl; continue; } else if (distance == range_length) { //cout << "Distance is equal to range length --> +1" << endl; solutions++; } else if (distance < range_length) { //cout << "Distance " << distance << " is smaller --> +1" << endl; int diff = range_length - distance; int min_start = max(0, left_pos - diff); int max_end = min(n - 1, right_pos + diff); int delta = (max_end - min_start + 1) - range_length + 1; //cout << "[" << min_start << ", " << max_end << "]; --> +" << delta << endl; solutions += delta; } } long long fun_result = 2 * n + 1; printf("%lld %lld\n", fun_result, solutions); //cout << fun_result << " " << solutions << endl; } |
English