#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll b1,n, k, a, b, c, z, m, d, t,m2,s; vi vk, va, vb, vc; vvi vva; vii vkx; pii rozp(int m2) { if (m2 % 2 == 1) m2--; pii p1=MP(n,0); for (int i = m2 / 2; i <= n; i++) { p1.first = min(p1.first, vb[i]); p1.second = max(p1.second, vb[i]); } return p1; } pii rozp2(pii p1, int m2) { int a = (m2 - 2) / 2; if (vk[p1.first] == a) { for (int i = p1.first + 1; i < p1.second; i++) { if (vk[i] > a) { p1.first = i; break; } } } else if (vk[p1.second] == a) { for (int i = p1.second - 1; i > p1.first; i--) { if (vk[i] > a) { p1.second = i; break; } } } return p1; } int prze(int m2) { if (m2 % 2 == 0) return n - m2 / 2 +1 + (n - m2 / 2); else return 2*(n - m2 / 2); } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t2.in.txt", "rt", stdin); // while (1) { cin >> n; vk.assign(n, 0); vb.assign(n + 1, 0); for (int i = 0; i < n; i++) { cin >> vk[i]; vb[vk[i]] = i; } k = 0; m2 = n + 1; z = 2 * n + 1; if (n % 2 == 0) { k = 1; m2++; } pii p1 = rozp(m2); int a2 = prze(m2); if (p1.second - p1.first + 1 <= a2) { a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; m2++; a2--; a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; } m2++; while (m2<2*n) { p1 = rozp2(p1, m2); a2 = prze(m2); if (p1.second - p1.first + 1 <= a2) { a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; m2++; a2--; a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; } m2++; } if(n>2) k++; cout << z<<" "<<k << endl; // } 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 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 | #include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <map> #include <string> #include <vector> #include <iostream> #include <sstream> #include <queue> #include <algorithm> #include <assert.h> using namespace std; #define ll long long #define PB push_back #define FOR(a,start,end) for(int a=int(start); a<int(end); a++) #define INF INT_MAX #define SORT(a) sort(a.begin(),a.end()) #define CL(a,x) memset(a,x,sizeof(a)) #define REP(a,x) for(int a=0;a<x;a++) #define REP1(a,x) for(int a=1;a<=x;a++) #define MP make_pair typedef vector<ll> vi; typedef pair<ll, ll> pii; typedef vector<string> vs; typedef vector<pii> vii; typedef vector<vector<ll> > vvi; typedef vector<vector<string> > vvs; typedef vector<pair<string, string> > vss; typedef pair<string, string> pss; typedef pair<ll, pii> ppii; typedef vector<ppii> vppii; typedef vector<vector<pii> > vvii; typedef vector<vvi> vvvi; ll b1,n, k, a, b, c, z, m, d, t,m2,s; vi vk, va, vb, vc; vvi vva; vii vkx; pii rozp(int m2) { if (m2 % 2 == 1) m2--; pii p1=MP(n,0); for (int i = m2 / 2; i <= n; i++) { p1.first = min(p1.first, vb[i]); p1.second = max(p1.second, vb[i]); } return p1; } pii rozp2(pii p1, int m2) { int a = (m2 - 2) / 2; if (vk[p1.first] == a) { for (int i = p1.first + 1; i < p1.second; i++) { if (vk[i] > a) { p1.first = i; break; } } } else if (vk[p1.second] == a) { for (int i = p1.second - 1; i > p1.first; i--) { if (vk[i] > a) { p1.second = i; break; } } } return p1; } int prze(int m2) { if (m2 % 2 == 0) return n - m2 / 2 +1 + (n - m2 / 2); else return 2*(n - m2 / 2); } int main() { //freopen("c:\\wojtek\\uva\\pa\\debug\\t2.in.txt", "rt", stdin); // while (1) { cin >> n; vk.assign(n, 0); vb.assign(n + 1, 0); for (int i = 0; i < n; i++) { cin >> vk[i]; vb[vk[i]] = i; } k = 0; m2 = n + 1; z = 2 * n + 1; if (n % 2 == 0) { k = 1; m2++; } pii p1 = rozp(m2); int a2 = prze(m2); if (p1.second - p1.first + 1 <= a2) { a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; m2++; a2--; a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; } m2++; while (m2<2*n) { p1 = rozp2(p1, m2); a2 = prze(m2); if (p1.second - p1.first + 1 <= a2) { a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; m2++; a2--; a = max(0ll, p1.second - a2 + 1); b = min(n - 1, p1.first + a2 - 1); k += (p1.first - a) + (b - p1.second) - (a2 - (p1.second - p1.first + 1)) + 1; } m2++; } if(n>2) k++; cout << z<<" "<<k << endl; // } return 0; } |