标签:its top amp 模拟 name strong can box char
题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次。
思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操作序列根本无法做到有序出栈。所以碰到不合顺序的数,将栈中所有元素出栈一遍即可。
/** @Date : 2017-07-04 15:21:52
* @FileName: C.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
int main()
{
int n;
while(cin >> n)
{
stacks;
char q[10];
int t;
int tmp = 1;
int ans = 0;
for(int i = 0; i < 2 * n; i++)
{
scanf("%s", q);
if(q[0] == ‘a‘)
{
scanf("%d", &t);
s.push(t);
}
else
{
if(s.empty())
{
tmp++;
continue;
}
if(s.top() != tmp)
{
ans++;
while(!s.empty())
s.pop();
}
else s.pop();
tmp++;
}
}
printf("%d\n", ans);
}
return 0;
}
标签:its top amp 模拟 name strong can box char
原文地址:http://www.cnblogs.com/Yumesenya/p/7189700.html