//Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(auto i : (c)) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); #define debugOn 0 #define debug(a,b) if (debugOn) cout << a << ": " << b << endl; const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 1000000; int t[MAXN + 5]; int positions[MAXN + 5]; void rob(int test) { int n; scanf("%d", &n); FOR (i, 0, n) { scanf("%d", &t[i]); positions[t[i]] = i; } pii currRange = MP(positions[n], positions[n]); ll result = 1; int maxes = 1; FORREV (i, 1, n - 1) { int position = positions[i]; if (position < currRange.F) { currRange.F = position; } else { currRange.S = position; } ++maxes; debug("i", i); debug("position", position); debug("currRange.F", currRange.F); debug("currRange.S", currRange.S); debug("maxes", maxes); FORREV (j, 1, i - 1) { if (positions[j] >= currRange.F && positions[j] <= currRange.S) { ++maxes; i = j; } else { break; } } debug("iUpdated", i); if (i == 1) { ++result; debug("result", result); break; } int possibleNumbersToAdd = min(maxes - 1, n - maxes); debug("possibleNumbersToAdd", possibleNumbersToAdd); int notMaxesInRange = currRange.S - currRange.F + 1 - maxes; debug("notMaxesInRange", notMaxesInRange); int possibleNumbersToAddOutsideRange = possibleNumbersToAdd - notMaxesInRange; debug("possibleNumbersToAddOutsideRange", possibleNumbersToAddOutsideRange); if (possibleNumbersToAddOutsideRange >= 0) { int possibleNumbersToLeft = positions[i - 1] < currRange.F ? currRange.F - positions[i - 1] - 1 : currRange.F; int possibleNumbersToRight = positions[i - 1] > currRange.S ? positions[i - 1] - currRange.S - 1 : n - currRange.S - 1; debug("possibleNumbersToLeft", possibleNumbersToLeft); debug("possibleNumbersToRight", possibleNumbersToRight); int mxNumbersToAdd = min(possibleNumbersToAddOutsideRange, possibleNumbersToLeft + possibleNumbersToRight); ll all = ((ll) (mxNumbersToAdd + 2)) * (mxNumbersToAdd + 1) / 2; ll impossibleLeft = mxNumbersToAdd > possibleNumbersToLeft ? ((ll) mxNumbersToAdd - possibleNumbersToLeft + 1) * (mxNumbersToAdd - possibleNumbersToLeft) / 2 : 0; ll impossibleRight = mxNumbersToAdd > possibleNumbersToRight ? ((ll) mxNumbersToAdd - possibleNumbersToRight + 1) * (mxNumbersToAdd - possibleNumbersToRight) / 2 : 0; debug("all", all); debug("impossibleLeft", impossibleLeft); debug("impossibleRight", impossibleRight); debug("add to result", all - impossibleLeft - impossibleRight); result += (all - impossibleLeft - impossibleRight); } debug("result", result); } int median2 = (n % 2 == 1) ? (n/2 + 1) * 2 : (n/2 + n/2+1); printf("%d %lld\n", n + median2, result); } int main() { int test = 1; //scanf("%d", &test); FORE (i, 1, test) rob(i); 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 | //Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(auto i : (c)) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); #define debugOn 0 #define debug(a,b) if (debugOn) cout << a << ": " << b << endl; const int INF = 1000000001; const double EPS = 10e-9; const int MAXN = 1000000; int t[MAXN + 5]; int positions[MAXN + 5]; void rob(int test) { int n; scanf("%d", &n); FOR (i, 0, n) { scanf("%d", &t[i]); positions[t[i]] = i; } pii currRange = MP(positions[n], positions[n]); ll result = 1; int maxes = 1; FORREV (i, 1, n - 1) { int position = positions[i]; if (position < currRange.F) { currRange.F = position; } else { currRange.S = position; } ++maxes; debug("i", i); debug("position", position); debug("currRange.F", currRange.F); debug("currRange.S", currRange.S); debug("maxes", maxes); FORREV (j, 1, i - 1) { if (positions[j] >= currRange.F && positions[j] <= currRange.S) { ++maxes; i = j; } else { break; } } debug("iUpdated", i); if (i == 1) { ++result; debug("result", result); break; } int possibleNumbersToAdd = min(maxes - 1, n - maxes); debug("possibleNumbersToAdd", possibleNumbersToAdd); int notMaxesInRange = currRange.S - currRange.F + 1 - maxes; debug("notMaxesInRange", notMaxesInRange); int possibleNumbersToAddOutsideRange = possibleNumbersToAdd - notMaxesInRange; debug("possibleNumbersToAddOutsideRange", possibleNumbersToAddOutsideRange); if (possibleNumbersToAddOutsideRange >= 0) { int possibleNumbersToLeft = positions[i - 1] < currRange.F ? currRange.F - positions[i - 1] - 1 : currRange.F; int possibleNumbersToRight = positions[i - 1] > currRange.S ? positions[i - 1] - currRange.S - 1 : n - currRange.S - 1; debug("possibleNumbersToLeft", possibleNumbersToLeft); debug("possibleNumbersToRight", possibleNumbersToRight); int mxNumbersToAdd = min(possibleNumbersToAddOutsideRange, possibleNumbersToLeft + possibleNumbersToRight); ll all = ((ll) (mxNumbersToAdd + 2)) * (mxNumbersToAdd + 1) / 2; ll impossibleLeft = mxNumbersToAdd > possibleNumbersToLeft ? ((ll) mxNumbersToAdd - possibleNumbersToLeft + 1) * (mxNumbersToAdd - possibleNumbersToLeft) / 2 : 0; ll impossibleRight = mxNumbersToAdd > possibleNumbersToRight ? ((ll) mxNumbersToAdd - possibleNumbersToRight + 1) * (mxNumbersToAdd - possibleNumbersToRight) / 2 : 0; debug("all", all); debug("impossibleLeft", impossibleLeft); debug("impossibleRight", impossibleRight); debug("add to result", all - impossibleLeft - impossibleRight); result += (all - impossibleLeft - impossibleRight); } debug("result", result); } int median2 = (n % 2 == 1) ? (n/2 + 1) * 2 : (n/2 + n/2+1); printf("%d %lld\n", n + median2, result); } int main() { int test = 1; //scanf("%d", &test); FORE (i, 1, test) rob(i); return 0; } |