python【蓝桥杯vip练习题库】ALGO-232找零钱(贪心 模拟)

试题 算法训练 找零钱

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  有n个人正在饭堂排队买海北鸡饭。每份海北鸡饭要25元。奇怪的是,每个人手里只有一张钞票(每张钞票的面值为25、50、100元),而且饭堂阿姨一开始没有任何零钱。请问饭堂阿姨能否给所有人找零(假设饭堂阿姨足够聪明)
输入格式
  第一行一个整数n,表示排队的人数。

接下来n个整数a[1],a[2],…,a[n]。a[i]表示第i位学生手里钞票的价值(i越小,在队伍里越靠前)
输出格式
  输出YES或者NO
样例输入
4
25 25 50 50
样例输出
YES
样例输入
2
25 100
样例输出
NO
样例输入
4
25 25 50 100
样例输出
YES
数据规模和约定
  n不超过1000000

"""
@Author:Lixiang

@Blog(个人博客地址): https://lixiang007.top/

@WeChat:18845312866

"""
import math
import string
import sys
import cmath
c1=0
c2=0
n=int(input())
a=list(map(int,input().strip().split()))
for i in range(n):
    if a[i]==25:
        c1+=1
    elif a[i]==50:
        c1-=1
        c2+=1
    elif a[i]==100:
        if c2>0:
            c2-=1
            c1-=1
        else:
            c1-=3
    if c1<0:
        print("NO")
        break
if c1>=0:
    print("YES")

在这里插入图片描述

发布了829 篇原创文章 · 获赞 215 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_43838785/article/details/104657028
今日推荐