NC14893 栈和排序 模拟 暴力 栈
栈和排序
http://www.nowcoder.com/questionTerminal/b10a7ac681e9429e89a6a510e5799647
很明显"当前还未打印的数字中的最大数字"压栈时,就应该把弹栈打印
此时一定是字典序最大,如果最后栈内还有值,就全部出栈打印即可
\
#ifdef debug #include <time.h> #include "/home/majiao/mb.h" #endif #include <iostream> #include <algorithm> #include <vector> #include <string.h> #include <map> #include <set> #include <stack> #include <queue> #include <math.h> #define MAXN (1000005) #define ll long long int #define INF (0x7f7f7f7f) #define fori(lef, rig) for(int i=lef; i<=rig; i++) #define forj(lef, rig) for(int j=lef; j<=rig; j++) #define fork(lef, rig) for(int k=lef; k<=rig; k++) #define QAQ (0) using namespace std; #ifdef debug #define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0) void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... A> void err(T a, A... x) { cout << a << ' '; err(x...); } #endif #ifndef debug namespace FIO { template <typename T> void read(T& x) { int f = 1; x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } }; using namespace FIO; #endif int n, m, Q, K, a[MAXN]; stack<int> stk; int main() { #ifdef debug freopen("test", "r", stdin); clock_t stime = clock(); #endif scanf("%d ", &n); int now = n; for(int i=1, x; i<=n; i++) { scanf("%d ", &x); if(x == now) //测试数据比较水,一开始少了now--都能AC a[++m] = x, now --; else stk.push(x); } while(!stk.empty()) { a[++m] = stk.top(); stk.pop(); } for(int i=1; i<=n; i++) printf("%d%c", a[i], i==n?'\n':' '); #ifdef debug clock_t etime = clock(); printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC); #endif return 0; }