#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int N, a[500000]; void readInput() { cin >> N; for (int i = 0; i < N; i++) cin >> a[i]; } void randomInput(int size) { N = size; for (int i = 0; i < N; i++) a[i] = i+1; random_shuffle(a, a+N); } void printSol(pair<int,int> sol) { cout << sol.first << " " << sol.second << endl; } pair<int,int> solveExhaustive() { int best = 0; pair<int,int> ans; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { int sol = 0; for (int k = i; k <= j; k++) { if (a[i] > a[k] && a[j] < a[k]) sol++; } if (sol > best) { best = sol; ans = make_pair(i+1,j+1); } } } return ans; } pair<int,int> solveHeuristic() { int bestLeft = 0, bestRight = 0, left, right; for (int i = 0; i < N; i++) { if (a[i]-i > bestLeft) { bestLeft = a[i]-i; left = i+1; } if (i-a[i] > bestRight) { bestRight = i-a[i]; right = i+1; } } return make_pair(left, right); } pair<int,int> solveHeuristic2() { pair<int,int> topLeft[N], topRight[N]; for (int i = 0; i < N; i++) { topRight[i] = make_pair(a[i]-i, i); topLeft[i] = make_pair(i-a[i], i); } sort(topRight, topRight + N); sort(topLeft, topLeft + N); int DEPTH = 50; DEPTH = min(DEPTH, N); int best = 0; pair<int,int> ans; for (int i = 0; i < DEPTH; i++) { for (int j = 0; j < DEPTH; j++) { int left = topLeft[i].second, right = topRight[j].second; // copy paste from exhaustive int sol = 0; for (int k = left+1; k < right; k++) { if (a[left] > a[k] && a[right] < a[k]) sol++; } if (sol > best || sol == best && make_pair(left+1,right+1) < ans) { best = sol; ans = make_pair(left+1,right+1); } } } pair<int,int> alternative = solveHeuristic(); int left = alternative.first-1, right = alternative.second-1; int sol = 0; for (int k = left+1; k < right; k++) { if (a[left] > a[k] && a[right] < a[k]) sol++; } if (sol >= best) { best = sol; ans = make_pair(left+1,right+1); } return ans; } int main() { readInput(); bool coolArray = true; for (int i = 0; i < N; i++) if (a[i] != i+1) coolArray = false; if (coolArray) { cout << "Cool Array" << endl; return 0; } printSol(solveHeuristic2()); } int main2() { int TC = 1000, bad1 = 0, bad2 = 0; for (int tc = 0; tc < TC; tc++) { randomInput(0); bool coolArray = true; for (int i = 0; i < N; i++) if (a[i] != i+1) coolArray = false; if (coolArray) { cout << "Cool Array" << endl; return 0; } //printSol(solveExhaustive()); //printSol(solveHeuristic()); if (solveExhaustive() != solveHeuristic()) { /*for (int i = 0; i < N; i++) cout << a[i] << " "; cout << endl; printSol(solveExhaustive()); printSol(solveHeuristic()); cout << endl;*/ bad1++; } if (solveExhaustive() != solveHeuristic2()) { bad2++; } } cout << (double)bad1 / TC << endl; cout << (double)bad2 / TC << endl; return 0; }