//#include "pch.h" #include <iostream> #include <algorithm> using namespace std; const int maxik = 1e6 + 7; int tab[maxik]; int main() { ios::sync_with_stdio(false); cin.tie(0); int hmnumbers; int tocout; int result=2; int loop; int place; int maks, minimini; cin >> hmnumbers; if (hmnumbers % 2) { loop = hmnumbers / 2; tocout = 2 * (hmnumbers / 2 + 1) + hmnumbers; } else { loop = hmnumbers / 2 - 1; tocout = 2 * hmnumbers+1; } for (int i = 1; i <= hmnumbers; i++) { cin >> place; tab[place] = i; } int size = 0; maks = max(tab[hmnumbers], tab[hmnumbers - 1]); minimini = min(tab[hmnumbers], tab[hmnumbers - 1]); for (int i = 0; i < hmnumbers; i++) { size++; if (i !=0 ) { maks = max(maks, tab[hmnumbers - 1 - i]); minimini = min(minimini, tab[hmnumbers - 1 - i]); } int diff = maks - minimini; if (size == diff) { result++; } else if (diff < size) { int t = size - diff; result++; if (t <= minimini - 1 && t <= hmnumbers-maks) { result += t; } else { result += min(minimini - 1, hmnumbers - maks); } } size++; if (size == hmnumbers - 1) break; if (size == diff) { result++; } else if (diff < size) { int t = size - diff; result++; if (t <= minimini - 1 && t <= hmnumbers - maks) { result += t; } else { result += min(minimini - 1, hmnumbers - maks); } } } cout <<tocout <<" " << result; //system("pause"); }
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 | //#include "pch.h" #include <iostream> #include <algorithm> using namespace std; const int maxik = 1e6 + 7; int tab[maxik]; int main() { ios::sync_with_stdio(false); cin.tie(0); int hmnumbers; int tocout; int result=2; int loop; int place; int maks, minimini; cin >> hmnumbers; if (hmnumbers % 2) { loop = hmnumbers / 2; tocout = 2 * (hmnumbers / 2 + 1) + hmnumbers; } else { loop = hmnumbers / 2 - 1; tocout = 2 * hmnumbers+1; } for (int i = 1; i <= hmnumbers; i++) { cin >> place; tab[place] = i; } int size = 0; maks = max(tab[hmnumbers], tab[hmnumbers - 1]); minimini = min(tab[hmnumbers], tab[hmnumbers - 1]); for (int i = 0; i < hmnumbers; i++) { size++; if (i !=0 ) { maks = max(maks, tab[hmnumbers - 1 - i]); minimini = min(minimini, tab[hmnumbers - 1 - i]); } int diff = maks - minimini; if (size == diff) { result++; } else if (diff < size) { int t = size - diff; result++; if (t <= minimini - 1 && t <= hmnumbers-maks) { result += t; } else { result += min(minimini - 1, hmnumbers - maks); } } size++; if (size == hmnumbers - 1) break; if (size == diff) { result++; } else if (diff < size) { int t = size - diff; result++; if (t <= minimini - 1 && t <= hmnumbers - maks) { result += t; } else { result += min(minimini - 1, hmnumbers - maks); } } } cout <<tocout <<" " << result; //system("pause"); } |