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 100, 50 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 25, 50 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”.
Examples
Input
4
25 25 50 50
Output
YES
Input
2
25 100
Output
NO
Input
4
50 50 25 25
Output
NO
题意描述:
一群人排队付款,他们每个人手里有面值为25,50,100其中一种的一张钞票,且每个人都需付25元,此时收银员的手里没有钱,问:在不打乱顺序的情况下,收银员能否给每个顾客都找零钱
解题思路
这是一道比较简单的贪心题目,联系实际生活,思路很容易出来,很显然如果第一个付款的人付的为100或者50面值的钞票,那么肯定不能找零;顾客用25元付的不用找零,用50的付款需要1张25的找零,用100的付款的比较特殊,有50的话优先使用,此时还需要1张25的,没有50的话,就使用3张25的。那么就判断面值为25的钞票的数量,一旦不够了,就不能找零;如果到最后一位顾客25面值的还足够,那么就能成功找零
代码实现
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int s[N];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> s[i];
if (s[0] == 50 || s[0] == 100)//第一个付款的人付的为100或者50面值
{
cout << "NO" << endl;
return 0;
}
int a = 0, b = 0, c = 0;//分别为25,50,100
int flag = 1;
for (int i = 0; i < n; i++)
{
if (s[i] == 25)//收25
a++;
else if( s[i] == 50)//收50
{
b++;
a--;
}
else if (s[i] == 100)//收100
{
c++;
if (b > 0) {
b--;
a--;
}
else a -= 3;
}
if (a < 0)//判断25的够不够
{
flag = 0;
cout << "NO" << endl;
return 0;
}
}
if (flag)cout << "YES" << endl;
return 0;
}