原题
描述
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
题意
排队的人按照输入的顺序依次依次付钱,问是否能找给每个人零钱。
思路:
有三种人,要尽可能满足所有的人,
拿50的 --只能用25的找
拿100的—50+25或者3*25
为了找给更多的人,对一百的人要优先用50+25的方法,因为这样可以尽可能留出更多的25,因为25的更好用。
解题过程:
一个个的考虑每一个人
遇到25的sum25++,不用找钱。
遇到50的,sum50++,sum25–。
遇到100的,不用统计100的数量,因为100的不能用来找给别人,没用。
找钱的时候优先sum50–;sum25–;
再考虑sum25-=3;
如果在中间出现断了就输出NO
正常结束就说明每个人都可以被满足,输出YES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
using namespace std;
int a[100005];
int main()
{
int n;
int num50=0,num25=0,num100=0;
bool p=1,q=1;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
}
for(int i=1; i<=n; i++)
{
if(a[i]==25)
{
num25++;
continue;
}
if(a[i]==50)
{num50++;
if(num25)
{
num25--;
continue;
}
else
{
cout<<"NO";
p=0;
break;
}
}
if(a[i]==100)
{
if(num50&&num25)
{
num50--;
num25--;
}
else
{
if(num25>=3)
{
num25-=3;
}
else
{
cout<<"NO";
p=0;
break;
}
}
}
}
if(p)
cout<<"YES";
}