#include <bits/stdc++.h> using namespace std; int idxs[1000100]; bool is_in[1000100]; set<int, greater<int>> missing; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; idxs[a[i]] = i; } int p = idxs[n]; int q = idxs[n]; is_in[n] = true; long long c = 1; int bdgt = 0; for(int i = 2; i <= n; i++) { if(i%2 == 0) { if(!is_in[n - i/2]) { missing.insert(n - i/2); } } bdgt++; while(missing.size()) { int curr_miss = *missing.begin(); int cost_l = p - idxs[curr_miss]; int cost_r = idxs[curr_miss] - q; if(cost_l > 0) { if(cost_l <= bdgt) { bdgt -= cost_l; for(int j = p - 1; cost_l; cost_l--, j--, p--) { is_in[a[j]] = true; if(missing.find(a[j]) != missing.end()) { missing.erase(a[j]); } } } else { break; } } else { if(cost_r <= bdgt) { bdgt -= cost_r; for(int j = q + 1; cost_r; cost_r--, j++, q++) { is_in[a[j]] = true; if(missing.find(a[j]) != missing.end()) { missing.erase(a[j]); } } } else { break; } } } int ll = min(bdgt, p); int lr = min(bdgt, n - q - 1); if(!missing.size()) { c += (ll + lr - bdgt + 1); } } cout << a[idxs[n]]*2 + 1 << " " << c << "\n"; return 0; }
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 | #include <bits/stdc++.h> using namespace std; int idxs[1000100]; bool is_in[1000100]; set<int, greater<int>> missing; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; idxs[a[i]] = i; } int p = idxs[n]; int q = idxs[n]; is_in[n] = true; long long c = 1; int bdgt = 0; for(int i = 2; i <= n; i++) { if(i%2 == 0) { if(!is_in[n - i/2]) { missing.insert(n - i/2); } } bdgt++; while(missing.size()) { int curr_miss = *missing.begin(); int cost_l = p - idxs[curr_miss]; int cost_r = idxs[curr_miss] - q; if(cost_l > 0) { if(cost_l <= bdgt) { bdgt -= cost_l; for(int j = p - 1; cost_l; cost_l--, j--, p--) { is_in[a[j]] = true; if(missing.find(a[j]) != missing.end()) { missing.erase(a[j]); } } } else { break; } } else { if(cost_r <= bdgt) { bdgt -= cost_r; for(int j = q + 1; cost_r; cost_r--, j++, q++) { is_in[a[j]] = true; if(missing.find(a[j]) != missing.end()) { missing.erase(a[j]); } } } else { break; } } } int ll = min(bdgt, p); int lr = min(bdgt, n - q - 1); if(!missing.size()) { c += (ll + lr - bdgt + 1); } } cout << a[idxs[n]]*2 + 1 << " " << c << "\n"; return 0; } |