#include <iostream> long long min(long long i1, long long i2){ if (i1<i2) return i1; else return i2; } long long max(long long i1, long long i2){ if (i1>i2) return i1; else return i2; } int main(){ long long n; std::cin >> n; long long array[n]; long long index[n]; long long indLow = 0; long long indHigh = 0; long long count = 0; long long target = 2*n + 1; long long fragment = 0; long long minimum = 0; long long need = 0; for (int i = 0; i < n; i++){ std::cin >> array[i]; index[array[i]-1] = i; } indLow = index[n-1]; indHigh = index[n-1]; for (int i = 1; i <= n/2; i++){ if (index[n-i-1] < indLow){ indLow = index[n-i-1]; }else if (index[n-i-1] > indHigh){ indHigh = index[n-i-1]; } fragment = indHigh-indLow+1; need = 2*i - fragment; if (fragment == 2*i && 2*i < n){ count++; }else if(fragment < 2*i && 2*i < n){ minimum = min(indLow,n-1-indHigh); if (n-1-indHigh == 0 || indLow == 0){ count++; }else if (indHigh - 2*i + 1 < 0 && indLow + 2*i> n){ if (need > minimum){ count += n - 2*i + 1; }else{ count += minimum+1; } }else if (indHigh - 2*i + 1 < 0 || indLow + 2*i> n){ count += minimum + 1; } else{ count += 2*i + 1 - fragment; } } need = 2*i - fragment + 1; if (fragment == 2*i + 1 && 2*i + 1 < n){ count ++; } else if(fragment < 2*i + 1 && 2*i + 1 < n){ minimum = min(indLow, n-1-indHigh); if (n-1-indHigh == 0 || indLow == 0){ count++; }else if (indHigh - (2*i+1) + 1 < 0 && indLow + (2*i+1)> n){ if (need > minimum){ count += n - 2*i ; }else{ count += minimum+1; } }else if (indHigh - (2*i + 1) + 1 < 0 || indLow + 2*i+1> n){ count += minimum + 1; }else { count += 2*i + 1 - fragment + 1; } } } if (n == 1 ){ std::cout << target << " " << 1; }else{ std::cout << target << " " << count+2; } }
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 | #include <iostream> long long min(long long i1, long long i2){ if (i1<i2) return i1; else return i2; } long long max(long long i1, long long i2){ if (i1>i2) return i1; else return i2; } int main(){ long long n; std::cin >> n; long long array[n]; long long index[n]; long long indLow = 0; long long indHigh = 0; long long count = 0; long long target = 2*n + 1; long long fragment = 0; long long minimum = 0; long long need = 0; for (int i = 0; i < n; i++){ std::cin >> array[i]; index[array[i]-1] = i; } indLow = index[n-1]; indHigh = index[n-1]; for (int i = 1; i <= n/2; i++){ if (index[n-i-1] < indLow){ indLow = index[n-i-1]; }else if (index[n-i-1] > indHigh){ indHigh = index[n-i-1]; } fragment = indHigh-indLow+1; need = 2*i - fragment; if (fragment == 2*i && 2*i < n){ count++; }else if(fragment < 2*i && 2*i < n){ minimum = min(indLow,n-1-indHigh); if (n-1-indHigh == 0 || indLow == 0){ count++; }else if (indHigh - 2*i + 1 < 0 && indLow + 2*i> n){ if (need > minimum){ count += n - 2*i + 1; }else{ count += minimum+1; } }else if (indHigh - 2*i + 1 < 0 || indLow + 2*i> n){ count += minimum + 1; } else{ count += 2*i + 1 - fragment; } } need = 2*i - fragment + 1; if (fragment == 2*i + 1 && 2*i + 1 < n){ count ++; } else if(fragment < 2*i + 1 && 2*i + 1 < n){ minimum = min(indLow, n-1-indHigh); if (n-1-indHigh == 0 || indLow == 0){ count++; }else if (indHigh - (2*i+1) + 1 < 0 && indLow + (2*i+1)> n){ if (need > minimum){ count += n - 2*i ; }else{ count += minimum+1; } }else if (indHigh - (2*i + 1) + 1 < 0 || indLow + 2*i+1> n){ count += minimum + 1; }else { count += 2*i + 1 - fragment + 1; } } } if (n == 1 ){ std::cout << target << " " << 1; }else{ std::cout << target << " " << count+2; } } |