#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> v; int a; for (int i = 0; i < n; i++) { cin >> a; v.push_back(a); } int *index = new int[n + 1]; bool *isTop = new bool[n + 1]; for (int i = 0; i < n; i++) { index[v[i]] = i; isTop[v[i]] = false; } int resultValue = 1 + 2 * n; int resultCount = 1; int currentCount = 1; int currentTopCount = 1; int leftIndex = index[n]; int rightIndex = index[n]; for (int currentNumber = n; currentNumber > 1; currentNumber--) { if (index[currentNumber - 1] >= leftIndex && index[currentNumber - 1] <= rightIndex) { if(!isTop[currentNumber - 1]) { currentTopCount++; isTop[currentNumber - 1] = true; } continue; } int maxToInclude = n - currentNumber - (currentCount - (n - currentNumber) - 1); int leftExtension = 0; if (maxToInclude > 0) { for (int l = leftIndex - 1; l >= 0; l--) { if (v[l] < currentNumber - 1) { leftExtension++; if (leftExtension == maxToInclude) { break; } } else { break; } } } int rightExtension = 0; if (maxToInclude > 0) { for (int r = rightIndex + 1; r < n; r++) { if (v[r] < currentNumber - 1) { rightExtension++; if (rightExtension == maxToInclude) { break; } } else { break; } } } if (leftExtension + rightExtension <= maxToInclude) { resultCount += (leftExtension + 1) * (rightExtension + 1) - 1; } else { if (leftExtension < rightExtension) { for (int ll = 0; ll <= leftExtension; ll++) { resultCount += min(maxToInclude - ll, rightExtension) + 1; } resultCount--; } else { for (int rr = 0; rr <= rightExtension; rr++) { resultCount += min(maxToInclude - rr, leftExtension) + 1; } resultCount--; } } for (int nextNumber = currentNumber - 1; nextNumber > 0; nextNumber--) { int nextIndex = index[nextNumber]; if (nextIndex < leftIndex) { int diff = leftIndex - nextIndex; int newCurrentCount = currentCount + diff; int requiredNumbers = newCurrentCount / 2 - currentTopCount; bool good = true; for (int bla = 0; bla < requiredNumbers; bla++) { int nextNextIndex = index[nextNumber - 1 - bla]; if (nextNextIndex < nextIndex || nextNextIndex > rightIndex) { good = false; break; } } currentCount += diff; leftIndex = nextIndex; currentTopCount++; isTop[nextNumber] = true; if (good) { // cout << leftIndex << ", " << rightIndex << endl; resultCount++; break; } } else if (nextIndex > rightIndex) { int diff = nextIndex - rightIndex; int newCurrentCount = currentCount + diff; int requiredNumbers = newCurrentCount / 2 - currentTopCount; bool good = true; for (int bla = 0; bla < requiredNumbers; bla++) { int nextNextIndex = index[nextNumber - 1 - bla]; if (nextNextIndex < leftIndex || nextNextIndex > nextIndex) { good = false; break; } } currentCount += diff; rightIndex = nextIndex; currentTopCount++; isTop[nextNumber] = true; if (good) { // cout << leftIndex << ", " << rightIndex << endl; resultCount++; break; } } else { currentTopCount++; isTop[nextNumber] = true; } } } cout << resultValue << " " << resultCount << endl; delete[] index; delete[] isTop; //todo return 0; // return resultCount; }
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> v; int a; for (int i = 0; i < n; i++) { cin >> a; v.push_back(a); } int *index = new int[n + 1]; bool *isTop = new bool[n + 1]; for (int i = 0; i < n; i++) { index[v[i]] = i; isTop[v[i]] = false; } int resultValue = 1 + 2 * n; int resultCount = 1; int currentCount = 1; int currentTopCount = 1; int leftIndex = index[n]; int rightIndex = index[n]; for (int currentNumber = n; currentNumber > 1; currentNumber--) { if (index[currentNumber - 1] >= leftIndex && index[currentNumber - 1] <= rightIndex) { if(!isTop[currentNumber - 1]) { currentTopCount++; isTop[currentNumber - 1] = true; } continue; } int maxToInclude = n - currentNumber - (currentCount - (n - currentNumber) - 1); int leftExtension = 0; if (maxToInclude > 0) { for (int l = leftIndex - 1; l >= 0; l--) { if (v[l] < currentNumber - 1) { leftExtension++; if (leftExtension == maxToInclude) { break; } } else { break; } } } int rightExtension = 0; if (maxToInclude > 0) { for (int r = rightIndex + 1; r < n; r++) { if (v[r] < currentNumber - 1) { rightExtension++; if (rightExtension == maxToInclude) { break; } } else { break; } } } if (leftExtension + rightExtension <= maxToInclude) { resultCount += (leftExtension + 1) * (rightExtension + 1) - 1; } else { if (leftExtension < rightExtension) { for (int ll = 0; ll <= leftExtension; ll++) { resultCount += min(maxToInclude - ll, rightExtension) + 1; } resultCount--; } else { for (int rr = 0; rr <= rightExtension; rr++) { resultCount += min(maxToInclude - rr, leftExtension) + 1; } resultCount--; } } for (int nextNumber = currentNumber - 1; nextNumber > 0; nextNumber--) { int nextIndex = index[nextNumber]; if (nextIndex < leftIndex) { int diff = leftIndex - nextIndex; int newCurrentCount = currentCount + diff; int requiredNumbers = newCurrentCount / 2 - currentTopCount; bool good = true; for (int bla = 0; bla < requiredNumbers; bla++) { int nextNextIndex = index[nextNumber - 1 - bla]; if (nextNextIndex < nextIndex || nextNextIndex > rightIndex) { good = false; break; } } currentCount += diff; leftIndex = nextIndex; currentTopCount++; isTop[nextNumber] = true; if (good) { // cout << leftIndex << ", " << rightIndex << endl; resultCount++; break; } } else if (nextIndex > rightIndex) { int diff = nextIndex - rightIndex; int newCurrentCount = currentCount + diff; int requiredNumbers = newCurrentCount / 2 - currentTopCount; bool good = true; for (int bla = 0; bla < requiredNumbers; bla++) { int nextNextIndex = index[nextNumber - 1 - bla]; if (nextNextIndex < leftIndex || nextNextIndex > nextIndex) { good = false; break; } } currentCount += diff; rightIndex = nextIndex; currentTopCount++; isTop[nextNumber] = true; if (good) { // cout << leftIndex << ", " << rightIndex << endl; resultCount++; break; } } else { currentTopCount++; isTop[nextNumber] = true; } } } cout << resultValue << " " << resultCount << endl; delete[] index; delete[] isTop; //todo return 0; // return resultCount; } |