UVa514

模拟题做的不多,在这一方面比较弱。直接按书上的打了,再慢慢理解,消化。一开始还不理解的,后来看着书上的图就懂了。(因为输入不同,此代码并不能在oj上ac)有一组数据很奇怪,试了几次有不同的结果。

#include
#include
using namespace std;
int const maxn=1000+10;
int target[maxn];
int main()
{
        int T,x;
        while(cin>>T)
        {
                stack  s;
                int A=1,B=1;
                for(int i=1;i<=T;i++)
                {
                cin>>target[i];
                }
                int ok=1;
               while(B<=T)
               {
                       if(A==target[B]) {A++;B++;} //进栈后马上出栈
                       else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;} //出栈
                       else if(A<=T) s.push(A++); //进栈
                       else {ok=0;break;}
               }
                if(ok) cout<<"Yes"<                 else cout<<"No"<         }
        return 0;
}
UVa514_第1张图片
UVa514_第2张图片



顺便把另外一题代码贴上 UVa12100
#include
using namespace  std;

int main()
{
        int T,n,x,target,m,step;
        cin>>T;
        while(T--)
        {
                queue q;
                priority_queue pq;
                step=0;
                cin>>n>>m;
                for(int i=0;i                 {
                        cin>>x;
                        q.push(x);
                        pq.push(x);
                        if(i==m) target=x;
                }
                while(!pq.empty()&&pq.top()>=target)
                {
                        while(q.front()!=pq.top())//移动不需要时间
                        {
                        q.push(q.front());
                        q.pop();
                        m=m==0?q.size()-1:m-1;
                        }
                        q.pop();
                        pq.pop();
                        ++step;--m;
                        if(m<0) break; //目标打印完成
                }
                cout<         }
        return 0;
}


你可能感兴趣的:(模拟,数据结构)