#include <iostream>
#include <climits>
#include <vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int size;
cin >> size;
vector<int> values(size), positions(size);
for(int index = 0; index < size; index++)
{
cin >> values[index];
positions[values[index] - 1] = index;
}
long long result = 0;
int lowest = INT_MAX, highest = 0;
for(int length = 1; length <= size; length++)
{
lowest = min(lowest, positions[size - (length / 2) - 1]);
highest = max(highest, positions[size - (length / 2) - 1]);
//cout << "LENGTH: " << length << ", VALUE: " << (size - (length / 2)) << ", [" << lowest + 1 << ", " << highest + 1 << "] ";
int begin = max(highest - (length - 1), 0);
int end = min(lowest + (length - 1), size - 1);
//cout << "[" << begin + 1 << ", " << end + 1<< "]" << endl;
if(begin <= end && end - begin + 2 - length > 0 && highest - lowest < length)
{
result += end - begin + 2 - length;
//cout << "RESULT: " << end - begin + 2 - length << endl;
}
}
cout << 2 * size + 1 << ' ' << result << '\n';
}
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 | #include <iostream> #include <climits> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int size; cin >> size; vector<int> values(size), positions(size); for(int index = 0; index < size; index++) { cin >> values[index]; positions[values[index] - 1] = index; } long long result = 0; int lowest = INT_MAX, highest = 0; for(int length = 1; length <= size; length++) { lowest = min(lowest, positions[size - (length / 2) - 1]); highest = max(highest, positions[size - (length / 2) - 1]); //cout << "LENGTH: " << length << ", VALUE: " << (size - (length / 2)) << ", [" << lowest + 1 << ", " << highest + 1 << "] "; int begin = max(highest - (length - 1), 0); int end = min(lowest + (length - 1), size - 1); //cout << "[" << begin + 1 << ", " << end + 1<< "]" << endl; if(begin <= end && end - begin + 2 - length > 0 && highest - lowest < length) { result += end - begin + 2 - length; //cout << "RESULT: " << end - begin + 2 - length << endl; } } cout << 2 * size + 1 << ' ' << result << '\n'; } |
English