Envío 1964
- Autor: Javier
- Fecha: 2020-11-14 20:22:33 UTC (Hace alrededor de 2 años)
Caso # |
Resultado |
Tiempo |
Memoria |
#1 |

Correcto
|
0.007 s
|
1 KBi |
#2 |

Correcto
|
0.006 s
|
1 KBi |
#3 |

Correcto
|
0.007 s
|
12 KBi |
#4 |

Correcto
|
0.007 s
|
1 KBi |
#5 |

Correcto
|
0.006 s
|
1 KBi |
#6 |

Correcto
|
0.243 s
|
6 KBi |
#7 |

Correcto
|
0.179 s
|
6 KBi |
#8 |

Correcto
|
0.278 s
|
8 KBi |
#9 |

Correcto
|
0.211 s
|
6 KBi |
#10 |

Correcto
|
0.304 s
|
8 KBi |
Puntos totales: 100 / 100
Código
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
void print(const vector<int> &a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
vector<int> solve(const vector<int> &a) {
stack<int> pendingIndexes;
vector<int> solution(a.size());
// push the first index to the pending stack
pendingIndexes.push(0);
for (int i = 1; i < a.size(); i++) {
if(!pendingIndexes.empty()) {
int topIndex = pendingIndexes.top();
int top = a[topIndex];
int current = a[i];
// If the top of the stack < current element :
// current element is NGE for all element on stack < current
while (current > top and !pendingIndexes.empty()) {
int topIndex = pendingIndexes.top();
solution[topIndex] = current;
pendingIndexes.pop();
if (!pendingIndexes.empty()) {
topIndex = pendingIndexes.top();
}
top = a[topIndex];
}
}
pendingIndexes.push(i);
}
// All the remaining elements in stack have no NGE
while (!pendingIndexes.empty()) {
int topIndex = pendingIndexes.top();
solution[topIndex] = -1;
pendingIndexes.pop();
}
return solution;
}
int main() {
int C;
cin >> C;
for (int i = 0; i < C; i++) {
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
vector<int> s = solve(a);
cout << "Case #" << (i+1) << ": ";
print(s);
}
return 0;
}