#include <bits/stdc++.h> #define FOR(i,n) for(int i = 0; i<n; ++i) #define FOREACH(v, a) for(auto & v : (a)) #define X first #define Y second #define PR std::pair #define MPR std::make_pair typedef long long ll; typedef std::vector<int> VI; using namespace std; #pragma region Ready functions int fastPower(int a, int b){ int res=1, pop_a = a; while(b){ if(b&1) res *= pop_a; pop_a *= pop_a; b>>=1; } return res; } #pragma endregion vector<int> nums; struct segment{ set<int> bigNums, smallNums; int l,p; int GetDoubleMedian(){ int n = p-l+1; if(n%2==0){ return *smallNums.rbegin() + *bigNums.begin(); } else{ return 2 * (*bigNums.begin()); } } void TakeOut(int x){ //jesli jest mniejszy od mediany, to sie znajduje w dolnym if(x < *bigNums.begin()){ smallNums.erase(x); } else{ bigNums.erase(x); } } void Normalize(){ while(bigNums.size() < smallNums.size()){ int v = *smallNums.rbegin(); bigNums.insert(v); smallNums.erase(v); } if(GetSize()%2 == 1){ while(bigNums.size() - smallNums.size() > 1){ int v = *bigNums.begin(); smallNums.insert(v); bigNums.erase(v); } } else{ while(bigNums.size() > smallNums.size()){ int v = *bigNums.begin(); smallNums.insert(v); bigNums.erase(v); } } } void Insert(int x){ if(x < *bigNums.begin()){ smallNums.insert(x); } else{ bigNums.insert(x); } } int GetSize(){ return smallNums.size()+bigNums.size(); } }; segment StarterSegs[1000001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; ll FVal = n + n + 1; StarterSegs[0].l = 0; StarterSegs[0].p = 0; int counter = 0; FOR(i, n){ int x; cin>>x; nums.push_back(x); StarterSegs[i+1] = StarterSegs[i]; StarterSegs[i+1].Insert(x); StarterSegs[i+1].Normalize(); StarterSegs[i+1].p = i; if(StarterSegs[i+1].GetSize() + StarterSegs[i+1].GetDoubleMedian() == FVal){ //cout<<StarterSegs[i+1].GetDoubleMedian()<<" "<<StarterSegs[i+1].GetSize()<<endl; counter++; } } for(int length = 1; length<n; length++){ //throw away the previous one and add the new one for(int str = 1; str <= n - length; str++){ StarterSegs[length].TakeOut(nums[str-1]); StarterSegs[length].Insert(nums[str + length - 1]); StarterSegs[length].Normalize(); StarterSegs[length].p++; StarterSegs[length].l--; //cout<<StarterSegs[length].GetDoubleMedian()<<" "<<StarterSegs[length].GetSize()<<endl; if(StarterSegs[length].GetDoubleMedian() + StarterSegs[length].GetSize() == FVal){ counter++; //cout<<StarterSegs[length].GetDoubleMedian()<<" "<<StarterSegs[length].GetSize()<<endl; } } } cout<<FVal<<" "<<counter<<endl; }
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 146 147 148 149 150 151 152 | #include <bits/stdc++.h> #define FOR(i,n) for(int i = 0; i<n; ++i) #define FOREACH(v, a) for(auto & v : (a)) #define X first #define Y second #define PR std::pair #define MPR std::make_pair typedef long long ll; typedef std::vector<int> VI; using namespace std; #pragma region Ready functions int fastPower(int a, int b){ int res=1, pop_a = a; while(b){ if(b&1) res *= pop_a; pop_a *= pop_a; b>>=1; } return res; } #pragma endregion vector<int> nums; struct segment{ set<int> bigNums, smallNums; int l,p; int GetDoubleMedian(){ int n = p-l+1; if(n%2==0){ return *smallNums.rbegin() + *bigNums.begin(); } else{ return 2 * (*bigNums.begin()); } } void TakeOut(int x){ //jesli jest mniejszy od mediany, to sie znajduje w dolnym if(x < *bigNums.begin()){ smallNums.erase(x); } else{ bigNums.erase(x); } } void Normalize(){ while(bigNums.size() < smallNums.size()){ int v = *smallNums.rbegin(); bigNums.insert(v); smallNums.erase(v); } if(GetSize()%2 == 1){ while(bigNums.size() - smallNums.size() > 1){ int v = *bigNums.begin(); smallNums.insert(v); bigNums.erase(v); } } else{ while(bigNums.size() > smallNums.size()){ int v = *bigNums.begin(); smallNums.insert(v); bigNums.erase(v); } } } void Insert(int x){ if(x < *bigNums.begin()){ smallNums.insert(x); } else{ bigNums.insert(x); } } int GetSize(){ return smallNums.size()+bigNums.size(); } }; segment StarterSegs[1000001]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; ll FVal = n + n + 1; StarterSegs[0].l = 0; StarterSegs[0].p = 0; int counter = 0; FOR(i, n){ int x; cin>>x; nums.push_back(x); StarterSegs[i+1] = StarterSegs[i]; StarterSegs[i+1].Insert(x); StarterSegs[i+1].Normalize(); StarterSegs[i+1].p = i; if(StarterSegs[i+1].GetSize() + StarterSegs[i+1].GetDoubleMedian() == FVal){ //cout<<StarterSegs[i+1].GetDoubleMedian()<<" "<<StarterSegs[i+1].GetSize()<<endl; counter++; } } for(int length = 1; length<n; length++){ //throw away the previous one and add the new one for(int str = 1; str <= n - length; str++){ StarterSegs[length].TakeOut(nums[str-1]); StarterSegs[length].Insert(nums[str + length - 1]); StarterSegs[length].Normalize(); StarterSegs[length].p++; StarterSegs[length].l--; //cout<<StarterSegs[length].GetDoubleMedian()<<" "<<StarterSegs[length].GetSize()<<endl; if(StarterSegs[length].GetDoubleMedian() + StarterSegs[length].GetSize() == FVal){ counter++; //cout<<StarterSegs[length].GetDoubleMedian()<<" "<<StarterSegs[length].GetSize()<<endl; } } } cout<<FVal<<" "<<counter<<endl; } |