【51nod-1289】大鱼吃小鱼(思维)

这道题我想的方法是用栈,每次去出栈顶两元素,比较是否相对,相对的话合并后压入栈。类似括号匹配。

#include <bits/stdc++.h>
using namespace std;
const int N = 100004;
typedef long long LL;
stack<pair<int, int> >S;
int main()
{
    int n;
    cin>>n;
    pair<int, int>p, q;
    for(int i=0; i<n; i++)
    {
        scanf("%d%d", &p.first, &p.second);
        S.push(p);
        while(1)
        {
            p = S.top();
            S.pop();
            if(S.empty())
            {
                S.push(p);
                break;
            }
            else
            {
                q = S.top();
                S.pop();
                if(p.second==0&&q.second==1)
                {
                    if(p.first>q.first)
                        S.push(p);
                    else
                        S.push(q);
                }
                else
                {
                    S.push(q);
                    S.push(p);
                    break;
                }
            }
        }
    }
    printf("%d\n", S.size());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lesroad/p/9236287.html
今日推荐