#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <map>
class sHeap {
std::priority_queue<int> pq; // Główny kopiec
std::map<int, int> to_remove; // Licznik elementów do usunięcia
// Prywatna funkcja czyszcząca szczyt kopca
void clean() {
while (!pq.empty() && to_remove[pq.top()] > 0) {
to_remove[pq.top()]--; // Zmniejszamy licznik oczekujących
if (to_remove[pq.top()] == 0) {
to_remove.erase(pq.top()); // Opcjonalne: czyszczenie mapy
}
pq.pop(); // Fizyczne usunięcie z kopca
}
}
public:
void push(int x) {
pq.push(x);
}
void remove(int x) {
if (!pq.empty()) {
to_remove[x]++;
}
}
int top() {
clean();
return pq.empty() ? -1 : pq.top(); // -1 jako błąd/pusty
}
bool empty() {
clean();
return pq.empty();
}
};
using namespace std;
void vecvecp(vector<std::vector<int>> spot) {
for (int i=0; i<spot.size(); i++) {
cout << i << ": ";
for (int j=0; j<spot[i].size(); j++)
cout << spot[i][j] << " ";
cout << endl;
}
}
void vecp(vector<int> spot) {
for (int j=0; j<spot.size(); j++)
cout << spot[j] << " ";
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
sHeap perly_set;
int n, m, k, p;
cin >> n;
vector<int> perly;
for (int i=0; i<n; i++) {
cin >> p;
perly.push_back(p);
perly_set.push(p);
}
vector<int> stackd;
vector<int> sumkon(n);
for (int i=perly.size()-1; i>=0; i--) {
while (stackd.size()>0 and stackd[stackd.size()-1] <= perly[i]) {
stackd.pop_back();
}
stackd.push_back(perly[i]);
sumkon[i] = stackd.size();
}
vector<int> stackogon;
stackogon.push_back(0);
int sumamax = sumkon[0];
int suma=0;
int maxogon = 0;
for (int i=0; i<perly.size()-1; i++) {
p = perly[i];
suma = sumkon[i+1];
perly_set.remove(p);
if (p > stackogon[stackogon.size()-1]) {
stackogon.push_back(p);
}
int najwiekszy= perly_set.top();
// Zakładając, że szukasz elementów większych od 'wartosc':
auto itog = std::upper_bound(stackogon.begin(), stackogon.end(), najwiekszy);
// Wynik to różnica między końcem wektora a znalezionym miejscem
int ile_wiekszych = stackogon.end() - itog;
suma+= ile_wiekszych;
sumamax= max(sumamax, suma);
}
cout << sumamax << 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 | #include <algorithm> #include <iostream> #include <vector> #include <utility> #include <queue> #include <map> class sHeap { std::priority_queue<int> pq; // Główny kopiec std::map<int, int> to_remove; // Licznik elementów do usunięcia // Prywatna funkcja czyszcząca szczyt kopca void clean() { while (!pq.empty() && to_remove[pq.top()] > 0) { to_remove[pq.top()]--; // Zmniejszamy licznik oczekujących if (to_remove[pq.top()] == 0) { to_remove.erase(pq.top()); // Opcjonalne: czyszczenie mapy } pq.pop(); // Fizyczne usunięcie z kopca } } public: void push(int x) { pq.push(x); } void remove(int x) { if (!pq.empty()) { to_remove[x]++; } } int top() { clean(); return pq.empty() ? -1 : pq.top(); // -1 jako błąd/pusty } bool empty() { clean(); return pq.empty(); } }; using namespace std; void vecvecp(vector<std::vector<int>> spot) { for (int i=0; i<spot.size(); i++) { cout << i << ": "; for (int j=0; j<spot[i].size(); j++) cout << spot[i][j] << " "; cout << endl; } } void vecp(vector<int> spot) { for (int j=0; j<spot.size(); j++) cout << spot[j] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); sHeap perly_set; int n, m, k, p; cin >> n; vector<int> perly; for (int i=0; i<n; i++) { cin >> p; perly.push_back(p); perly_set.push(p); } vector<int> stackd; vector<int> sumkon(n); for (int i=perly.size()-1; i>=0; i--) { while (stackd.size()>0 and stackd[stackd.size()-1] <= perly[i]) { stackd.pop_back(); } stackd.push_back(perly[i]); sumkon[i] = stackd.size(); } vector<int> stackogon; stackogon.push_back(0); int sumamax = sumkon[0]; int suma=0; int maxogon = 0; for (int i=0; i<perly.size()-1; i++) { p = perly[i]; suma = sumkon[i+1]; perly_set.remove(p); if (p > stackogon[stackogon.size()-1]) { stackogon.push_back(p); } int najwiekszy= perly_set.top(); // Zakładając, że szukasz elementów większych od 'wartosc': auto itog = std::upper_bound(stackogon.begin(), stackogon.end(), najwiekszy); // Wynik to różnica między końcem wektora a znalezionym miejscem int ile_wiekszych = stackogon.end() - itog; suma+= ile_wiekszych; sumamax= max(sumamax, suma); } cout << sumamax << endl; return(0); } |
English