Cinema Line

Description

The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 10050 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Input

The first line contains integer n(1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 2550 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.

Output

Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".

Sample Input

Input
4
25 25 50 50
Output
YES
Input
2
25 100
Output
NO
Input
4
50 50 25 25
Output
NO

 

解题思路:

定义两个队列分别放25和50,对于输入的每个数据,如果是25则25的队列增加一位,如果是50那就要找零25,此时观察25的队列长度是否为0:如果为0,就不能找零输出NO;否则25的队列就减少一位。对于100分两种方法,第一是找零一个25和一个50,(此方法优先,因为50只有在给100找零时有用)第一种方法不满足则看第二种:找零3个25;对于两种方法都不满足则输出NO

代码:

#include <iostream>
#include<queue>
using namespace std;
int main()
{
    int n,i;
    int a[199999];
    while(cin>>n)
    {
        queue<int> b;
        queue<int> c;
        int flag=1;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(i=0;i<n;i++)
        {
            if(a[i]==25) b.push(1);
            if(a[i]==50)
            {
                if(b.size()>0)
                {b.pop();c.push(1);}
                else {cout<<"NO";flag=0; break;}
            }
        if(a[i]==100)
        {
            if(b.size()>0&&c.size()>0)
            {b.pop();c.pop();}
            else if(b.size()>=3) {b.pop();b.pop();b.pop();}
            else {cout<<"NO";flag=0;break;}
        }
    }
    if(flag) cout<<"YES";
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值